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  
20  package org.apache.commons.compress.compressors.gzip;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.IOException;
25  import java.io.OutputStream;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  import java.nio.file.Path;
29  
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link GzipCompressorOutputStream}.
34   */
35  public class GzipCompressorOutputStreamTest {
36  
37      private void testFileName(final String expected, final String sourceFile) throws IOException {
38          final Path tempSourceFile = Files.createTempFile(sourceFile, sourceFile);
39          Files.write(tempSourceFile, "<text>Hello World!</text>".getBytes(StandardCharsets.ISO_8859_1));
40          final Path targetFile = Files.createTempFile("test", ".gz");
41          final GzipParameters parameters = new GzipParameters();
42          parameters.setFilename(sourceFile);
43          assertEquals(parameters.getFilename(), parameters.getFileName());
44          parameters.setFileName(sourceFile);
45          assertEquals(parameters.getFilename(), parameters.getFileName());
46          try (OutputStream fos = Files.newOutputStream(targetFile);
47                  GzipCompressorOutputStream gos = new GzipCompressorOutputStream(fos, parameters)) {
48              Files.copy(tempSourceFile, gos);
49          }
50          try (GzipCompressorInputStream gis = new GzipCompressorInputStream(Files.newInputStream(targetFile))) {
51              assertEquals(expected, gis.getMetaData().getFileName());
52              assertEquals(expected, gis.getMetaData().getFilename());
53          }
54      }
55  
56      @Test
57      public void testFileNameAscii() throws IOException {
58          testFileName("ASCII.xml", "ASCII.xml");
59      }
60  
61      /**
62       * Tests COMPRESS-638.
63       *
64       * GZip RFC requires ISO 8859-1 (LATIN-1).
65       *
66       * @throws IOException When the test fails.
67       */
68      @Test
69      public void testFileNameChinesePercentEncoded() throws IOException {
70          // "Test Chinese name"
71          testFileName("%E6%B5%8B%E8%AF%95%E4%B8%AD%E6%96%87%E5%90%8D%E7%A7%B0.xml", "\u6D4B\u8BD5\u4E2D\u6587\u540D\u79F0.xml");
72      }
73  
74  }