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.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest;
22  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  
25  import java.io.ByteArrayInputStream;
26  import java.io.File;
27  import java.io.InputStream;
28  import java.util.zip.ZipEntry;
29  
30  import org.apache.commons.compress.AbstractTempDirTest;
31  import org.apache.commons.compress.parallel.InputStreamSupplier;
32  import org.apache.commons.io.IOUtils;
33  import org.junit.jupiter.api.Test;
34  
35  class ScatterZipOutputStreamTest extends AbstractTempDirTest {
36  
37      private InputStreamSupplier createPayloadSupplier(final ByteArrayInputStream payload) {
38          return () -> payload;
39      }
40  
41      @Test
42      void testPutArchiveEntry() throws Exception {
43          final File scatterFile = createTempFile("scattertest", ".notzip");
44          final File target = createTempFile("scattertest", ".zip");
45          final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
46          final byte[] A_PAYLOAD = "XAAY".getBytes();
47          try (ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.fileBased(scatterFile)) {
48  
49              final ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
50              zab.setMethod(ZipEntry.DEFLATED);
51              final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
52              scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
53  
54              final ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
55              zae.setMethod(ZipEntry.DEFLATED);
56              final ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
57              scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
58  
59              try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target)) {
60                  scatterZipOutputStream.writeTo(outputStream);
61              }
62          }
63  
64          try (ZipFile zf = ZipFile.builder().setFile(target).get()) {
65              final ZipArchiveEntry bEntry = zf.getEntries("b.txt").iterator().next();
66              assertEquals(8, bEntry.getSize());
67              try (InputStream inputStream = zf.getInputStream(bEntry)) {
68                  assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
69              }
70  
71              final ZipArchiveEntry aEntry = zf.getEntries("a.txt").iterator().next();
72              assertEquals(4, aEntry.getSize());
73              try (InputStream inputStream = zf.getInputStream(aEntry)) {
74                  assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
75              }
76          }
77      }
78  }