简体   繁体   English

adm-zip 不压缩数据

[英]adm-zip doesn't compress data

I'm trying to use adm-zip to add files from memory to a zip file also in memory. It seems that the zip file is created correctly (the result of saving zipData can be unzipped in Windows), but the compression ratio is always zero.我正在尝试使用 adm-zip 将文件从 memory 添加到 memory 中的 zip 文件。似乎正确创建了 zip 文件(保存 zipData 的结果可以在 Windows 中解压缩),但压缩率始终是零。

This is a model of the code that I expected to work but doesn't.这是我期望工作但没有工作的代码的 model。 As can be seen from the output, "compressedData" is null and "size" and "compressedSize" are the same whatever value is passed as the file content.从output可以看出,“compressedData”为null,“size”和“compressedSize”无论传递什么值作为文件内容都是一样的。

var admzip = require("adm-zip")
var zip = new admzip();

zip.addFile("tmp.txt", "aaaaaaaaaaaaaaaaaaaa");
var zipData = zip.toBuffer();

console.log(zip.getEntries()[0].toString());

https://runkit.com/embed/pn5kaiir12b0 https://runkit.com/embed/pn5kaiir12b0

How do I get it to compress the files as well as just zipping?我如何才能压缩文件以及压缩文件?

This is an old question but to anyone who is also experiencing this issue, the reason is that the adm-zip does not compress the data until the compressedData field is accessed for the first time.这是一个老问题,但对于同样遇到此问题的任何人来说,原因是adm-zip在第一次访问compressedData字段之前不会压缩数据。

Quote from the docs引用文档

[Buffer] Buffer compressedData When setting compressedData, the LOC Data Header must also be present at the beginning of the Buffer. [Buffer] Buffer compressedData 当设置 compressedData 时,LOC Data Header 也必须出现在 Buffer 的开头。 If the compressedData was set for a ZipEntry anf no other change was made to its properties (comment, extra etc), reading this property will return the same value.如果为 ZipEntry 设置了 compressedData 并且没有对其属性(评论、额外等)进行任何其他更改,则读取此属性将返回相同的值。 If changes had been made, reading this property will recompress the data and recreate the entry headers.如果进行了更改,读取此属性将重新压缩数据并重新创建条目标题。

If no compressedData was specified, reading this property will compress the data and create the required headers.如果未指定 compressedData,读取此属性将压缩数据并创建所需的标头。

The output of the compressedData Buffer contains the LOC Data Header压缩数据缓冲区的 output 包含 LOC 数据 Header

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM