001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.commons.compress.archivers.zip;
019
020
021import org.apache.commons.compress.parallel.FileBasedScatterGatherBackingStore;
022import org.apache.commons.compress.parallel.ScatterGatherBackingStore;
023import org.apache.commons.compress.utils.BoundedInputStream;
024
025import java.io.Closeable;
026import java.io.File;
027import java.io.FileNotFoundException;
028import java.io.IOException;
029import java.io.InputStream;
030import java.util.Queue;
031import java.util.concurrent.ConcurrentLinkedQueue;
032import java.util.zip.Deflater;
033
034/**
035 * A zip output stream that is optimized for multi-threaded scatter/gather construction of zip files.
036 * <p>
037 * The internal data format of the entries used by this class are entirely private to this class
038 * and are not part of any public api whatsoever.
039 * </p>
040 * <p>It is possible to extend this class to support different kinds of backing storage, the default
041 * implementation only supports file-based backing.
042 * </p>
043 * Thread safety: This class supports multiple threads. But the "writeTo" method must be called
044 * by the thread that originally created the {@link ZipArchiveEntry}.
045 *
046 * @since 1.10
047 */
048public class ScatterZipOutputStream implements Closeable {
049    private final Queue<CompressedEntry> items = new ConcurrentLinkedQueue<CompressedEntry>();
050    private final ScatterGatherBackingStore backingStore;
051    private final StreamCompressor streamCompressor;
052
053    private static class CompressedEntry {
054        final ZipArchiveEntryRequest zipArchiveEntryRequest;
055        final long crc;
056        final long compressedSize;
057        final long size;
058
059        public CompressedEntry(ZipArchiveEntryRequest zipArchiveEntryRequest, long crc, long compressedSize, long size) {
060            this.zipArchiveEntryRequest = zipArchiveEntryRequest;
061            this.crc = crc;
062            this.compressedSize = compressedSize;
063            this.size = size;
064        }
065
066        /**
067         * Update the original {@link ZipArchiveEntry} with sizes/crc
068         * Do not use this methods from threads that did not create the instance itself !
069         * @return the zipArchiveEntry that is basis for this request
070         */
071
072        public ZipArchiveEntry transferToArchiveEntry(){
073            ZipArchiveEntry entry = zipArchiveEntryRequest.getZipArchiveEntry();
074            entry.setCompressedSize(compressedSize);
075            entry.setSize(size);
076            entry.setCrc(crc);
077            entry.setMethod(zipArchiveEntryRequest.getMethod());
078            return entry;
079        }
080    }
081
082    public ScatterZipOutputStream(ScatterGatherBackingStore backingStore,
083                                  StreamCompressor streamCompressor) {
084        this.backingStore = backingStore;
085        this.streamCompressor = streamCompressor;
086    }
087
088    /**
089     * Add an archive entry to this scatter stream.
090     *
091     * @param zipArchiveEntryRequest The entry to write.
092     * @throws IOException    If writing fails
093     */
094    public void addArchiveEntry(ZipArchiveEntryRequest zipArchiveEntryRequest) throws IOException {
095        final InputStream payloadStream = zipArchiveEntryRequest.getPayloadStream();
096        try {
097            streamCompressor.deflate(payloadStream, zipArchiveEntryRequest.getMethod());
098        } finally {
099            payloadStream.close();
100        }
101        items.add(new CompressedEntry(zipArchiveEntryRequest, streamCompressor.getCrc32(),
102                                      streamCompressor.getBytesWrittenForLastEntry(), streamCompressor.getBytesRead()));
103    }
104
105    /**
106     * Write the contents of this scatter stream to a target archive.
107     *
108     * @param target The archive to receive the contents of this {@link ScatterZipOutputStream}.
109     * @throws IOException If writing fails
110     */
111    public void writeTo(ZipArchiveOutputStream target) throws IOException {
112        backingStore.closeForWriting();
113        InputStream data = backingStore.getInputStream();
114        for (CompressedEntry compressedEntry : items) {
115            final BoundedInputStream rawStream = new BoundedInputStream(data, compressedEntry.compressedSize);
116            target.addRawArchiveEntry(compressedEntry.transferToArchiveEntry(), rawStream);
117            rawStream.close();
118        }
119        data.close();
120    }
121
122
123    /**
124     * Closes this stream, freeing all resources involved in the creation of this stream.
125     * @throws IOException If closing fails
126     */
127    public void close() throws IOException {
128        backingStore.close();
129    }
130
131    /**
132     * Create a {@link ScatterZipOutputStream} with default compression level that is backed by a file
133     *
134     * @param file The file to offload compressed data into.
135     * @return A ScatterZipOutputStream that is ready for use.
136     * @throws FileNotFoundException if the file cannot be found
137     */
138    public static ScatterZipOutputStream fileBased(File file) throws FileNotFoundException {
139        return fileBased(file, Deflater.DEFAULT_COMPRESSION);
140    }
141
142    /**
143     * Create a {@link ScatterZipOutputStream} that is backed by a file
144     *
145     * @param file             The file to offload compressed data into.
146     * @param compressionLevel The compression level to use, @see #Deflater
147     * @return A  ScatterZipOutputStream that is ready for use.
148     * @throws FileNotFoundException if the file cannot be found
149     */
150    public static ScatterZipOutputStream fileBased(File file, int compressionLevel) throws FileNotFoundException {
151        ScatterGatherBackingStore bs = new FileBasedScatterGatherBackingStore(file);
152        StreamCompressor sc = StreamCompressor.create(compressionLevel, bs);
153        return new ScatterZipOutputStream(bs, sc);
154    }
155}