节点:zlib.gunzip有效,但不是zlib.inflate吗?

时间:2015-03-09 13:45:44

标签: javascript node.js compression zip zlib

为什么我上传的GZIP文件可以使用zlib.gunzip()解压缩,但上传的ZIP文件导致[Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR'出现zlib.inflate()错误,使用以下代码?

if (req.body.compression == 'GZIP') {
    zlib.gunzip(body, function(err, dezipped) {
        if (typeof dezipped != 'undefined') {
            var xmlData = dezipped.toString('utf-8');
            app.parseAndOffload(req, res, xmlData);
        } else
            res.status(400).send('Could not decompress as GZIP file')
    });
    //-> Decompresses perfectly
} else 
if (req.body.compression == 'ZIP') { 
    zlib.inflate(body, function(err, dezipped) {
        if (typeof dezipped != 'undefined') {
            var xmlData = dezipped.toString('utf-8');
            app.parseAndOffload(req, res, xmlData);
        } else
            res.status(400).send('Could not decompress as ZIP file')
    });
    //-> [Error: incorrect header check] errno: -3, code: 'Z_DATA_ERROR'
}

1 个答案:

答案 0 :(得分:0)

zlib.inflate解码zlib格式,而不是zip格式。压缩格式有tools out there