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  import static org.junit.Assert.fail;
26  
27  import java.io.BufferedInputStream;
28  import java.io.EOFException;
29  import java.io.File;
30  import java.io.FileInputStream;
31  import java.io.IOException;
32  import java.io.InputStream;
33  import java.util.Arrays;
34  import java.util.zip.ZipException;
35  
36  import org.apache.commons.compress.archivers.ArchiveEntry;
37  import org.apache.commons.compress.utils.IOUtils;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  public class ZipArchiveInputStreamTest {
42  
43      /**
44       * @see "https://issues.apache.org/jira/browse/COMPRESS-176"
45       */
46      @Test
47      public void winzipBackSlashWorkaround() throws Exception {
48          ZipArchiveInputStream in = null;
49          try {
50              in = new ZipArchiveInputStream(new FileInputStream(getFile("test-winzip.zip")));
51              ZipArchiveEntry zae = in.getNextZipEntry();
52              zae = in.getNextZipEntry();
53              zae = in.getNextZipEntry();
54              assertEquals("\u00e4/", zae.getName());
55          } finally {
56              if (in != null) {
57                  in.close();
58              }
59          }
60      }
61  
62      /**
63       * @see "https://issues.apache.org/jira/browse/COMPRESS-189"
64       */
65      @Test
66      public void properUseOfInflater() throws Exception {
67          ZipFile zf = null;
68          ZipArchiveInputStream in = null;
69          try {
70              zf = new ZipFile(getFile("COMPRESS-189.zip"));
71              final ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP");
72              in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)));
73              ZipArchiveEntry innerEntry;
74              while ((innerEntry = in.getNextZipEntry()) != null) {
75                  if (innerEntry.getName().endsWith("XML")) {
76                      assertTrue(0 < in.read());
77                  }
78              }
79          } finally {
80              if (zf != null) {
81                  zf.close();
82              }
83              if (in != null) {
84                  in.close();
85              }
86          }
87      }
88  
89      @Test
90      public void shouldConsumeArchiveCompletely() throws Exception {
91          final InputStream is = ZipArchiveInputStreamTest.class
92              .getResourceAsStream("/archive_with_trailer.zip");
93          final ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
94          while (zip.getNextZipEntry() != null) {
95              // just consume the archive
96          }
97          final byte[] expected = new byte[] {
98              'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'
99          };
100         final byte[] actual = new byte[expected.length];
101         is.read(actual);
102         assertArrayEquals(expected, actual);
103         zip.close();
104     }
105 
106     /**
107      * @see "https://issues.apache.org/jira/browse/COMPRESS-219"
108      */
109     @Test
110     public void shouldReadNestedZip() throws IOException {
111         ZipArchiveInputStream in = null;
112         try {
113             in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-219.zip")));
114             extractZipInputStream(in);
115         } finally {
116             if (in != null) {
117                 in.close();
118             }
119         }
120     }
121 
122     private void extractZipInputStream(final ZipArchiveInputStream in)
123         throws IOException {
124         ZipArchiveEntry zae = in.getNextZipEntry();
125         while (zae != null) {
126             if (zae.getName().endsWith(".zip")) {
127                 extractZipInputStream(new ZipArchiveInputStream(in));
128             }
129             zae = in.getNextZipEntry();
130         }
131     }
132 
133     @Test
134     public void testUnshrinkEntry() throws Exception {
135         final ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("SHRUNK.ZIP")));
136         
137         ZipArchiveEntry entry = in.getNextZipEntry();
138         assertEquals("method", ZipMethod.UNSHRINKING.getCode(), entry.getMethod());
139         assertTrue(in.canReadEntryData(entry));
140         
141         FileInputStream original = new FileInputStream(getFile("test1.xml"));
142         try {
143             assertArrayEquals(IOUtils.toByteArray(original), IOUtils.toByteArray(in));
144         } finally {
145             original.close();
146         }
147         
148         entry = in.getNextZipEntry();
149         assertEquals("method", ZipMethod.UNSHRINKING.getCode(), entry.getMethod());
150         assertTrue(in.canReadEntryData(entry));
151         
152         original = new FileInputStream(getFile("test2.xml"));
153         try {
154             assertArrayEquals(IOUtils.toByteArray(original), IOUtils.toByteArray(in));
155         } finally {
156             original.close();
157         }
158     }
159 
160 
161     /**
162      * Test case for 
163      * <a href="https://issues.apache.org/jira/browse/COMPRESS-264"
164      * >COMPRESS-264</a>.
165      */
166     @Test
167     public void testReadingOfFirstStoredEntry() throws Exception {
168 
169         try (ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-264.zip")))) {
170             final ZipArchiveEntry ze = in.getNextZipEntry();
171             assertEquals(5, ze.getSize());
172             assertArrayEquals(new byte[] { 'd', 'a', 't', 'a', '\n' },
173                     IOUtils.toByteArray(in));
174         }
175     }
176 
177     /**
178      * Test case for
179      * <a href="https://issues.apache.org/jira/browse/COMPRESS-351"
180      * >COMPRESS-351</a>.
181      */
182     @Test
183     public void testMessageWithCorruptFileName() throws Exception {
184         try (ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-351.zip")))) {
185             ZipArchiveEntry ze = in.getNextZipEntry();
186             while (ze != null) {
187                 ze = in.getNextZipEntry();
188             }
189             fail("expected EOFException");
190         } catch (final EOFException ex) {
191             final String m = ex.getMessage();
192             assertTrue(m.startsWith("Truncated ZIP entry: ?2016")); // the first character is not printable
193         }
194     }
195 
196     @Test
197     public void testUnzipBZip2CompressedEntry() throws Exception {
198 
199         try (ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("bzip2-zip.zip")))) {
200             final ZipArchiveEntry ze = in.getNextZipEntry();
201             assertEquals(42, ze.getSize());
202             final byte[] expected = new byte[42];
203             Arrays.fill(expected, (byte) 'a');
204             assertArrayEquals(expected, IOUtils.toByteArray(in));
205         }
206     }
207 
208     /**
209      * Test case for
210      * <a href="https://issues.apache.org/jira/browse/COMPRESS-364"
211      * >COMPRESS-364</a>.
212      */
213     @Test
214     public void testWithBytesAfterData() throws Exception {
215         final int expectedNumEntries = 2;
216         final InputStream is = ZipArchiveInputStreamTest.class
217                 .getResourceAsStream("/archive_with_bytes_after_data.zip");
218         final ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
219 
220         try {
221             int actualNumEntries = 0;
222             ZipArchiveEntry zae = zip.getNextZipEntry();
223             while (zae != null) {
224                 actualNumEntries++;
225                 readEntry(zip, zae);
226                 zae = zip.getNextZipEntry();
227             }
228             assertEquals(expectedNumEntries, actualNumEntries);
229         } finally {
230             zip.close();
231         }
232     }
233 
234     /**
235      * <code>getNextZipEntry()</code> should throw a <code>ZipException</code> rather than return
236      * <code>null</code> when an unexpected structure is encountered.
237      */
238     @Test
239     public void testThrowOnInvalidEntry() throws Exception {
240         final InputStream is = ZipArchiveInputStreamTest.class
241                 .getResourceAsStream("/invalid-zip.zip");
242         final ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
243 
244         try {
245             zip.getNextZipEntry();
246             fail("IOException expected");
247         } catch (ZipException expected) {
248             assertTrue(expected.getMessage().contains("Unexpected record signature"));
249         } finally {
250             zip.close();
251         }
252     }
253 
254     /**
255      * Test correct population of header and data offsets.
256      */
257     @Test
258     public void testOffsets() throws Exception {
259         // mixed.zip contains both inflated and stored files
260         try (InputStream archiveStream = ZipArchiveInputStream.class.getResourceAsStream("/mixed.zip");
261              ZipArchiveInputStream zipStream =  new ZipArchiveInputStream((archiveStream))
262         ) {
263             ZipArchiveEntry inflatedEntry = zipStream.getNextZipEntry();
264             Assert.assertEquals("inflated.txt", inflatedEntry.getName());
265             Assert.assertEquals(0x0000, inflatedEntry.getLocalHeaderOffset());
266             Assert.assertEquals(0x0046, inflatedEntry.getDataOffset());
267             ZipArchiveEntry storedEntry = zipStream.getNextZipEntry();
268             Assert.assertEquals("stored.txt", storedEntry.getName());
269             Assert.assertEquals(0x5892, storedEntry.getLocalHeaderOffset());
270             Assert.assertEquals(0x58d6, storedEntry.getDataOffset());
271             Assert.assertNull(zipStream.getNextZipEntry());
272         }
273     }
274 
275     private static byte[] readEntry(ZipArchiveInputStream zip, ZipArchiveEntry zae) throws IOException {
276         final int len = (int)zae.getSize();
277         final byte[] buff = new byte[len];
278         zip.read(buff, 0, len);
279 
280         return buff;
281     }
282 }