微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Zlib解压缩时不正确的标头检查 用于提取已下载文件的代码:完整zipinfo -v:

如何解决Zlib解压缩时不正确的标头检查 用于提取已下载文件的代码:完整zipinfo -v:

我需要从服务器接收一个zip文件,将其解压缩并将其内容传输到其他位置。

但是,当尝试使用createInflate从zlib内置软件包中提取它时,出现错误Error: incorrect header check

(我也尝试过createUnzipcreateGunzip

使用cUrl下载文件并使用unzip linux命令将其解压缩即可。

$ unzip report.zip 
Archive:  report.zip
  inflating: report.csv
$ zipinfo -v report.zip
[...]
  file system or operating system of origin:      MS-DOS,OS/2 or NT FAT
  version of encoding software:                   2.0
  minimum file system compatibility required:     MS-DOS,OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
[...]

用于提取已下载文件代码

const pipeline = promisify(stream.pipeline);

(async () => {
  const unzipper = createInflate();
  const sourceStream = fs.createReadStream('report.zip');
  const destStream = fs.createWriteStream('report.csv');

  await pipeline(sourceStream,unzipper,destStream);
})();

请注意,直接传递响应和传递createReadStream结果之间的错误是相同的

完整zipinfo -v

$ zipinfo -v report.zip
Archive:  report.zip
There is no zipfile comment.

End-of-central-directory record:
-------------------------------

  Zip archive file size:                       527 (000000000000020Fh)
  Actual end-cent-dir record offset:           505 (00000000000001F9h)
  Expected end-cent-dir record offset:         505 (00000000000001F9h)
  (based on the length of the central directory and its expected offset)

  This zipfile constitutes the sole disk of a single-part archive; its
  central directory contains 1 entry.
  The central directory is 78 (000000000000004Eh) bytes long,and its (expected) offset in bytes from the beginning of the zipfile
  is 427 (00000000000001ABh).


Central directory entry #1:
---------------------------

  report_SMS_1f7c2069_20200730.csv

  offset of local header from start of archive:   0
                                                  (0000000000000000h) bytes
  file system or operating system of origin:      MS-DOS,OS/2 or NT FAT
  minimum software version required to extract:   2.0
  compression method:                             deflated
  compression sub-type (deflation):               normal
  file security status:                           not encrypted
  extended local header:                          yes
  file last modified on (DOS date/time):          2020 Jul 30 11:05:48
  32-bit CRC value (hex):                         5abe6238
  compressed size:                                349 bytes
  uncompressed size:                              934 bytes
  length of filename:                             32 characters
  length of extra field:                          0 bytes
  length of file comment:                         0 characters
  disk number on which file begins:               disk 1
  apparent file type:                             binary
  non-MSDOS external file attributes:             000000 hex
  MS-DOS file attributes (00 hex):                none

  There is no file comment.

解决方法

zlib不是zip。 zip不是zlib。它们是两种不同的格式。 gzip是另一个。 (在node.js zlib界面中使用“解压缩”一词具有误导性。)

您需要解压缩zip文件的工具。看看this

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。