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.assertThrows;
24 import static org.junit.jupiter.api.Assertions.assertTrue;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.File;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.nio.file.Files;
32 import java.util.zip.CRC32;
33 import java.util.zip.CheckedOutputStream;
34
35 import org.apache.commons.io.IOUtils;
36 import org.apache.commons.io.input.BoundedInputStream;
37 import org.junit.jupiter.api.Test;
38
39 public class ExplodeSupportTest {
40
41 private void testArchiveWithImplodeCompression(final String fileName, final String entryName) throws IOException {
42 try (ZipFile zip = ZipFile.builder().setFile(fileName).get()) {
43 final ZipArchiveEntry entry = zip.getEntries().nextElement();
44 assertEquals(entryName, entry.getName(), "entry name");
45 assertTrue(zip.canReadEntryData(entry), "entry can't be read");
46 assertEquals(ZipMethod.IMPLODING.getCode(), entry.getMethod(), "method");
47
48 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
49 final CheckedOutputStream out = new CheckedOutputStream(bout, new CRC32());
50 try (InputStream inputStream = zip.getInputStream(entry)) {
51 IOUtils.copy(inputStream, out);
52 out.flush();
53 }
54 assertEquals(entry.getCrc(), out.getChecksum().getValue(), "CRC32");
55 }
56 }
57
58 @Test
59 public void testArchiveWithImplodeCompression4K2Trees() throws IOException {
60 testArchiveWithImplodeCompression("target/test-classes/imploding-4Kdict-2trees.zip", "HEADER.TXT");
61 }
62
63 @Test
64 public void testArchiveWithImplodeCompression8K3Trees() throws IOException {
65 testArchiveWithImplodeCompression("target/test-classes/imploding-8Kdict-3trees.zip", "LICENSE.TXT");
66 }
67
68 @Test
69 public void testConstructorThrowsExceptions() {
70 assertThrows(IllegalArgumentException.class, () -> new ExplodingInputStream(4095, 2, new ByteArrayInputStream(new byte[] {})),
71 "should have failed with illegal argument exception");
72
73 assertThrows(IllegalArgumentException.class, () -> new ExplodingInputStream(4096, 4, new ByteArrayInputStream(new byte[] {})),
74 "should have failed with illegal argument exception");
75 }
76
77 @Test
78 public void testTikaTestArchive() throws IOException {
79 testArchiveWithImplodeCompression("target/test-classes/moby-imploded.zip", "README");
80 }
81
82 @Test
83 public void testTikaTestStream() throws IOException {
84 testZipStreamWithImplodeCompression("target/test-classes/moby-imploded.zip", "README");
85 }
86
87 private void testZipStreamWithImplodeCompression(final String fileName, final String entryName) throws IOException {
88 try (ZipArchiveInputStream zin = new ZipArchiveInputStream(Files.newInputStream(new File(fileName).toPath()))) {
89 final ZipArchiveEntry entry = zin.getNextZipEntry();
90 assertEquals(entryName, entry.getName(), "entry name");
91 assertTrue(zin.canReadEntryData(entry), "entry can't be read");
92 assertEquals(ZipMethod.IMPLODING.getCode(), entry.getMethod(), "method");
93 try (InputStream bio = new BoundedInputStream(zin, entry.getSize())) {
94 final ByteArrayOutputStream bout = new ByteArrayOutputStream();
95 final CheckedOutputStream out = new CheckedOutputStream(bout, new CRC32());
96 IOUtils.copy(bio, out);
97 out.flush();
98 assertEquals(entry.getCrc(), out.getChecksum().getValue(), "CRC32");
99 }
100 }
101 }
102
103 @Test
104 public void testZipStreamWithImplodeCompression4K2Trees() throws IOException {
105 testZipStreamWithImplodeCompression("target/test-classes/imploding-4Kdict-2trees.zip", "HEADER.TXT");
106 }
107
108 @Test
109 public void testZipStreamWithImplodeCompression8K3Trees() throws IOException {
110 testZipStreamWithImplodeCompression("target/test-classes/imploding-8Kdict-3trees.zip", "LICENSE.TXT");
111 }
112
113 }