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