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  
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      * @see "https://issues.apache.org/jira/browse/COMPRESS-84"
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                 // the whole point of the test
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 }