View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.compress.archivers.zip;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertNotNull;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.nio.charset.StandardCharsets;
29  import java.nio.file.Files;
30  import java.util.Enumeration;
31  
32  import org.apache.commons.compress.AbstractTest;
33  import org.junit.jupiter.api.Test;
34  import org.junit.jupiter.api.io.TempDir;
35  
36  class ZipFileIgnoringLocalFileHeaderTest {
37  
38      private static ZipFile openZipWithoutLocalFileHeader(final String fileName) throws IOException {
39          // @formatter:off
40          return ZipFile.builder()
41                  .setFile(AbstractTest.getFile(fileName))
42                  .setCharset(StandardCharsets.UTF_8.name())
43                  .setUseUnicodeExtraFields(true)
44                  .setIgnoreLocalFileHeader(true)
45                  .get();
46          // @formatter:on
47      }
48  
49      private static ZipFile openZipWithoutLocalFileHeaderDeprecated(final String fileName) throws IOException {
50          return new ZipFile(AbstractTest.getFile(fileName), StandardCharsets.UTF_8.name(), true, true);
51      }
52  
53      @TempDir
54      private File dir;
55  
56      @Test
57      void testDuplicateEntry() throws Exception {
58          try (ZipFile zf = openZipWithoutLocalFileHeader("COMPRESS-227.zip")) {
59              int numberOfEntries = 0;
60              for (final ZipArchiveEntry entry : zf.getEntries("test1.txt")) {
61                  numberOfEntries++;
62                  try (InputStream inputStream = zf.getInputStream(entry)) {
63                      assertNotNull(inputStream);
64                  }
65              }
66              assertEquals(2, numberOfEntries);
67          }
68      }
69  
70      @Test
71      void testGetEntryWorks() throws IOException {
72          try (ZipFile zf = openZipWithoutLocalFileHeader("bla.zip")) {
73              final ZipArchiveEntry ze = zf.getEntry("test1.xml");
74              assertEquals(610, ze.getSize());
75          }
76      }
77  
78      @Test
79      void testGetRawInputStreamReturnsNotNull() throws IOException {
80          try (ZipFile zf = openZipWithoutLocalFileHeader("bla.zip")) {
81              final ZipArchiveEntry ze = zf.getEntry("test1.xml");
82              try (InputStream rawInputStream = zf.getRawInputStream(ze)) {
83                  assertNotNull(rawInputStream);
84              }
85          }
86      }
87  
88      @Test
89      void testPhysicalOrder() throws IOException {
90          try (ZipFile zf = openZipWithoutLocalFileHeader("ordertest.zip")) {
91              final Enumeration<ZipArchiveEntry> e = zf.getEntriesInPhysicalOrder();
92              ZipArchiveEntry ze;
93              do {
94                  ze = e.nextElement();
95              } while (e.hasMoreElements());
96              assertEquals("src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java", ze.getName());
97          }
98      }
99  
100     /**
101      * Simple unarchive test. Asserts nothing.
102      *
103      * @throws Exception
104      */
105     @Test
106     void testZipUnarchive() throws Exception {
107         try (ZipFile zipFile = openZipWithoutLocalFileHeaderDeprecated("bla.zip")) {
108             zipFile.stream().forEach(entry -> {
109                 try (InputStream inputStream = zipFile.getInputStream(entry)) {
110                     Files.copy(inputStream, new File(dir, entry.getName()).toPath());
111                 }
112             });
113         }
114     }
115 }