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.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.BufferedInputStream;
25 import java.io.File;
26 import java.io.InputStream;
27 import java.nio.file.Files;
28
29 import org.apache.commons.compress.AbstractTest;
30 import org.apache.commons.compress.compressors.z.ZCompressorInputStream;
31 import org.junit.jupiter.api.Test;
32
33 public final class ZTest extends AbstractTest {
34
35 @Test
36 public void testMatches() {
37 assertFalse(ZCompressorInputStream.matches(new byte[] { 1, 2, 3, 4 }, 4));
38 assertFalse(ZCompressorInputStream.matches(new byte[] { 0x1f, 2, 3, 4 }, 4));
39 assertFalse(ZCompressorInputStream.matches(new byte[] { 1, (byte) 0x9d, 3, 4 }, 4));
40 assertFalse(ZCompressorInputStream.matches(new byte[] { 0x1f, (byte) 0x9d, 3, 4 }, 3));
41 assertTrue(ZCompressorInputStream.matches(new byte[] { 0x1f, (byte) 0x9d, 3, 4 }, 4));
42 }
43
44 private void testUnarchive(final StreamWrapper<CompressorInputStream> wrapper) throws Exception {
45 final File input = getFile("bla.tar.Z");
46 final File output = newTempFile("bla.tar");
47 try (InputStream is = Files.newInputStream(input.toPath())) {
48 try (InputStream in = wrapper.wrap(is)) {
49 Files.copy(in, output.toPath());
50 }
51 }
52 }
53
54 @Test
55 public void testZUnarchive() throws Exception {
56 testUnarchive(ZCompressorInputStream::new);
57 }
58
59 @Test
60 public void testZUnarchiveViaAutoDetection() throws Exception {
61 testUnarchive(is -> new CompressorStreamFactory().createCompressorInputStream(new BufferedInputStream(is)));
62 }
63
64 @Test
65 public void testZUnarchiveViaFactory() throws Exception {
66 testUnarchive(is -> new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.Z, is));
67 }
68
69 }