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.File;
22  import java.io.FileInputStream;
23  import java.io.FileOutputStream;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  
27  import org.apache.commons.compress.AbstractTestCase;
28  import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
29  import org.apache.commons.compress.utils.IOUtils;
30  
31  public final class BZip2TestCase extends AbstractTestCase {
32  
33      public void testBzipCreation()  throws Exception {
34          File output = null;
35          final File input = getFile("test.txt");
36          {
37              output = new File(dir, "test.txt.bz2");
38              final OutputStream out = new FileOutputStream(output);
39              final CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("bzip2", out);
40              FileInputStream in = new FileInputStream(input);
41              IOUtils.copy(in, cos);
42              cos.close();
43              in.close();
44          }
45  
46          final File decompressed = new File(dir, "decompressed.txt");
47          {
48              final File toDecompress = output;
49              final InputStream is = new FileInputStream(toDecompress);
50              final CompressorInputStream in =
51                  new CompressorStreamFactory().createCompressorInputStream("bzip2", is);
52              FileOutputStream os = new FileOutputStream(decompressed);
53              IOUtils.copy(in, os);
54              is.close();
55              os.close();
56          }
57  
58          assertEquals(input.length(),decompressed.length());
59      }
60  
61      public void testBzip2Unarchive() throws Exception {
62          final File input = getFile("bla.txt.bz2");
63          final File output = new File(dir, "bla.txt");
64          final InputStream is = new FileInputStream(input);
65          final CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("bzip2", is);
66          FileOutputStream os = new FileOutputStream(output);
67          IOUtils.copy(in, os);
68          is.close();
69          os.close();
70      }
71  
72      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
73          final File input = getFile("multiple.bz2");
74          final InputStream is = new FileInputStream(input);
75          try {
76              final CompressorInputStream in = new CompressorStreamFactory()
77                  .createCompressorInputStream("bzip2", is);
78              try {
79                  assertEquals('a', in.read());
80                  assertEquals(-1, in.read());
81              } finally {
82                  in.close();
83              }
84          } finally {
85              is.close();
86          }
87      }
88  
89      public void testConcatenatedStreamsReadFully() throws Exception {
90          final File input = getFile("multiple.bz2");
91          final InputStream is = new FileInputStream(input);
92          try {
93              final CompressorInputStream in =
94                  new BZip2CompressorInputStream(is, true);
95              try {
96                  assertEquals('a', in.read());
97                  assertEquals('b', in.read());
98                  assertEquals(0, in.available());
99                  assertEquals(-1, in.read());
100             } finally {
101                 in.close();
102             }
103         } finally {
104             is.close();
105         }
106     }
107 
108     public void testCOMPRESS131() throws Exception {
109         final File input = getFile("COMPRESS-131.bz2");
110         final InputStream is = new FileInputStream(input);
111         try {
112             final CompressorInputStream in =
113                 new BZip2CompressorInputStream(is, true);
114             try {
115                 int l = 0;
116                 while(in.read() != -1) {
117                     l++;
118                 }
119                 assertEquals(539, l);
120             } finally {
121                 in.close();
122             }
123         } finally {
124             is.close();
125         }
126     }
127 
128 }