16

I am trying to pack files into a zip file using Adm-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.

Screenshot

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

shashi
  • 4,616
  • 9
  • 50
  • 77

3 Answers3

16

So I did some digging: 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. 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(filePathInArchive, fileBuffer, '', 0644 << 16);

To get a fileBuffer yourself, you can use fs.readFile or fs.readFileSync

Josh
  • 100
  • 6
Speedy
  • 326
  • 2
  • 7
2
var zip = new admZip();
var fs=require('fs-extra');
zip.addFile('NGINX/app.js',fs.readFileSync('./app.js'),'',0644);
zip.writeZip("./files.zip");
Andrew Rayan
  • 350
  • 1
  • 7
1

From the wiki of adm-zip:

[void] addLocalFile(String localPath, String zipPath)

Adds a file from the disk to the archive.

[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.

sstauross
  • 2,602
  • 2
  • 30
  • 50
  • 3
    no, I can confirm the OP claim. addLocalFile adds the file as a directory with or without the 2nd argument. I also note that addFile does the same unless you use 0644 << 16 as the 4th parameter. – Astra Bear Sep 10 '16 at 05:32