Search in sources :

Example 1 with Deflate64CompressorInputStream

use of org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream in project LibreraReader by foobnix.

the class ZipFile method getInputStream.

/**
 * Returns an InputStream for reading the contents of the given entry.
 *
 * @param ze the entry to get the stream for.
 * @return a stream to read the entry from.
 * @throws IOException if unable to create an input stream from the zipentry
 */
public InputStream getInputStream(final ZipArchiveEntry ze) throws IOException {
    if (!(ze instanceof Entry)) {
        return null;
    }
    // cast validity is checked just above
    ZipUtil.checkRequestedFeatures(ze);
    final long start = ze.getDataOffset();
    // doesn't get closed if the method is not supported - which
    // should never happen because of the checkRequestedFeatures
    // call above
    final InputStream is = // NOSONAR
    new BufferedInputStream(createBoundedInputStream(start, ze.getCompressedSize()));
    switch(ZipMethod.getMethodByCode(ze.getMethod())) {
        case STORED:
            return is;
        case UNSHRINKING:
            return new UnshrinkingInputStream(is);
        case IMPLODING:
            return new ExplodingInputStream(ze.getGeneralPurposeBit().getSlidingDictionarySize(), ze.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), is);
        case DEFLATED:
            final Inflater inflater = new Inflater(true);
            // https://docs.oracle.com/javase/7/docs/api/java/util/zip/Inflater.html#Inflater(boolean)
            return new InflaterInputStream(new SequenceInputStream(is, new ByteArrayInputStream(ONE_ZERO_BYTE)), inflater) {

                @Override
                public void close() throws IOException {
                    try {
                        super.close();
                    } finally {
                        inflater.end();
                    }
                }
            };
        case BZIP2:
            return new BZip2CompressorInputStream(is);
        case ENHANCED_DEFLATED:
            return new Deflate64CompressorInputStream(is);
        case AES_ENCRYPTED:
        case EXPANDING_LEVEL_1:
        case EXPANDING_LEVEL_2:
        case EXPANDING_LEVEL_3:
        case EXPANDING_LEVEL_4:
        case JPEG:
        case LZMA:
        case PKWARE_IMPLODING:
        case PPMD:
        case TOKENIZATION:
        case UNKNOWN:
        case WAVPACK:
        case XZ:
        default:
            throw new ZipException("Found unsupported compression method " + ze.getMethod());
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InflaterInputStream(java.util.zip.InflaterInputStream) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) SequenceInputStream(java.io.SequenceInputStream) InputStream(java.io.InputStream) InflaterInputStream(java.util.zip.InflaterInputStream) ZipException(java.util.zip.ZipException) SequenceInputStream(java.io.SequenceInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Inflater(java.util.zip.Inflater) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream)

Example 2 with Deflate64CompressorInputStream

use of org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream in project LibreraReader by foobnix.

the class ZipArchiveInputStream method getNextZipEntry.

