1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.zip;
20
21 import static org.apache.commons.compress.AbstractTestCase.getFile;
22 import static org.apache.commons.compress.AbstractTestCase.mkdir;
23 import static org.apache.commons.compress.AbstractTestCase.rmdir;
24 import static org.junit.Assert.assertArrayEquals;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.BufferedInputStream;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.InputStream;
33 import java.io.IOException;
34 import java.io.OutputStream;
35
36 import org.junit.Test;
37 import org.apache.commons.compress.utils.IOUtils;
38
39 public class ZipArchiveInputStreamTest {
40
41
42
43
44 @Test
45 public void winzipBackSlashWorkaround() throws Exception {
46 ZipArchiveInputStream in = null;
47 try {
48 in = new ZipArchiveInputStream(new FileInputStream(getFile("test-winzip.zip")));
49 ZipArchiveEntry zae = in.getNextZipEntry();
50 zae = in.getNextZipEntry();
51 zae = in.getNextZipEntry();
52 assertEquals("\u00e4/", zae.getName());
53 } finally {
54 if (in != null) {
55 in.close();
56 }
57 }
58 }
59
60
61
62
63 @Test
64 public void properUseOfInflater() throws Exception {
65 ZipFile zf = null;
66 ZipArchiveInputStream in = null;
67 try {
68 zf = new ZipFile(getFile("COMPRESS-189.zip"));
69 ZipArchiveEntry zae = zf.getEntry("USD0558682-20080101.ZIP");
70 in = new ZipArchiveInputStream(new BufferedInputStream(zf.getInputStream(zae)));
71 ZipArchiveEntry innerEntry;
72 while ((innerEntry = in.getNextZipEntry()) != null) {
73 if (innerEntry.getName().endsWith("XML")) {
74 assertTrue(0 < in.read());
75 }
76 }
77 } finally {
78 if (zf != null) {
79 zf.close();
80 }
81 if (in != null) {
82 in.close();
83 }
84 }
85 }
86
87 @Test
88 public void shouldConsumeArchiveCompletely() throws Exception {
89 InputStream is = ZipArchiveInputStreamTest.class
90 .getResourceAsStream("/archive_with_trailer.zip");
91 ZipArchiveInputStream zip = new ZipArchiveInputStream(is);
92 while (zip.getNextZipEntry() != null) {
93
94 }
95 byte[] expected = new byte[] {
96 'H', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n'
97 };
98 byte[] actual = new byte[expected.length];
99 is.read(actual);
100 assertArrayEquals(expected, actual);
101 zip.close();
102 }
103
104
105
106
107 @Test
108 public void shouldReadNestedZip() throws IOException {
109 ZipArchiveInputStream in = null;
110 try {
111 in = new ZipArchiveInputStream(new FileInputStream(getFile("COMPRESS-219.zip")));
112 extractZipInputStream(in);
113 } finally {
114 if (in != null) {
115 in.close();
116 }
117 }
118 }
119
120 private void extractZipInputStream(final ZipArchiveInputStream in)
121 throws IOException {
122 ZipArchiveEntry zae = in.getNextZipEntry();
123 while (zae != null) {
124 if (zae.getName().endsWith(".zip")) {
125 extractZipInputStream(new ZipArchiveInputStream(in));
126 }
127 zae = in.getNextZipEntry();
128 }
129 }
130 }