View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   *
17   */
18  
19  package org.apache.commons.compress.archivers.zip;
20  
21  import static org.apache.commons.compress.AbstractTestCase.getFile;
22  import static org.junit.Assert.assertArrayEquals;
23  import static org.junit.Assert.assertEquals;
24  import static org.junit.Assert.assertTrue;
25  
26  import java.io.BufferedInputStream;
27  import java.io.FileInputStream;
28  import java.io.InputStream;
29  import java.util.Arrays;
30  import java.io.IOException;
31  
32  import org.apache.commons.compress.utils.IOUtils;
33  import org.junit.Test;
34  
35  public class ZipArchiveInputStreamTest {
36  
37      /**
38       * @see "https://issues.apache.org/jira/browse/COMPRESS-176"
39       */
40      @Test
41      public void winzipBackSlashWorkaround() throws Exception {
42          ZipArchiveInputStream in = null;
43          try {
44              in = new ZipArchiveInputStream(new FileInputStream(getFile("test-winzip.zip")));
45              ZipArchiveEntry zae = in.getNextZipEntry();
46              zae = in.getNextZipEntry();
47              zae = in.getNextZipEntry();
48              assertEquals("\u00e4/", zae.getName());
49          } finally {
50              if (in != null) {
51                  in.close();
52              }
53          }
54      }
55  
56      /**
57       * @see "https://issues.apache.org/jira/browse/COMPRESS-189"
58       */
59      @Test
60      public void properUseOfInflater() throws Exception {
61          ZipFile zf = null;
62          ZipArchiveInputStream in = null;
63          try {
64              zf = new ZipFile(getFile("COMPRESS-189.zip"));
65              ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP");
66              in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)));
67              ZipArchiveEntry innerEntry;
68              while ((innerEntry = in.getNextZipEntry()) != null) {
69                  if (innerEntry.getName().endsWith("XML")) {
70                      assertTrue(0 < in.read());
71                  }
72              }
73          } finally {
74              if (zf != null) {
75                  zf.close();
76              }
77              if (in != null) {
78                  in.close();
79              }
80          }
81      }
82  
83      @Test
84      public void shouldConsumeArchiveCompletely() throws Exception {
85          InputStream is = ZipArchiveInputStreamTest.class
86              .getResourceAsStream("/archive_with_trailer.zip");
87          ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
88          while (zip.getNextZipEntry() != null) {
89              // just consume the archive
90          }
91          byte[] expected = new byte[] {
92              'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'
93          };
94          byte[] actual = new byte[expected.length];
95          is.read(actual);
96          assertArrayEquals(expected, actual);
97          zip.close();
98      }
99  
100     /**
101      * @see "https://issues.apache.org/jira/browse/COMPRESS-219"
102      */
103     @Test
104     public void shouldReadNestedZip() throws IOException {
105         ZipArchiveInputStream in = null;
106         try {
107             in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-219.zip")));
108             extractZipInputStream(in);
109         } finally {
110             if (in != null) {
111                 in.close();
112             }
113         }
114     }
115 
116     private void extractZipInputStream(final ZipArchiveInputStream in)
117         throws IOException {
118         ZipArchiveEntry zae = in.getNextZipEntry();
119         while (zae != null) {
120             if (zae.getName().endsWith(".zip")) {
121                 extractZipInputStream(new ZipArchiveInputStream(in));
122             }
123             zae = in.getNextZipEntry();
124         }
125     }
126 
127     @Test
128     public void testUnshrinkEntry() throws Exception {
129         ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("SHRUNK.ZIP")));
130         
131         ZipArchiveEntry entry = in.getNextZipEntry();
132         assertEquals("method", ZipMethod.UNSHRINKING.getCode(), entry.getMethod());
133         assertTrue(in.canReadEntryData(entry));
134         
135         FileInputStream original = new FileInputStream(getFile("test1.xml"));
136         try {
137             assertArrayEquals(IOUtils.toByteArray(original), IOUtils.toByteArray(in));
138         } finally {
139             original.close();
140         }
141         
142         entry = in.getNextZipEntry();
143         assertEquals("method", ZipMethod.UNSHRINKING.getCode(), entry.getMethod());
144         assertTrue(in.canReadEntryData(entry));
145         
146         original = new FileInputStream(getFile("test2.xml"));
147         try {
148             assertArrayEquals(IOUtils.toByteArray(original), IOUtils.toByteArray(in));
149         } finally {
150             original.close();
151         }
152     }
153 
154 
155     /**
156      * Test case for 
157      * <a href="https://issues.apache.org/jira/browse/COMPRESS-264"
158      * >COMPRESS-264</a>.
159      */
160     @Test
161     public void testReadingOfFirstStoredEntry() throws Exception {
162         ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-264.zip")));
163         
164         try {
165             ZipArchiveEntry ze = in.getNextZipEntry();
166             assertEquals(5, ze.getSize());
167             assertArrayEquals(new byte[] {'d', 'a', 't', 'a', '\n'},
168                               IOUtils.toByteArray(in));
169         } finally {
170             in.close();
171         }
172     }
173 
174     @Test
175     public void testUnzipBZip2CompressedEntry() throws Exception {
176         ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("bzip2-zip.zip")));
177         
178         try {
179             ZipArchiveEntry ze = in.getNextZipEntry();
180             assertEquals(42, ze.getSize());
181             byte[] expected = new byte[42];
182             Arrays.fill(expected , (byte)'a');
183             assertArrayEquals(expected, IOUtils.toByteArray(in));
184         } finally {
185             in.close();
186         }
187     }
188 }