View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.compress.archivers.zip;
18  
19  import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest;
20  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  
23  import java.io.ByteArrayInputStream;
24  import java.io.File;
25  import java.io.InputStream;
26  import java.util.zip.ZipEntry;
27  
28  import org.apache.commons.compress.AbstractTempDirTest;
29  import org.apache.commons.compress.parallel.InputStreamSupplier;
30  import org.apache.commons.io.IOUtils;
31  import org.junit.jupiter.api.Test;
32  
33  public class ScatterZipOutputStreamTest extends AbstractTempDirTest {
34  
35      private InputStreamSupplier createPayloadSupplier(final ByteArrayInputStream payload) {
36          return () -> payload;
37      }
38  
39      @Test
40      public void testPutArchiveEntry() throws Exception {
41          final File scatterFile = createTempFile("scattertest", ".notzip");
42          final File target = createTempFile("scattertest", ".zip");
43          final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
44          final byte[] A_PAYLOAD = "XAAY".getBytes();
45          try (ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.fileBased(scatterFile)) {
46  
47              final ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
48              zab.setMethod(ZipEntry.DEFLATED);
49              final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
50              scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
51  
52              final ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
53              zae.setMethod(ZipEntry.DEFLATED);
54              final ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
55              scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
56  
57              try (ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target)) {
58                  scatterZipOutputStream.writeTo(outputStream);
59              }
60          }
61  
62          try (ZipFile zf = ZipFile.builder().setFile(target).get()) {
63              final ZipArchiveEntry bEntry = zf.getEntries("b.txt").iterator().next();
64              assertEquals(8, bEntry.getSize());
65              try (InputStream inputStream = zf.getInputStream(bEntry)) {
66                  assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(inputStream));
67              }
68  
69              final ZipArchiveEntry aEntry = zf.getEntries("a.txt").iterator().next();
70              assertEquals(4, aEntry.getSize());
71              try (InputStream inputStream = zf.getInputStream(aEntry)) {
72                  assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(inputStream));
73              }
74          }
75      }
76  }