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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.compress.archivers.zip;
20
import org.apache.commons.compress.parallel.InputStreamSupplier;
21
import org.apache.commons.compress.utils.IOUtils;
22
import org.junit.After;
23
import org.junit.Test;
25
import java.io.ByteArrayInputStream;
27
import java.io.InputStream;
29
import static org.apache.commons.compress.AbstractTestCase.tryHardToDelete;
30
import static org.apache.commons.compress.archivers.zip.ZipArchiveEntryRequest.createZipArchiveEntryRequest;
31
import static org.junit.Assert.assertArrayEquals;
32
import static org.junit.Assert.assertEquals;
34
public class ScatterZipOutputStreamTest {
36
private File scatterFile = null;
37
private File target = null;
40
public void cleanup() {
41
tryHardToDelete(scatterFile);
42
tryHardToDelete(target);
46
public void putArchiveEntry() throws Exception {
47
scatterFile = File.createTempFile("scattertest", ".notzip");
48
ScatterZipOutputStream scatterZipOutputStream = ScatterZipOutputStream.fileBased(scatterFile);
49
final byte[] B_PAYLOAD = "RBBBBBBS".getBytes();
50
final byte[] A_PAYLOAD = "XAAY".getBytes();
52
ZipArchiveEntry zab = new ZipArchiveEntry("b.txt");
53
zab.setMethod(ZipArchiveEntry.DEFLATED);
54
final ByteArrayInputStream payload = new ByteArrayInputStream(B_PAYLOAD);
55
scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zab, createPayloadSupplier(payload)));
57
ZipArchiveEntry zae = new ZipArchiveEntry("a.txt");
58
zae.setMethod(ZipArchiveEntry.DEFLATED);
59
ByteArrayInputStream payload1 = new ByteArrayInputStream(A_PAYLOAD);
60
scatterZipOutputStream.addArchiveEntry(createZipArchiveEntryRequest(zae, createPayloadSupplier(payload1)));
62
target = File.createTempFile("scattertest", ".zip");
63
ZipArchiveOutputStream outputStream = new ZipArchiveOutputStream(target);
64
scatterZipOutputStream.writeTo( outputStream);
66
scatterZipOutputStream.close();
68
ZipFile zf = new ZipFile(target);
69
final ZipArchiveEntry b_entry = zf.getEntries("b.txt").iterator().next();
70
assertEquals(8, b_entry.getSize());
71
assertArrayEquals(B_PAYLOAD, IOUtils.toByteArray(zf.getInputStream(b_entry)));
73
final ZipArchiveEntry a_entry = zf.getEntries("a.txt").iterator().next();
74
assertEquals(4, a_entry.getSize());
75
assertArrayEquals(A_PAYLOAD, IOUtils.toByteArray(zf.getInputStream(a_entry)));
79
private InputStreamSupplier createPayloadSupplier(final ByteArrayInputStream payload) {
80
return new InputStreamSupplier() {
81
public InputStream get() {