Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adm Zip zipping files as directories

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

like image 377
shashi Avatar asked Oct 23 '15 06:10

shashi


People also ask

Can zip files contain directories?

A Zip file is a data container containing one or more compressed files or directories. Compressed (zipped) files take up less disk space and can be transferred from one to another machine more quickly than uncompressed files.

Can I zip a folder and subfolders?

Creating a zip folder allows files to be organized and compressed to a smaller file size for distribution or saving space. Zip folder can have subfolders within this main folder.

How do I change a zip folder to a regular folder?

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.

What is a zipped directory?

Compressed (zipped) Folders overview. Folders that are compressed using the Compressed (zipped) Folders feature use less drive space and can be transferred to other computers more quickly. You can work with a compressed folder and the files or programs it contains just as you would an uncompressed folder.


1 Answers

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

like image 155
Speedy Avatar answered Oct 10 '22 19:10

Speedy