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