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.jar;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.io.IOException;
26  import java.nio.file.Files;
27  import java.nio.file.Path;
28  import java.time.Instant;
29  
30  import org.apache.commons.compress.AbstractTempDirTest;
31  import org.apache.commons.compress.AbstractTest;
32  import org.apache.commons.compress.archivers.zip.JarMarker;
33  import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
34  import org.apache.commons.compress.archivers.zip.ZipExtraField;
35  import org.apache.commons.compress.archivers.zip.ZipFile;
36  import org.junit.jupiter.api.Test;
37  
38  public class JarArchiveOutputStreamTest extends AbstractTempDirTest {
39  
40      @Test
41      public void testJarMarker() throws IOException {
42          final Path testArchive = createTempPath("jar-aostest", ".jar");
43          try (JarArchiveOutputStream out = new JarArchiveOutputStream(Files.newOutputStream(testArchive))) {
44              final ZipArchiveEntry ze1 = new ZipArchiveEntry("foo/");
45              // Ensure we won't accidentally add an Extra field.
46              ze1.setTime(Instant.parse("2022-12-27T12:10:23Z").toEpochMilli());
47              out.putArchiveEntry(ze1);
48              out.closeArchiveEntry();
49              final ZipArchiveEntry ze2 = new ZipArchiveEntry("bar/");
50              // Ensure we won't accidentally add an Extra field.
51              ze2.setTime(Instant.parse("2022-12-28T02:56:01Z").toEpochMilli());
52              out.putArchiveEntry(ze2);
53              out.closeArchiveEntry();
54              out.finish();
55          }
56          try (ZipFile zf = ZipFile.builder().setPath(testArchive).get()) {
57              ZipArchiveEntry ze = zf.getEntry("foo/");
58              assertNotNull(ze);
59              ZipExtraField[] fes = ze.getExtraFields();
60              assertEquals(1, fes.length);
61              assertTrue(fes[0] instanceof JarMarker);
62  
63              ze = zf.getEntry("bar/");
64              assertNotNull(ze);
65              fes = ze.getExtraFields();
66              assertEquals(0, fes.length);
67          } finally {
68              AbstractTest.forceDelete(testArchive);
69          }
70      }
71  
72  }