1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }