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   *   https://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.zip;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.util.concurrent.ExecutionException;
29  import java.util.zip.ZipEntry;
30  
31  import org.apache.commons.compress.AbstractTempDirTest;
32  import org.apache.commons.compress.parallel.InputStreamSupplier;
33  import org.apache.commons.compress.utils.IOUtils;
34  import org.junit.jupiter.api.Test;
35  
36  class ScatterSampleTest extends AbstractTempDirTest {
37  
38      private void checkFile(final File result) throws IOException {
39          try (ZipFile zipFile = ZipFile.builder().setFile(result).get()) {
40              final ZipArchiveEntry archiveEntry1 = zipFile.getEntries().nextElement();
41              assertEquals("test1.xml", archiveEntry1.getName());
42              try (InputStream inputStream = zipFile.getInputStream(archiveEntry1)) {
43                  final byte[] b = new byte[6];
44                  final int i = IOUtils.readFully(inputStream, b);
45                  assertEquals(5, i);
46                  assertEquals('H', b[0]);
47                  assertEquals('o', b[4]);
48              }
49          }
50          assertTrue(result.delete());
51      }
52  
53      private void createFile(final File result) throws IOException, ExecutionException, InterruptedException {
54          final ScatterSample scatterSample = new ScatterSample(this);
55          final ZipArchiveEntry archiveEntry = new ZipArchiveEntry("test1.xml");
56          archiveEntry.setMethod(ZipEntry.DEFLATED);
57          final InputStreamSupplier supp = () -> new ByteArrayInputStream("Hello".getBytes());
58  
59          scatterSample.addEntry(archiveEntry, supp);
60          try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(result)) {
61              scatterSample.writeTo(zipArchiveOutputStream);
62          }
63      }
64  
65      @Test
66      void testSample() throws Exception {
67          final File result = createTempFile("testSample", "fe");
68          createFile(result);
69          checkFile(result);
70      }
71  }