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