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.io.IOException;
30  
31  import org.apache.commons.compress.utils.IOUtils;
32  import org.junit.Test;
33  
34  public class ZipArchiveInputStreamTest {
35  
36      /**
37       * @see "https://issues.apache.org/jira/browse/COMPRESS-176"
38       */
39      @Test
40      public void winzipBackSlashWorkaround() throws Exception {
41          ZipArchiveInputStream in = null;
42          try {
43              in = new ZipArchiveInputStream(new FileInputStream(getFile("test-winzip.zip")));
44              ZipArchiveEntry zae = in.getNextZipEntry();
45              zae = in.getNextZipEntry();
46              zae = in.getNextZipEntry();
47              assertEquals("\u00e4/", zae.getName());
48          } finally {
49              if (in != null) {
50                  in.close();
51              }
52          }
53      }
54  
55      /**
56       * @see "https://issues.apache.org/jira/browse/COMPRESS-189"
57       */
58      @Test
59      public void properUseOfInflater() throws Exception {
60          ZipFile zf = null;
61          ZipArchiveInputStream in = null;
62          try {
63              zf = new ZipFile(getFile("COMPRESS-189.zip"));
64              ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP");
65              in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)));
66              ZipArchiveEntry innerEntry;
67              while ((innerEntry = in.getNextZipEntry()) != null) {
68                  if (innerEntry.getName().endsWith("XML")) {
69                      assertTrue(0 < in.read());
70                  }
71              }
72          } finally {
73              if (zf != null) {
74                  zf.close();
75              }
76              if (in != null) {
77                  in.close();
78              }
79          }
80      }
81  
82      @Test
83      public void shouldConsumeArchiveCompletely() throws Exception {
84          InputStream is = ZipArchiveInputStreamTest.class
85              .getResourceAsStream("/archive_with_trailer.zip");
86          ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
87          while (zip.getNextZipEntry() != null) {
88              // just consume the archive
89          }
90          byte[] expected = new byte[] {
91              'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'
92          };
93          byte[] actual = new byte[expected.length];
94          is.read(actual);
95          assertArrayEquals(expected, actual);
96          zip.close();
97      }
98  
99      /**
100      * @see "https://issues.apache.org/jira/browse/COMPRESS-219"
101      */
102     @Test
103     public void shouldReadNestedZip() throws IOException {
104         ZipArchiveInputStream in = null;
105         try {
106             in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-219.zip")));
107             extractZipInputStream(in);
108         } finally {
109             if (in != null) {
110                 in.close();
111             }
112         }
113     }
114 
115     private void extractZipInputStream(final ZipArchiveInputStream in)
116         throws IOException {
117         ZipArchiveEntry zae = in.getNextZipEntry();
118         while (zae != null) {
119             if (zae.getName().endsWith(".zip")) {
120                 extractZipInputStream(new ZipArchiveInputStream(in));
121             }
122             zae = in.getNextZipEntry();
123         }
124     }
125 
126     @Test
127     public void testUnshrinkEntry() throws Exception {
128         ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("SHRUNK.ZIP")));
129         
130         ZipArchiveEntry entry = in.getNextZipEntry();
131         assertEquals("method", ZipMethod.UNSHRINKING.getCode(), entry.getMethod());
132         assertTrue(in.canReadEntryData(entry));
133         
134         FileInputStream original = new FileInputStream(getFile("test1.xml"));
135         try {
136             assertArrayEquals(IOUtils.toByteArray(original), IOUtils.toByteArray(in));
137         } finally {
138             original.close();
139         }
140         
141         entry = in.getNextZipEntry();
142         assertEquals("method", ZipMethod.UNSHRINKING.getCode(), entry.getMethod());
143         assertTrue(in.canReadEntryData(entry));
144         
145         original = new FileInputStream(getFile("test2.xml"));
146         try {
147             assertArrayEquals(IOUtils.toByteArray(original), IOUtils.toByteArray(in));
148         } finally {
149             original.close();
150         }
151     }
152 
153 
154     /**
155      * Test case for 
156      * <a href="https://issues.apache.org/jira/browse/COMPRESS-264"
157      * >COMPRESS-264</a>.
158      */
159     @Test
160     public void testReadingOfFirstStoredEntry() throws Exception {
161         ZipArchiveInputStream in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-264.zip")));
162         
163         try {
164             ZipArchiveEntry ze = in.getNextZipEntry();
165             assertEquals(5, ze.getSize());
166             assertArrayEquals(new byte[] {'d', 'a', 't', 'a', '\n'},
167                               IOUtils.toByteArray(in));
168         } finally {
169             in.close();
170         }
171     }
172 }