1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.compressors;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.ByteArrayOutputStream;
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29
30 import org.apache.commons.compress.AbstractTestCase;
31 import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
32 import org.apache.commons.compress.utils.IOUtils;
33
34 public final class GZipTestCase extends AbstractTestCase {
35
36 public void testGzipCreation() throws Exception {
37 final File input = getFile("test1.xml");
38 final File output = new File(dir, "test1.xml.gz");
39 final OutputStream out = new FileOutputStream(output);
40 try {
41 final CompressorOutputStream cos = new CompressorStreamFactory()
42 .createCompressorOutputStream("gz", out);
43 try {
44 IOUtils.copy(new FileInputStream(input), cos);
45 } finally {
46 cos.close();
47 }
48 } finally {
49 out.close();
50 }
51 }
52
53 public void testGzipUnarchive() throws Exception {
54 final File input = getFile("bla.tgz");
55 final File output = new File(dir, "bla.tar");
56 final InputStream is = new FileInputStream(input);
57 try {
58 final CompressorInputStream in = new CompressorStreamFactory()
59 .createCompressorInputStream("gz", is);
60 FileOutputStream out = null;
61 try {
62 out = new FileOutputStream(output);
63 IOUtils.copy(in, out);
64 } finally {
65 if (out != null) {
66 out.close();
67 }
68 in.close();
69 }
70 } finally {
71 is.close();
72 }
73 }
74
75 public void testConcatenatedStreamsReadFirstOnly() throws Exception {
76 final File input = getFile("multiple.gz");
77 final InputStream is = new FileInputStream(input);
78 try {
79 final CompressorInputStream in = new CompressorStreamFactory()
80 .createCompressorInputStream("gz", is);
81 try {
82 assertEquals('a', in.read());
83 assertEquals(-1, in.read());
84 } finally {
85 in.close();
86 }
87 } finally {
88 is.close();
89 }
90 }
91
92 public void testConcatenatedStreamsReadFully() throws Exception {
93 final File input = getFile("multiple.gz");
94 final InputStream is = new FileInputStream(input);
95 try {
96 final CompressorInputStream in =
97 new GzipCompressorInputStream(is, true);
98 try {
99 assertEquals('a', in.read());
100 assertEquals('b', in.read());
101 assertEquals(0, in.available());
102 assertEquals(-1, in.read());
103 } finally {
104 in.close();
105 }
106 } finally {
107 is.close();
108 }
109 }
110
111
112
113
114 public void testCorruptedInput() throws Exception {
115 InputStream in = null;
116 OutputStream out = null;
117 CompressorInputStream cin = null;
118 try {
119 in = new FileInputStream(getFile("bla.tgz"));
120 out = new ByteArrayOutputStream();
121 IOUtils.copy(in, out);
122 in.close();
123 out.close();
124
125 byte[] data = ((ByteArrayOutputStream) out).toByteArray();
126 in = new ByteArrayInputStream(data, 0, data.length - 1);
127 cin = new CompressorStreamFactory()
128 .createCompressorInputStream("gz", in);
129 out = new ByteArrayOutputStream();
130
131 try {
132 IOUtils.copy(cin, out);
133 fail("Expected an exception");
134 } catch (IOException ioex) {
135
136 }
137
138 } finally {
139 if (out != null) {
140 out.close();
141 }
142 if (cin != null) {
143 cin.close();
144 }
145 if (in != null) {
146 in.close();
147 }
148 }
149 }
150 }