FileDocCategorySizeDatePackage
ZipUtil.javaAPI DocAndroid 5.1 API4435Thu Mar 12 22:22:48 GMT 2015android.support.multidex

ZipUtil

public final class ZipUtil extends Object
Tools to build a quick partial crc of zip files.

Fields Summary
private static final int
ENDHDR
private static final int
ENDSIG
private static final int
BUFFER_SIZE
Size of reading buffers.
Constructors Summary
Methods Summary
static longcomputeCrcOfCentralDir(java.io.RandomAccessFile raf, android.support.multidex.ZipUtil$CentralDirectory dir)

        CRC32 crc = new CRC32();
        long stillToRead = dir.size;
        raf.seek(dir.offset);
        int length = (int) Math.min(BUFFER_SIZE, stillToRead);
        byte[] buffer = new byte[BUFFER_SIZE];
        length = raf.read(buffer, 0, length);
        while (length != -1) {
            crc.update(buffer, 0, length);
            stillToRead -= length;
            if (stillToRead == 0) {
                break;
            }
            length = (int) Math.min(BUFFER_SIZE, stillToRead);
            length = raf.read(buffer, 0, length);
        }
        return crc.getValue();
    
static android.support.multidex.ZipUtil$CentralDirectoryfindCentralDirectory(java.io.RandomAccessFile raf)

        long scanOffset = raf.length() - ENDHDR;
        if (scanOffset < 0) {
            throw new ZipException("File too short to be a zip file: " + raf.length());
        }

        long stopOffset = scanOffset - 0x10000 /* ".ZIP file comment"'s max length */;
        if (stopOffset < 0) {
            stopOffset = 0;
        }

        int endSig = Integer.reverseBytes(ENDSIG);
        while (true) {
            raf.seek(scanOffset);
            if (raf.readInt() == endSig) {
                break;
            }

            scanOffset--;
            if (scanOffset < stopOffset) {
                throw new ZipException("End Of Central Directory signature not found");
            }
        }
        // Read the End Of Central Directory. ENDHDR includes the signature
        // bytes,
        // which we've already read.

        // Pull out the information we need.
        raf.skipBytes(2); // diskNumber
        raf.skipBytes(2); // diskWithCentralDir
        raf.skipBytes(2); // numEntries
        raf.skipBytes(2); // totalNumEntries
        CentralDirectory dir = new CentralDirectory();
        dir.size = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
        dir.offset = Integer.reverseBytes(raf.readInt()) & 0xFFFFFFFFL;
        return dir;
    
static longgetZipCrc(java.io.File apk)
Compute crc32 of the central directory of an apk. The central directory contains the crc32 of each entries in the zip so the computed result is considered valid for the whole zip file. Does not support zip64 nor multidisk but it should be OK for now since ZipFile does not either.


                                                            
          
        RandomAccessFile raf = new RandomAccessFile(apk, "r");
        try {
            CentralDirectory dir = findCentralDirectory(raf);

            return computeCrcOfCentralDir(raf, dir);
        } finally {
            raf.close();
        }