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.jupiter.api.Assertions.assertArrayEquals;
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.BufferedInputStream;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.OutputStream;
29  import java.nio.file.Files;
30  import java.nio.file.Path;
31  
32  import org.apache.commons.compress.AbstractTest;
33  import org.apache.commons.compress.compressors.lzma.LZMACompressorInputStream;
34  import org.apache.commons.io.IOUtils;
35  import org.junit.jupiter.api.Test;
36  
37  public final class LZMATest extends AbstractTest {
38  
39      @Test
40      public void testLzmaRoundtrip() throws Exception {
41          final Path input = getPath("test1.xml");
42          final File compressed = newTempFile("test1.xml.xz");
43          try (OutputStream out = Files.newOutputStream(compressed.toPath())) {
44              try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("lzma", out)) {
45                  Files.copy(input, cos);
46              }
47          }
48          final byte[] orig = Files.readAllBytes(input);
49          final byte[] uncompressed;
50          try (InputStream is = Files.newInputStream(compressed.toPath());
51                  CompressorInputStream in = new LZMACompressorInputStream(is)) {
52              uncompressed = IOUtils.toByteArray(in);
53          }
54          assertArrayEquals(orig, uncompressed);
55      }
56  
57      @Test
58      public void testLZMAUnarchive() throws Exception {
59          final File input = getFile("bla.tar.lzma");
60          final File output = newTempFile("bla.tar");
61          try (InputStream is = Files.newInputStream(input.toPath())) {
62              try (CompressorInputStream in = new LZMACompressorInputStream(is)) {
63                  Files.copy(in, output.toPath());
64              }
65          }
66      }
67  
68      @Test
69      public void testLZMAUnarchiveWithAutodetection() throws Exception {
70          final File input = getFile("bla.tar.lzma");
71          final File output = newTempFile("bla.tar");
72          try (InputStream is = new BufferedInputStream(Files.newInputStream(input.toPath()))) {
73              try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(is)) {
74                  Files.copy(in, output.toPath());
75              }
76          }
77      }
78  
79      @Test
80      public void testMultiByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
81          final File input = getFile("bla.tar.lzma");
82          final byte[] buf = new byte[2];
83          try (InputStream is = Files.newInputStream(input.toPath())) {
84              try (LZMACompressorInputStream in = new LZMACompressorInputStream(is)) {
85                  IOUtils.toByteArray(in);
86                  assertEquals(-1, in.read(buf));
87                  assertEquals(-1, in.read(buf));
88              }
89          }
90      }
91  
92      @Test
93      public void testSingleByteReadConsistentlyReturnsMinusOneAtEof() throws IOException {
94          final File input = getFile("bla.tar.lzma");
95          try (InputStream is = Files.newInputStream(input.toPath())) {
96              try (LZMACompressorInputStream in = new LZMACompressorInputStream(is)) {
97                  IOUtils.toByteArray(in);
98                  assertEquals(-1, in.read());
99                  assertEquals(-1, in.read());
100             }
101         }
102     }
103 }