这个例子:http ://cheeso.members.winisp.net/srcview.aspx?dir=js-unzip
展示了如何在 Javascript 中创建 ZIP 文件。现在,我知道您想要 ZLIB 或 DEFLATE 压缩,而不是 ZIP。但是,ZIP 使用 DEFLATE,并且在该示例的 .js 文件中,有一个 InflatingReader 类可以在读取时进行 INFLATE。
该类公开了这些方法:
readByte()
returns null when EOF is reached, or the value of the byte when successful.
readToEnd()
returns an array of all bytes read, to EOF
beginReadToEnd(callback)
async version of the above
readBytes(n)
returns an array of n bytes read from the source.
beginReadBytes(n, callback)
async version of the above
如果您想要INFLATE ,您可以不加改动地使用该代码。
如果您想要ZLIB(也称为解压缩),那么在读取压缩字节和执行 INFLATE 之前,您需要读取和验证一个 2 字节签名。只需修改 InflatingReader 以读取和转储 2 个字节,它就可以很好地执行 ZLIB。