我在服务器端使用了 Paul Duncans php ZipStream ( http://pablotron.org/software/zipstream-php/ ) 的组合,用于即时创建 zip 和 Fzip ( http://codeazur .com.br/lab/fzip/ ) 在 Flex/Air 客户端。
在 Air 中运行良好,但在浏览器中运行 Flex 时,zip 需要在标头中包含 Adler32 校验和,以便读取 FZip。
如何计算 php 中 zip 的 Adler32 校验和?
ZipStream 核心函数使用 gzdeflate 进行压缩,如下所示。
问候/乔纳斯
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);
}