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.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     // not really a 7z test, but I didn't feel like adding a new test just for this
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             // File.list may return a/b/c or a/b/d.txt first
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 }