private ZipArchiveEntryRequest(final ZipArchiveEntry zipArchiveEntry, final InputStreamSupplier payloadSupplier) { // this constructor has "safe" access to all member variables on zipArchiveEntry this.zipArchiveEntry = zipArchiveEntry; this.payloadSupplier = payloadSupplier; this.method = zipArchiveEntry.getMethod(); }
/** * Whether this library supports the compression method used by * the given entry. * * @return true if the compression method is supported */ private static boolean supportsMethodOf(final ZipArchiveEntry entry) { return entry.getMethod() == ZipEntry.STORED || entry.getMethod() == ZipMethod.UNSHRINKING.getCode() || entry.getMethod() == ZipMethod.IMPLODING.getCode() || entry.getMethod() == ZipEntry.DEFLATED || entry.getMethod() == ZipMethod.ENHANCED_DEFLATED.getCode() || entry.getMethod() == ZipMethod.BZIP2.getCode(); }
/** * Ensures all bytes sent to the deflater are written to the stream. */ private void flushDeflater() throws IOException { if (entry.entry.getMethod() == DEFLATED) { streamCompressor.flushDeflater(); } }
/** * Whether this entry requires a data descriptor this library can work with. * * @return true if allowStoredEntriesWithDataDescriptor is true, * the entry doesn't require any data descriptor or the method is * DEFLATED or ENHANCED_DEFLATED. */ private boolean supportsDataDescriptorFor(final ZipArchiveEntry entry) { return !entry.getGeneralPurposeBit().usesDataDescriptor() || (allowStoredEntriesWithDataDescriptor && entry.getMethod() == ZipEntry.STORED) || entry.getMethod() == ZipEntry.DEFLATED || entry.getMethod() == ZipMethod.ENHANCED_DEFLATED.getCode(); }
/** * If the mode is AsNeeded and the entry is a compressed entry of * unknown size that gets written to a non-seekable stream then * change the default to Never. * * @since 1.3 */ private Zip64Mode getEffectiveZip64Mode(final ZipArchiveEntry ze) { if (zip64Mode != Zip64Mode.AsNeeded || channel != null || ze.getMethod() != DEFLATED || ze.getSize() != ArchiveEntry.SIZE_UNKNOWN) { return zip64Mode; } return Zip64Mode.Never; }
/** * Whether the compressed size for the entry is either known or * not required by the compression method being used. */ private boolean supportsCompressedSizeFor(final ZipArchiveEntry entry) { return entry.getCompressedSize() != ArchiveEntry.SIZE_UNKNOWN || entry.getMethod() == ZipEntry.DEFLATED || entry.getMethod() == ZipMethod.ENHANCED_DEFLATED.getCode() || (entry.getGeneralPurposeBit().usesDataDescriptor() && allowStoredEntriesWithDataDescriptor && entry.getMethod() == ZipEntry.STORED); }
final int method = zipArchiveEntry.getMethod(); if (method == ZipMethod.UNKNOWN_CODE) { throw new IllegalArgumentException("Method must be set on zipArchiveEntry: " + zipArchiveEntry);
/** * Whether this stream is able to write the given entry. * * <p>May return false if it is set up to use encryption or a * compression method that hasn't been implemented yet.</p> * @since 1.1 */ @Override public boolean canWriteEntryData(final ArchiveEntry ae) { if (ae instanceof ZipArchiveEntry) { final ZipArchiveEntry zae = (ZipArchiveEntry) ae; return zae.getMethod() != ZipMethod.IMPLODING.getCode() && zae.getMethod() != ZipMethod.UNSHRINKING.getCode() && ZipUtil.canHandleEntryData(zae); } return false; }
/** * @since 1.17 */ @Override public long getCompressedCount() { if (current.entry.getMethod() == ZipArchiveOutputStream.STORED) { return current.bytesRead; } else if (current.entry.getMethod() == ZipArchiveOutputStream.DEFLATED) { return getBytesInflated(); } else if (current.entry.getMethod() == ZipMethod.UNSHRINKING.getCode()) { return ((UnshrinkingInputStream) current.in).getCompressedCount(); } else if (current.entry.getMethod() == ZipMethod.IMPLODING.getCode()) { return ((ExplodingInputStream) current.in).getCompressedCount(); } else if (current.entry.getMethod() == ZipMethod.ENHANCED_DEFLATED.getCode()) { return ((Deflate64CompressorInputStream) current.in).getCompressedCount(); } else if (current.entry.getMethod() == ZipMethod.BZIP2.getCode()) { return ((BZip2CompressorInputStream) current.in).getCompressedCount(); } else { return -1; } }
/** * Provides default values for compression method and last * modification time. */ private void setDefaults(final ZipArchiveEntry entry) { if (entry.getMethod() == -1) { // not specified entry.setMethod(method); } if (entry.getTime() == -1) { // not specified entry.setTime(System.currentTimeMillis()); } }
/** * Writes bytes to ZIP entry. * @param b the byte array to write * @param offset the start position to write from * @param length the number of bytes to write * @throws IOException on error */ @Override public void write(final byte[] b, final int offset, final int length) throws IOException { if (entry == null) { throw new IllegalStateException("No current entry"); } ZipUtil.checkRequestedFeatures(entry.entry); final long writtenThisTime = streamCompressor.write(b, offset, length, entry.entry.getMethod()); count(writtenThisTime); }
if (current.entry.getMethod() == ZipArchiveOutputStream.STORED) { read = readStored(buffer, offset, length); } else if (current.entry.getMethod() == ZipArchiveOutputStream.DEFLATED) { read = readDeflated(buffer, offset, length); } else if (current.entry.getMethod() == ZipMethod.UNSHRINKING.getCode() || current.entry.getMethod() == ZipMethod.IMPLODING.getCode() || current.entry.getMethod() == ZipMethod.ENHANCED_DEFLATED.getCode() || current.entry.getMethod() == ZipMethod.BZIP2.getCode()) { read = current.in.read(buffer, offset, length); } else { throw new UnsupportedZipFeatureException(ZipMethod.getMethodByCode(current.entry.getMethod()), current.entry);
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(); }
/** * Checks whether the entry requires features not (yet) supported * by the library and throws an exception if it does. */ static void checkRequestedFeatures(final ZipArchiveEntry ze) throws UnsupportedZipFeatureException { if (!supportsEncryptionOf(ze)) { throw new UnsupportedZipFeatureException(UnsupportedZipFeatureException .Feature.ENCRYPTION, ze); } if (!supportsMethodOf(ze)) { final ZipMethod m = ZipMethod.getMethodByCode(ze.getMethod()); if (m == null) { throw new UnsupportedZipFeatureException(UnsupportedZipFeatureException .Feature.METHOD, ze); } throw new UnsupportedZipFeatureException(m, ze); } } }
final Zip64Mode effectiveMode) throws ZipException { if (entry.entry.getMethod() == DEFLATED) {
/** * Throws an exception if the size is unknown for a stored entry * that is written to a non-seekable output or the entry is too * big to be written without Zip64 extra but the mode has been set * to Never. */ private void validateSizeInformation(final Zip64Mode effectiveMode) throws ZipException { // Size/CRC not required if SeekableByteChannel is used if (entry.entry.getMethod() == STORED && channel == null) { if (entry.entry.getSize() == ArchiveEntry.SIZE_UNKNOWN) { throw new ZipException("uncompressed size is required for" + " STORED method when not writing to a" + " file"); } if (entry.entry.getCrc() == ZipArchiveEntry.CRC_UNKNOWN) { throw new ZipException("crc checksum is required for STORED" + " method when not writing to a file"); } entry.entry.setCompressedSize(entry.entry.getSize()); } if ((entry.entry.getSize() >= ZIP64_MAGIC || entry.entry.getCompressedSize() >= ZIP64_MAGIC) && effectiveMode == Zip64Mode.Never) { throw new Zip64RequiredException(Zip64RequiredException .getEntryTooBigMessage(entry.entry)); } }
/** * Writes the data descriptor entry. * @param ze the entry to write * @throws IOException on error */ protected void writeDataDescriptor(final ZipArchiveEntry ze) throws IOException { if (!usesDataDescriptor(ze.getMethod(), false)) { return; } writeCounted(DD_SIG); writeCounted(ZipLong.getBytes(ze.getCrc())); if (!hasZip64Extra(ze)) { writeCounted(ZipLong.getBytes(ze.getCompressedSize())); writeCounted(ZipLong.getBytes(ze.getSize())); } else { writeCounted(ZipEightByteInteger.getBytes(ze.getCompressedSize())); writeCounted(ZipEightByteInteger.getBytes(ze.getSize())); } }
writeOut(ZipShort.getBytes(versionNeededToExtract(entry.entry.getMethod(), false, false)));