1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40 return ZipFile.builder()
41 .setFile(AbstractTest.getFile(fileName))
42 .setCharset(StandardCharsets.UTF_8.name())
43 .setUseUnicodeExtraFields(true)
44 .setIgnoreLocalFileHeader(true)
45 .get();
46
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
102
103
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 }