View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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  import java.util.zip.Deflater;
30  import java.util.zip.GZIPInputStream;
31  
32  import org.apache.commons.compress.AbstractTestCase;
33  import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
34  import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
35  import org.apache.commons.compress.compressors.gzip.GzipParameters;
36  import org.apache.commons.compress.utils.IOUtils;
37  import org.junit.Assert;
38  
39  public final class GZipTestCase extends AbstractTestCase {
40  
41      public void testGzipCreation()  throws Exception {
42          final File input = getFile("test1.xml");
43          final File output = new File(dir, "test1.xml.gz");
44          final OutputStream out = new FileOutputStream(output);
45          try {
46              final CompressorOutputStream cos = new CompressorStreamFactory()
47                  .createCompressorOutputStream("gz", out);
48              try {
49                  IOUtils.copy(new FileInputStream(input), cos);
50              } finally {
51                  cos.close();
52              }
53          } finally {
54              out.close();
55          }
56      }
57  
58      public void testGzipUnarchive() throws Exception {
59          final File input = getFile("bla.tgz");
60          final File output = new File(dir, "bla.tar");
61          final InputStream is = new FileInputStream(input);
62          try {
63              final CompressorInputStream in = new CompressorStreamFactory()
64                  .createCompressorInputStream("gz", is);
65              FileOutputStream out = null;
66              try {
67                  out = new FileOutputStream(output);
68                  IOUtils.copy(in, out);
69              } finally {
70                  if (out != null) {
71                      out.close();
72                  }
73                  in.close();
74              }
75          } finally {
76              is.close();
77          }
78      }
79  
80      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
81          final File input = getFile("multiple.gz");
82          final InputStream is = new FileInputStream(input);
83          try {
84              final CompressorInputStream in = new CompressorStreamFactory()
85                  .createCompressorInputStream("gz", is);
86              try {
87                  assertEquals('a', in.read());
88                  assertEquals(-1, in.read());
89              } finally {
90                  in.close();
91              }
92          } finally {
93              is.close();
94          }
95      }
96  
97      public void testConcatenatedStreamsReadFully() throws Exception {
98          final File input = getFile("multiple.gz");
99          final InputStream is = new FileInputStream(input);
100         try {
101             final CompressorInputStream in =
102                 new GzipCompressorInputStream(is, true);
103             try {
104                 assertEquals('a', in.read());
105                 assertEquals('b', in.read());
106                 assertEquals(0, in.available());
107                 assertEquals(-1, in.read());
108             } finally {
109                 in.close();
110             }
111         } finally {
112             is.close();
113         }
114     }
115 
116     /**
117      * @see "https://issues.apache.org/jira/browse/COMPRESS-84"
118      */
119     public void testCorruptedInput() throws Exception {
120         InputStream in = null;
121         OutputStream out = null;
122         CompressorInputStream cin = null;
123         try {
124             in = new FileInputStream(getFile("bla.tgz"));
125             out = new ByteArrayOutputStream();
126             IOUtils.copy(in, out);
127             in.close();
128             out.close();
129 
130             byte[] data = ((ByteArrayOutputStream) out).toByteArray();
131             in = new ByteArrayInputStream(data, 0, data.length - 1);
132             cin = new CompressorStreamFactory()
133                 .createCompressorInputStream("gz", in);
134             out = new ByteArrayOutputStream();
135 
136             try {
137                 IOUtils.copy(cin, out);
138                 fail("Expected an exception");
139             } catch (IOException ioex) {
140                 // the whole point of the test
141             }
142 
143         } finally {
144             if (out != null) {
145                 out.close();
146             }
147             if (cin != null) {
148                 cin.close();
149             }
150             if (in != null) {
151                 in.close();
152             }
153         }
154     }
155 
156     public void testInteroperabilityWithGzipCompressorInputStream() throws Exception {
157         FileInputStream fis = new FileInputStream(getFile("test3.xml"));
158         byte[] content;
159         try {
160             content = IOUtils.toByteArray(fis);
161         } finally {
162             fis.close();
163         }
164         
165         ByteArrayOutputStream bout = new ByteArrayOutputStream();
166 
167         GzipParameters parameters = new GzipParameters();
168         parameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
169         parameters.setOperatingSystem(3);
170         parameters.setFilename("test3.xml");
171         parameters.setComment("Test file");
172         parameters.setModificationTime(System.currentTimeMillis());
173         GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters);
174         out.write(content);
175         out.flush();
176         out.close();
177 
178         GzipCompressorInputStream in = new GzipCompressorInputStream(new ByteArrayInputStream(bout.toByteArray()));
179         byte[] content2 = IOUtils.toByteArray(in);
180 
181         Assert.assertArrayEquals("uncompressed content", content, content2);
182     }
183 
184     public void testInteroperabilityWithGZIPInputStream() throws Exception {
185         FileInputStream fis = new FileInputStream(getFile("test3.xml"));
186         byte[] content;
187         try {
188             content = IOUtils.toByteArray(fis);
189         } finally {
190             fis.close();
191         }
192         
193         ByteArrayOutputStream bout = new ByteArrayOutputStream();
194 
195         GzipParameters parameters = new GzipParameters();
196         parameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
197         parameters.setOperatingSystem(3);
198         parameters.setFilename("test3.xml");
199         parameters.setComment("Test file");
200         parameters.setModificationTime(System.currentTimeMillis());
201         GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters);
202         out.write(content);
203         out.flush();
204         out.close();
205 
206         GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(bout.toByteArray()));
207         byte[] content2 = IOUtils.toByteArray(in);
208 
209         Assert.assertArrayEquals("uncompressed content", content, content2);
210     }
211 
212     public void testInvalidCompressionLevel() {
213         GzipParameters parameters = new GzipParameters();
214         try {
215             parameters.setCompressionLevel(10);
216             fail("IllegalArgumentException not thrown");
217         } catch (IllegalArgumentException e) {
218             // expected
219         }
220         
221         try {
222             parameters.setCompressionLevel(-5);
223             fail("IllegalArgumentException not thrown");
224         } catch (IllegalArgumentException e) {
225             // expected
226         }
227     }
228 
229     private void testExtraFlags(int compressionLevel, int flag) throws Exception {
230         FileInputStream fis = new FileInputStream(getFile("test3.xml"));
231         byte[] content;
232         try {
233             content = IOUtils.toByteArray(fis);
234         } finally {
235             fis.close();
236         }
237         
238         ByteArrayOutputStream bout = new ByteArrayOutputStream();
239         
240         GzipParameters parameters = new GzipParameters();
241         parameters.setCompressionLevel(compressionLevel);
242         GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters);
243         IOUtils.copy(new ByteArrayInputStream(content), out);
244         out.flush();
245         out.close();
246         
247         assertEquals("extra flags (XFL)", flag, bout.toByteArray()[8]);
248     }
249 
250     public void testExtraFlagsFastestCompression() throws Exception {
251         testExtraFlags(Deflater.BEST_SPEED, 4);
252     }
253 
254     public void testExtraFlagsBestCompression() throws Exception {
255         testExtraFlags(Deflater.BEST_COMPRESSION, 2);
256     }
257 
258     public void testExtraFlagsDefaultCompression() throws Exception {
259         testExtraFlags(Deflater.DEFAULT_COMPRESSION, 0);
260     }
261     
262     public void testOverWrite() throws Exception {
263         GzipCompressorOutputStream out = new GzipCompressorOutputStream(new ByteArrayOutputStream());
264         out.close();
265         try {
266             out.write(0);
267             fail("IOException expected");
268         } catch (IOException e) {
269             // expected
270         }
271     }
272 
273     public void testMetadataRoundTrip() throws Exception {
274         ByteArrayOutputStream bout = new ByteArrayOutputStream();
275                 
276         GzipParameters parameters = new GzipParameters();
277         parameters.setCompressionLevel(Deflater.BEST_COMPRESSION);
278         parameters.setModificationTime(123456000);
279         parameters.setOperatingSystem(13);
280         parameters.setFilename("test3.xml");
281         parameters.setComment("Umlaute möglich?");
282         GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters);
283         FileInputStream fis = new FileInputStream(getFile("test3.xml"));
284         try {
285             IOUtils.copy(fis, out);
286         } finally {
287             fis.close();
288             out.close();
289         }
290         
291         GzipCompressorInputStream input =
292             new GzipCompressorInputStream(new ByteArrayInputStream(bout.toByteArray()));
293         input.close();
294         GzipParameters readParams = input.getMetaData();
295         assertEquals(Deflater.BEST_COMPRESSION, readParams.getCompressionLevel());
296         assertEquals(123456000, readParams.getModificationTime());
297         assertEquals(13, readParams.getOperatingSystem());
298         assertEquals("test3.xml", readParams.getFilename());
299         assertEquals("Umlaute möglich?", readParams.getComment());
300     }
301 }