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  package org.apache.commons.compress.archivers.zip;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.util.Enumeration;
29  
30  import org.apache.commons.compress.AbstractTest;
31  import org.junit.jupiter.api.Test;
32  import org.junit.jupiter.api.io.TempDir;
33  
34  public class ZipFileIgnoringLocalFileHeaderTest {
35  
36      private static ZipFile openZipWithoutLocalFileHeader(final String fileName) throws IOException {
37          // @formatter:off
38          return ZipFile.builder()
39                  .setFile(AbstractTest.getFile(fileName))
40                  .setCharset(StandardCharsets.UTF_8.name())
41                  .setUseUnicodeExtraFields(true)
42                  .setIgnoreLocalFileHeader(true)
43                  .get();
44          // @formatter:on
45      }
46  
47      private static ZipFile openZipWithoutLocalFileHeaderDeprecated(final String fileName) throws IOException {
48          return new ZipFile(AbstractTest.getFile(fileName), StandardCharsets.UTF_8.name(), true, true);
49      }
50  
51      @TempDir
52      private File dir;
53  
54      @Test
55      public void testDuplicateEntry() throws Exception {
56          try (ZipFile zf = openZipWithoutLocalFileHeader("COMPRESS-227.zip")) {
57              int numberOfEntries = 0;
58              for (final ZipArchiveEntry entry : zf.getEntries("test1.txt")) {
59                  numberOfEntries++;
60                  try (InputStream inputStream = zf.getInputStream(entry)) {
61                      assertNotNull(inputStream);
62                  }
63              }
64              assertEquals(2, numberOfEntries);
65          }
66      }
67  
68      @Test
69      public void testGetEntryWorks() throws IOException {
70          try (ZipFile zf = openZipWithoutLocalFileHeader("bla.zip")) {
71              final ZipArchiveEntry ze = zf.getEntry("test1.xml");
72              assertEquals(610, ze.getSize());
73          }
74      }
75  
76      @Test
77      public void testGetRawInputStreamReturnsNotNull() throws IOException {
78          try (ZipFile zf = openZipWithoutLocalFileHeader("bla.zip")) {
79              final ZipArchiveEntry ze = zf.getEntry("test1.xml");
80              try (InputStream rawInputStream = zf.getRawInputStream(ze)) {
81                  assertNotNull(rawInputStream);
82              }
83          }
84      }
85  
86      @Test
87      public void testPhysicalOrder() throws IOException {
88          try (ZipFile zf = openZipWithoutLocalFileHeader("ordertest.zip")) {
89              final Enumeration<ZipArchiveEntry> e = zf.getEntriesInPhysicalOrder();
90              ZipArchiveEntry ze;
91              do {
92                  ze = e.nextElement();
93              } while (e.hasMoreElements());
94              assertEquals("src/main/java/org/apache/commons/compress/archivers/zip/ZipUtil.java", ze.getName());
95          }
96      }
97  
98      /**
99       * Simple unarchive test. Asserts nothing.
100      *
101      * @throws Exception
102      */
103     @Test
104     public void testZipUnarchive() throws Exception {
105         try (ZipFile zf = openZipWithoutLocalFileHeaderDeprecated("bla.zip")) {
106             for (final Enumeration<ZipArchiveEntry> e = zf.getEntries(); e.hasMoreElements();) {
107                 final ZipArchiveEntry entry = e.nextElement();
108                 try (InputStream inputStream = zf.getInputStream(entry)) {
109                     Files.copy(inputStream, new File(dir, entry.getName()).toPath());
110                 }
111             }
112         }
113     }
114 }