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.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  }