public ZipArchiveEntry getNextZipEntry() throws IOException {
    boolean firstEntry = true;
    if (closed || hitCentralDirectory) {
        return null;
    }
    if (current != null) {
        closeEntry();
        firstEntry = false;
    }
    long currentHeaderOffset = getBytesRead();
    try {
        if (firstEntry) {
            // split archives have a special signature before the
            // first local file header - look for it and fail with
            // the appropriate error message if this is a split
            // archive.
            readFirstLocalFileHeader(lfhBuf);
        } else {
            readFully(lfhBuf);
        }
    } catch (final EOFException e) {
        return null;
    }
    final ZipLong sig = new ZipLong(lfhBuf);
    if (sig.equals(ZipLong.CFH_SIG) || sig.equals(ZipLong.AED_SIG)) {
        hitCentralDirectory = true;
        skipRemainderOfArchive();
        return null;
    }
    if (!sig.equals(ZipLong.LFH_SIG)) {
        throw new ZipException(String.format("Unexpected record signature: 0X%X", sig.getValue()));
    }
    int off = WORD;
    current = new CurrentEntry();
    final int versionMadeBy = ZipShort.getValue(lfhBuf, off);
    off += SHORT;
    current.entry.setPlatform((versionMadeBy >> ZipFile.BYTE_SHIFT) & ZipFile.NIBLET_MASK);
    final GeneralPurposeBit gpFlag = GeneralPurposeBit.parse(lfhBuf, off);
    final boolean hasUTF8Flag = gpFlag.usesUTF8ForNames();
    final ZipEncoding entryEncoding = hasUTF8Flag ? ZipEncodingHelper.UTF8_ZIP_ENCODING : zipEncoding;
    current.hasDataDescriptor = gpFlag.usesDataDescriptor();
    current.entry.setGeneralPurposeBit(gpFlag);
    off += SHORT;
    current.entry.setMethod(ZipShort.getValue(lfhBuf, off));
    off += SHORT;
    final long time = ZipUtil.dosToJavaTime(ZipLong.getValue(lfhBuf, off));
    current.entry.setTime(time);
    off += WORD;
    ZipLong size = null, cSize = null;
    if (!current.hasDataDescriptor) {
        current.entry.setCrc(ZipLong.getValue(lfhBuf, off));
        off += WORD;
        cSize = new ZipLong(lfhBuf, off);
        off += WORD;
        size = new ZipLong(lfhBuf, off);
        off += WORD;
    } else {
        off += 3 * WORD;
    }
    final int fileNameLen = ZipShort.getValue(lfhBuf, off);
    off += SHORT;
    final int extraLen = ZipShort.getValue(lfhBuf, off);
    // NOSONAR - assignment as documentation
    off += SHORT;
    final byte[] fileName = new byte[fileNameLen];
    readFully(fileName);
    current.entry.setName(entryEncoding.decode(fileName), fileName);
    if (hasUTF8Flag) {
        current.entry.setNameSource(ZipArchiveEntry.NameSource.NAME_WITH_EFS_FLAG);
    }
    final byte[] extraData = new byte[extraLen];
    readFully(extraData);
    current.entry.setExtra(extraData);
    if (!hasUTF8Flag && useUnicodeExtraFields) {
        ZipUtil.setNameAndCommentFromExtraFields(current.entry, fileName, null);
    }
    processZip64Extra(size, cSize);
    current.entry.setLocalHeaderOffset(currentHeaderOffset);
    current.entry.setDataOffset(getBytesRead());
    current.entry.setStreamContiguous(true);
    ZipMethod m = ZipMethod.getMethodByCode(current.entry.getMethod());
    if (current.entry.getCompressedSize() != ArchiveEntry.SIZE_UNKNOWN) {
        if (ZipUtil.canHandleEntryData(current.entry) && m != ZipMethod.STORED && m != ZipMethod.DEFLATED) {
            InputStream bis = new BoundedInputStream(in, current.entry.getCompressedSize());
            switch(m) {
                case UNSHRINKING:
                    current.in = new UnshrinkingInputStream(bis);
                    break;
                case IMPLODING:
                    current.in = new ExplodingInputStream(current.entry.getGeneralPurposeBit().getSlidingDictionarySize(), current.entry.getGeneralPurposeBit().getNumberOfShannonFanoTrees(), bis);
                    break;
                case BZIP2:
                    current.in = new BZip2CompressorInputStream(bis);
                    break;
                case ENHANCED_DEFLATED:
                    current.in = new Deflate64CompressorInputStream(bis);
                    break;
                default:
                    // skip unsupported entries
                    break;
            }
        }
    } else if (m == ZipMethod.ENHANCED_DEFLATED) {
        current.in = new Deflate64CompressorInputStream(in);
    }
    entriesRead++;
    return current.entry;
}
Also used : PushbackInputStream(java.io.PushbackInputStream) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ArchiveInputStream(org.apache.commons.compress.archivers.ArchiveInputStream) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) InputStream(java.io.InputStream) ZipException(java.util.zip.ZipException) BZip2CompressorInputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream) EOFException(java.io.EOFException) Deflate64CompressorInputStream(org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 ZipException (java.util.zip.ZipException)2 BZip2CompressorInputStream (org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream)2 Deflate64CompressorInputStream (org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 EOFException (java.io.EOFException)1 PushbackInputStream (java.io.PushbackInputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 Inflater (java.util.zip.Inflater)1 InflaterInputStream (java.util.zip.InflaterInputStream)1 ArchiveInputStream (org.apache.commons.compress.archivers.ArchiveInputStream)1