Zlib不正确header解压时检查

Zlib incorrect header check when decompressing

我需要从服务器接收一个 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
  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
  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 界面中使用“unzip”一词具有误导性。)

您需要一些可以解压缩 zip 文件的工具。看看this.