简体   繁体   English

Adm Zip将文件压缩为目录

[英]Adm Zip zipping files as directories

I am trying to pack files into a zip file using Adm-Zip 我正在尝试使用Adm-Zip将文件打包到zip文件中

var AdmZip = require('adm-zip');

var pathToZip = 'build/release/Ext.zip';


var zip = new AdmZip();

zip.addLocalFile('background.js');
zip.addLocalFile('chrome_ex_oauth.html');
zip.addLocalFolder('images');
zip.writeZip(pathToZip);

However, all the files are getting added as folders inside the zip and the actual content is not getting zipped. 但是,所有文件都将作为文件夹添加到zip中,并且实际内容不会被压缩。

截图

The Getting Started reference is below and this seems to be a very simple example which is not working as expected. 入门参考如下,这似乎是一个非常简单的例子,它没有按预期工作。 What am I doing wrong? 我究竟做错了什么? https://github.com/cthackers/adm-zip/wiki/ADM-ZIP-Introduction https://github.com/cthackers/adm-zip/wiki/ADM-ZIP-Introduction

So I did some digging: https://github.com/cthackers/adm-zip/blob/master/adm-zip.js#L275 所以我做了一些挖掘: https//github.com/cthackers/adm-zip/blob/master/adm-zip.js#L275

addFile is ultimately called by addLocalFile, and that seems to be where the error is occurring, specifically on line 281 where it checks if the ZipEntry is a directory. addFile最终由addLocalFile调用,这似乎是发生错误的地方,特别是在第281行 ,它检查ZipEntry是否是一个目录。 The wrong flags are getting applied. 正在应用错误的标志。

To get around this, I ended up calling addFile manually and specified the attributes myself, so that it wouldn't rely on auto-detection and incorrectly flag files as directories. 为了解决这个问题,我最终手动调用了addFile并自己指定了属性,因此它不依赖于自动检测并错误地将文件标记为目录。

addFile(filePathInArchive, fileBuffer, '', 0644 << 16);

To get a fileBuffer yourself, you can use fs.readFile or fs.readFileSync 要自己获取fileBuffer,可以使用fs.readFilefs.readFileSync

var zip = new admZip();
var fs=require('fs-extra');
zip.addFile('NGINX/app.js',fs.readFileSync('./app.js'),'',0644);
zip.writeZip("./files.zip");

From the wiki of adm-zip: 来自adm-zip的wiki

[void] addLocalFile(String localPath, String zipPath) [void] addLocalFile(String localPath,String zipPath)

Adds a file from the disk to the archive. 将文件从磁盘添加到存档。

[void] addLocalFolder(String localPath, String zipPath) [void] addLocalFolder(String localPath,String zipPath)

Adds a local directory and all its nested files and directories to the archive 将本地目录及其所有嵌套文件和目录添加到存档

As it seems you miss the second parameter that is the zipPath. 因为看起来你错过了第二个参数,即zipPath。

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

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