/** * Create a stream compressor with the default compression level. * * @param bs The ScatterGatherBackingStore to receive output * @return A stream compressor */ public static StreamCompressor create(final ScatterGatherBackingStore bs) { return create(Deflater.DEFAULT_COMPRESSION, bs); }
/** * {@inheritDoc} * @throws Zip64RequiredException if the archive's size exceeds 4 * GByte or there are more than 65535 entries inside the archive * and {@link #setUseZip64} is {@link Zip64Mode#Never}. */ @Override public void finish() throws IOException { if (finished) { throw new IOException("This archive has already been finished"); } if (entry != null) { throw new IOException("This archive contains unclosed entries."); } cdOffset = streamCompressor.getTotalBytesWritten(); writeCentralDirectoryInChunks(); cdLength = streamCompressor.getTotalBytesWritten() - cdOffset; writeZip64CentralDirectory(); writeCentralDirectoryEnd(); metaData.clear(); entries.clear(); streamCompressor.close(); finished = true; }
/** * Add an archive entry to this scatter stream. * * @param zipArchiveEntryRequest The entry to write. * @throws IOException If writing fails */ public void addArchiveEntry(final ZipArchiveEntryRequest zipArchiveEntryRequest) throws IOException { try (final InputStream payloadStream = zipArchiveEntryRequest.getPayloadStream()) { streamCompressor.deflate(payloadStream, zipArchiveEntryRequest.getMethod()); } items.add(new CompressedEntry(zipArchiveEntryRequest, streamCompressor.getCrc32(), streamCompressor.getBytesWrittenForLastEntry(), streamCompressor.getBytesRead())); }
/** * Deflate the given source using the supplied compression method * * @param source The source to compress * @param method The #ZipArchiveEntry compression method * @throws IOException When failures happen */ public void deflate(final InputStream source, final int method) throws IOException { reset(); int length; while ((length = source.read(readerBuf, 0, readerBuf.length)) >= 0) { write(readerBuf, 0, length, method); } if (method == ZipEntry.DEFLATED) { flushDeflater(); } }
/** * Writes all necessary data for this entry. * @throws IOException on error * @throws Zip64RequiredException if the entry's uncompressed or * compressed size exceeds 4 GByte and {@link #setUseZip64} * is {@link Zip64Mode#Never}. */ @Override public void closeArchiveEntry() throws IOException { preClose(); flushDeflater(); final long bytesWritten = streamCompressor.getTotalBytesWritten() - entry.dataStart; final long realCrc = streamCompressor.getCrc32(); entry.bytesRead = streamCompressor.getBytesRead(); final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry); final boolean actuallyNeedsZip64 = handleSizesAndCrc(bytesWritten, realCrc, effectiveMode); closeEntry(actuallyNeedsZip64, false); streamCompressor.reset(); }
/** * Writes next block of compressed data to the output stream. * @throws IOException on error */ protected final void deflate() throws IOException { streamCompressor.deflate(); }
private void writeLocalFileHeader(final ZipArchiveEntry ze, final boolean phased) throws IOException { final boolean encodable = zipEncoding.canEncode(ze.getName()); final ByteBuffer name = getName(ze); if (createUnicodeExtraFields != UnicodeExtraFieldPolicy.NEVER) { addUnicodeExtraFields(ze, encodable, name); } final long localHeaderStart = streamCompressor.getTotalBytesWritten(); final byte[] localHeader = createLocalFileHeader(ze, name, encodable, phased, localHeaderStart); metaData.put(ze, new EntryMetaData(localHeaderStart, usesDataDescriptor(ze.getMethod(), phased))); entry.localDataStart = localHeaderStart + LFH_CRC_OFFSET; // At crc offset writeCounted(localHeader); entry.dataStart = streamCompressor.getTotalBytesWritten(); }
/** * Ensures all bytes sent to the deflater are written to the stream. */ private void flushDeflater() throws IOException { if (entry.entry.getMethod() == DEFLATED) { streamCompressor.flushDeflater(); } }
/** * Closes this stream, freeing all resources involved in the creation of this stream. * @throws IOException If closing fails */ @Override public void close() throws IOException { try { backingStore.close(); } finally { streamCompressor.close(); } }
private void writeDeflated(final byte[] b, final int offset, final int length) throws IOException { if (length > 0 && !def.finished()) { if (length <= DEFLATER_BLOCK_SIZE) { def.setInput(b, offset, length); deflateUntilInputIsNeeded(); } else { final int fullblocks = length / DEFLATER_BLOCK_SIZE; for (int i = 0; i < fullblocks; i++) { def.setInput(b, offset + i * DEFLATER_BLOCK_SIZE, DEFLATER_BLOCK_SIZE); deflateUntilInputIsNeeded(); } final int done = fullblocks * DEFLATER_BLOCK_SIZE; if (done < length) { def.setInput(b, offset + done, length - done); deflateUntilInputIsNeeded(); } } } }
/** * Writes all necessary data for this entry. * @throws IOException on error * @throws Zip64RequiredException if the entry's uncompressed or * compressed size exceeds 4 GByte and {@link #setUseZip64} * is {@link Zip64Mode#Never}. */ @Override public void closeArchiveEntry() throws IOException { preClose(); flushDeflater(); final long bytesWritten = streamCompressor.getTotalBytesWritten() - entry.dataStart; final long realCrc = streamCompressor.getCrc32(); entry.bytesRead = streamCompressor.getBytesRead(); final Zip64Mode effectiveMode = getEffectiveZip64Mode(entry.entry); final boolean actuallyNeedsZip64 = handleSizesAndCrc(bytesWritten, realCrc, effectiveMode); closeEntry(actuallyNeedsZip64, false); streamCompressor.reset(); }
/** * Deflate the given source using the supplied compression method * * @param source The source to compress * @param method The #ZipArchiveEntry compression method * @throws IOException When failures happen */ public void deflate(final InputStream source, final int method) throws IOException { reset(); int length; while ((length = source.read(readerBuf, 0, readerBuf.length)) >= 0) { write(readerBuf, 0, length, method); } if (method == ZipEntry.DEFLATED) { flushDeflater(); } }
void flushDeflater() throws IOException { def.finish(); while (!def.finished()) { deflate(); } }
final long offset = streamCompressor.getTotalBytesWritten();
/** * Ensures all bytes sent to the deflater are written to the stream. */ private void flushDeflater() throws IOException { if (entry.entry.getMethod() == DEFLATED) { streamCompressor.flushDeflater(); } }
/** * Closes this stream, freeing all resources involved in the creation of this stream. * @throws IOException If closing fails */ @Override public void close() throws IOException { backingStore.close(); streamCompressor.close(); }
private void writeDeflated(final byte[] b, final int offset, final int length) throws IOException { if (length > 0 && !def.finished()) { if (length <= DEFLATER_BLOCK_SIZE) { def.setInput(b, offset, length); deflateUntilInputIsNeeded(); } else { final int fullblocks = length / DEFLATER_BLOCK_SIZE; for (int i = 0; i < fullblocks; i++) { def.setInput(b, offset + i * DEFLATER_BLOCK_SIZE, DEFLATER_BLOCK_SIZE); deflateUntilInputIsNeeded(); } final int done = fullblocks * DEFLATER_BLOCK_SIZE; if (done < length) { def.setInput(b, offset + done, length - done); deflateUntilInputIsNeeded(); } } } }
/** * Create a stream compressor with the default compression level. * * @param os The stream to receive output * @return A stream compressor */ static StreamCompressor create(final OutputStream os) { return create(os, new Deflater(Deflater.DEFAULT_COMPRESSION, true)); }
/** * Add an archive entry to this scatter stream. * * @param zipArchiveEntryRequest The entry to write. * @throws IOException If writing fails */ public void addArchiveEntry(final ZipArchiveEntryRequest zipArchiveEntryRequest) throws IOException { try (final InputStream payloadStream = zipArchiveEntryRequest.getPayloadStream()) { streamCompressor.deflate(payloadStream, zipArchiveEntryRequest.getMethod()); } items.add(new CompressedEntry(zipArchiveEntryRequest, streamCompressor.getCrc32(), streamCompressor.getBytesWrittenForLastEntry(), streamCompressor.getBytesRead())); }
/** * {@inheritDoc} * @throws Zip64RequiredException if the archive's size exceeds 4 * GByte or there are more than 65535 entries inside the archive * and {@link #setUseZip64} is {@link Zip64Mode#Never}. */ @Override public void finish() throws IOException { if (finished) { throw new IOException("This archive has already been finished"); } if (entry != null) { throw new IOException("This archive contains unclosed entries."); } cdOffset = streamCompressor.getTotalBytesWritten(); writeCentralDirectoryInChunks(); cdLength = streamCompressor.getTotalBytesWritten() - cdOffset; writeZip64CentralDirectory(); writeCentralDirectoryEnd(); metaData.clear(); entries.clear(); streamCompressor.close(); finished = true; }