1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.examples;
20
21 import static java.nio.charset.StandardCharsets.UTF_8;
22 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertThrows;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.nio.channels.FileChannel;
33 import java.nio.channels.SeekableByteChannel;
34 import java.nio.file.Files;
35 import java.nio.file.StandardOpenOption;
36
37 import org.apache.commons.compress.AbstractTest;
38 import org.apache.commons.compress.archivers.ArchiveEntry;
39 import org.apache.commons.compress.archivers.ArchiveException;
40 import org.apache.commons.compress.archivers.StreamingNotSupportedException;
41 import org.apache.commons.compress.archivers.sevenz.SevenZFile;
42 import org.junit.jupiter.api.BeforeEach;
43 import org.junit.jupiter.api.Test;
44
45 public class SevenZArchiverTest extends AbstractTest {
46 private File target;
47
48 private void assertDir(final String expectedName, final ArchiveEntry entry) {
49 assertNotNull(entry, () -> expectedName + " does not exists");
50 assertEquals(expectedName + "/", entry.getName());
51 assertTrue(entry.isDirectory(), expectedName + " is not a directory");
52 }
53
54 private void assertHelloWorld(final String expectedName, final String suffix, final ArchiveEntry entry, final SevenZFile z) throws IOException {
55 assertNotNull(entry, () -> expectedName + " does not exists");
56 assertEquals(expectedName, entry.getName());
57 assertFalse(entry.isDirectory(), expectedName + " is a directory");
58 final byte[] expected = ("Hello, world " + suffix).getBytes(UTF_8);
59 final byte[] actual = new byte[expected.length];
60 assertEquals(actual.length, z.read(actual));
61 assertEquals(-1, z.read());
62 assertArrayEquals(expected, actual);
63 }
64
65 @BeforeEach
66 public void setUp() throws Exception {
67 final File c = newTempFile("a/b/c");
68 c.mkdirs();
69 try (OutputStream os = Files.newOutputStream(newTempFile("a/b/d.txt").toPath())) {
70 os.write("Hello, world 1".getBytes(UTF_8));
71 }
72 try (OutputStream os = Files.newOutputStream(newTempFile("a/b/c/e.txt").toPath())) {
73 os.write("Hello, world 2".getBytes(UTF_8));
74 }
75 target = new File(tempResultDir, "test.7z");
76 }
77
78 @Test
79 public void testChannelVersion() throws IOException, ArchiveException {
80 try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE,
81 StandardOpenOption.TRUNCATE_EXISTING)) {
82 new Archiver().create("7z", c, getTempDirFile());
83 }
84 verifyContent();
85 }
86
87 @Test
88 public void testFileVersion() throws IOException, ArchiveException {
89 new Archiver().create("7z", target, getTempDirFile());
90 verifyContent();
91 }
92
93 @Test
94 public void testOutputStreamVersion() throws IOException {
95 try (OutputStream os = Files.newOutputStream(target.toPath())) {
96 assertThrows(StreamingNotSupportedException.class, () -> new Archiver().create("7z", os, getTempDirFile()));
97 }
98 }
99
100
101 @Test
102 public void testUnknownFormat() throws IOException {
103 try (SeekableByteChannel c = FileChannel.open(target.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE,
104 StandardOpenOption.TRUNCATE_EXISTING)) {
105 assertThrows(ArchiveException.class, () -> new Archiver().create("unknown format", c, getTempDirFile()));
106 }
107 }
108
109 private void verifyContent() throws IOException {
110 try (SevenZFile z = SevenZFile.builder().setFile(target).get()) {
111 assertDir("a", z.getNextEntry());
112 assertDir("a/b", z.getNextEntry());
113 final ArchiveEntry n = z.getNextEntry();
114 assertNotNull(n);
115
116 if (n.getName().endsWith("/")) {
117 assertDir("a/b/c", n);
118 assertHelloWorld("a/b/c/e.txt", "2", z.getNextEntry(), z);
119 assertHelloWorld("a/b/d.txt", "1", z.getNextEntry(), z);
120 } else {
121 assertHelloWorld("a/b/d.txt", "1", n, z);
122 assertDir("a/b/c", z.getNextEntry());
123 assertHelloWorld("a/b/c/e.txt", "2", z.getNextEntry(), z);
124 }
125 }
126 }
127 }