phpapache-flexcompressionzipadler32

Php: How to calculate Adler32 checksum for zip?


I'm using a combination of Paul Duncans php ZipStream (http://pablotron.org/software/zipstream-php/) on the server side, for on-the-fly creation of zips, and Fzip (http://codeazur.com.br/lab/fzip/) on the Flex/Air client side.

Works fine in Air, but when running Flex in browser, the zip needs to include a Adler32 checksum in the header for FZip to be read.

How can I calculate an Adler32 checksum for the zip in php?

The ZipStream core functions, using gzdeflate for compression, can be seen below.

Regards / Jonas

function add_file($name, $data, $opt = array(), $deflateLevel=0) {
    # compress data

    $zdata = gzdeflate($data, $deflateLevel);

    # calculate header attributes

    $crc  = crc32($data);
    $zlen = strlen($zdata);
    $len  = strlen($data);
    $meth = 0x08;

    # send file header
    $this->add_file_header($name, $opt, $meth, $crc, $zlen, $len);

    # print data
    $this->send($zdata);
}

private function add_file_header($name, $opt, $meth, $crc, $zlen, $len) {
    # strip leading slashes from file name
    # (fixes bug in windows archive viewer)
    $name = preg_replace('/^\\/+/', '', $name);

    # calculate name length
    $nlen = strlen($name);

    # create dos timestamp
    $opt['time'] = $opt['time'] ? $opt['time'] : time();
    $dts = $this->dostime($opt['time']);

    # build file header
    $fields = array(            # (from V.A of APPNOTE.TXT)
    array('V', 0x04034b50),     # local file header signature
    array('v', (6 << 8) + 3),   # version needed to extract
    array('v', 0x00),           # general purpose bit flag
    array('v', $meth),          # compresion method (deflate or store)
    array('V', $dts),           # dos timestamp
    array('V', $crc),           # crc32 of data
    array('V', $zlen),          # compressed data length
    array('V', $len),           # uncompressed data length
    array('v', $nlen),          # filename length
    array('v', 0),              # extra data len
    );

    # pack fields and calculate "total" length
    $ret = $this->pack_fields($fields);
    $cdr_len = strlen($ret) + $nlen + $zlen;

    # print header and filename
    $this->send($ret . $name);

    # add to central directory record and increment offset
    $this->add_to_cdr($name, $opt, $meth, $crc, $zlen, $len, $cdr_len);
}

Solution

  • Tanslated from the example implementation in the Wikipedia article:

    define('MOD_ADLER', 65521);
    
    function adler32($data) {
        $a = 1; $b = 0; $len = strlen($data);
        for ($index = 0; $index < $len; ++$index) {
            $a = ($a + $data[$index]) % MOD_ADLER;
            $b = ($b + $a) % MOD_ADLER;
        }
        return ($b << 16) | $a;
    }
    

    And to convert that integer value to bytes:

    pack('H*', $checksum);