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 java.io.File;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.nio.file.Files;
25  
26  import org.apache.commons.compress.AbstractTest;
27  import org.apache.commons.compress.compressors.deflate.DeflateCompressorInputStream;
28  import org.apache.commons.compress.compressors.deflate.DeflateCompressorOutputStream;
29  import org.apache.commons.compress.compressors.deflate.DeflateParameters;
30  import org.junit.jupiter.api.Test;
31  
32  public final class DeflateTest extends AbstractTest {
33  
34      /**
35       * Tests the creation of a DEFLATE archive with zlib header
36       *
37       * @throws Exception
38       */
39      @Test
40      public void testDeflateCreation() throws Exception {
41          final File input = getFile("test1.xml");
42          final File output = newTempFile("test1.xml.deflatez");
43          try (OutputStream out = Files.newOutputStream(output.toPath())) {
44              try (CompressorOutputStream cos = new CompressorStreamFactory().createCompressorOutputStream("deflate", out)) {
45                  Files.copy(input.toPath(), cos);
46              }
47          }
48      }
49  
50      /**
51       * Tests the extraction of a DEFLATE archive with zlib header
52       *
53       * @throws Exception
54       */
55      @Test
56      public void testDeflateUnarchive() throws Exception {
57          final File input = getFile("bla.tar.deflatez");
58          final File output = newTempFile("bla.tar");
59          try (InputStream is = Files.newInputStream(input.toPath())) {
60              // zlib header is expected by default
61              try (CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream("deflate", is)) {
62                  Files.copy(in, output.toPath());
63              }
64          }
65      }
66  
67      /**
68       * Tests the creation of a "raw" DEFLATE archive (without zlib header)
69       *
70       * @throws Exception
71       */
72      @Test
73      public void testRawDeflateCreation() throws Exception {
74          final File input = getFile("test1.xml");
75          final File output = newTempFile("test1.xml.deflate");
76          try (OutputStream out = Files.newOutputStream(output.toPath())) {
77              final DeflateParameters params = new DeflateParameters();
78              params.setWithZlibHeader(false);
79              try (CompressorOutputStream cos = new DeflateCompressorOutputStream(out, params)) {
80                  Files.copy(input.toPath(), cos);
81              }
82          }
83      }
84  
85      /**
86       * Tests the extraction of a "raw" DEFLATE archive (without zlib header)
87       *
88       * @throws Exception
89       */
90      @Test
91      public void testRawDeflateUnarchive() throws Exception {
92          final File input = getFile("bla.tar.deflate");
93          final File output = newTempFile("bla.tar");
94          try (InputStream is = Files.newInputStream(input.toPath())) {
95              final DeflateParameters params = new DeflateParameters();
96              params.setWithZlibHeader(false);
97              try (CompressorInputStream in = new DeflateCompressorInputStream(is, params)) {
98                  Files.copy(in, output.toPath());
99              }
100         }
101     }
102 }