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.xz.XZCompressorInputStream;
29  import org.apache.commons.compress.utils.IOUtils;
30  
31  public final class XZTestCase extends AbstractTestCase {
32  
33      public void testXZCreation()  throws Exception {
34          final File input = getFile("test1.xml");
35          final File output = new File(dir, "test1.xml.xz");
36          final OutputStream out = new FileOutputStream(output);
37          try {
38              final CompressorOutputStream cos = new CompressorStreamFactory()
39                  .createCompressorOutputStream("xz", out);
40              try {
41                  IOUtils.copy(new FileInputStream(input), cos);
42              } finally {
43                  cos.close();
44              }
45          } finally {
46              out.close();
47          }
48      }
49  
50      public void testXZUnarchive() throws Exception {
51          final File input = getFile("bla.tar.xz");
52          final File output = new File(dir, "bla.tar");
53          final InputStream is = new FileInputStream(input);
54          try {
55              final CompressorInputStream in = new CompressorStreamFactory()
56                  .createCompressorInputStream("xz", is);
57              FileOutputStream out = null;
58              try {
59                  out = new FileOutputStream(output);
60                  IOUtils.copy(in, out);
61              } finally {
62                  if (out != null) {
63                      out.close();
64                  }
65                  in.close();
66              }
67          } finally {
68              is.close();
69          }
70      }
71  
72      public void testConcatenatedStreamsReadFirstOnly() throws Exception {
73          final File input = getFile("multiple.xz");
74          final InputStream is = new FileInputStream(input);
75          try {
76              final CompressorInputStream in = new CompressorStreamFactory()
77                  .createCompressorInputStream("xz", 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.xz");
91          final InputStream is = new FileInputStream(input);
92          try {
93              final CompressorInputStream in =
94                  new XZCompressorInputStream(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 }