Giter Site home page Giter Site logo

node-zip's Introduction

node-zip

node-zip - Zip/Unzip files ported from JSZip

Installation

npm install node-zip

Usage

Complete example, zip multiple files

var fs = require('fs');
var path = require('path');

// The zip library needs to be instantiated:
var zip = new require('node-zip')();

// You can add multiple files by performing subsequent calls to zip.file();
// the first argument is how you want the file to be named inside your zip,
// the second is the actual data:
zip.file('file1.txt', fs.readFileSync(path.join(__dirname, 'file1.txt')));
zip.file('file2.txt', fs.readFileSync(path.join(__dirname, 'file2.txt')));

var data = zip.generate({ base64:false, compression: 'DEFLATE' });

// it's important to use *binary* encode
fs.writeFileSync('test.zip', data, 'binary');

You can also load directly:

require('node-zip');
var zip = new JSZip(data, options)
    ...

Zip text into file:

var zip = new require('node-zip')();

zip.file('test.file', 'hello there');
var data = zip.generate({base64:false,compression:'DEFLATE'});
console.log(data); // ugly data

Unzip:

var zip = new require('node-zip')(data, {base64: false, checkCRC32: true});
console.log(zip.files['test.file']); // hello there

Write to a file (IMPORTANT: use binary encode, thanks to @Acek)

var fs = require("fs");
zip.file('test.txt', 'hello there');
var data = zip.generate({base64:false,compression:'DEFLATE'});
fs.writeFileSync('test.zip', data, 'binary');

Testing

npm install -g jasmine-node
jasmine-node test

Manual

node-zip uses JSZip, please refer to their website for further information: http://stuartk.com/jszip/

Contributors

David Duponchel @dduponchel

Feel free to send your pull requests and contribute to this project

License

MIT

node-zip's People

Contributors

daraosn avatar dduponchel avatar kemitchell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

node-zip's Issues

ZIP Contents of Lambda TMP Directory - Files are 0,1,2,3 Folders Deep

Hi everyone.

Thank you in advance for all your ideas and suggestions.

We copy 10MB (approx 2,000 files) from the pre-installed "files" folder in Lambda to its /tmp directory. We then download dozens of files from S3 to the same /tmp directory.

We have a hierarchy of multiple folders. Some files are in root. Some inside a folder. Some inside a folder inside a folder etc.

We don't know the file names in advance or their position.

We have a maximum of 3 folders deep.

Our goal is to zip up the contents of the /tmp directory to create an Archive.zip file ready to be uploaded to S3.

By carrying the majority of files locally we save the time and bandwidth of downloading over 2,000 files each time we run the function.

We have worked with a number of node modules but they don't seem capable of processing this multi level hierarchy of files.

Any help would be much appreciated.

Thank you.

Incorrectly zip image

I can zip image successful, but when i unzip, the output image is incorrectly(the file size almost twice bigger), and i can't open output image anymore.

zip file obtained cant be opened

I'm using node-zip package in AWS Lambda function to serve up a couple of files from S3 bucket to client via API/GW.

API Call via Postman is returning data however when I save that data to a .zip file and try to extract I'm getting told its not a valid zip file.

Below is example of code without all the S3 stuff as its not relevant

const zip = require("node-zip")();

// gets files from S3 OK then zips them up

zip.file(certFilePath, getCRTResponse);

zip.file(privateKeyPath, getPrivateKeyResponse);

const data = zip.generate({ base64: false, compression: "DEFLATE" });

responseBody = {
    statusCode: 200,
    headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "GET, POST",
        "Content-type": "application/zip",
        "Content-Disposition": "attachment; filename='" + certPath[3] + "-" + event.pathParameters.serial + ".zip'"
    },
    body: data
};

callback(null, responseBody);

request for adding feature

I use node-zip to create zip file for my game hotfix patch . but I meet a problem that md5 change every time after zip file create by node-zip with same files.

as zip document mention

-X --no-extra Do not save extra file attributes (Extended Attributes on OS/2, uid/gid and file times on Unix). The zip format uses extra fields to include additional information for each entry. Some extra fields are specific to particular systems while others are applicable to all systems. Normally when zip reads entries from an existing archive, it reads the extra fields it knows, strips the rest, and adds the extra fields applica- ble to that system. With -X, zip strips all old fields and only includes the Unicode and Zip64 extra fields (currently these two extra fields cannot be disabled).
does node-zip has the same option as '-X' ?

PDF file corrupted when zipped and downloaded

I'm using PDFKit to generate a PDF file. I want to zip it into a folder together with an excel file. When I download the zip file, I'm getting a 0kb pdf file. The excel file works fine though.

const zip = new require("node-zip")();
zip.file(pdfFilePath, fs.readFileSync(`tmp/${pdfFilePath}`));
const zipData = zip.generate({ base64: false, compression: "DEFLATE" });
const zipFilePath = `tmp/myzipfile.zip`;
fs.writeFileSync(zipFilePath, zipData, "binary");

Any suggestions?

Is it possible to create a zip from an existing file/folder structure?

Hello, thanks a lot for the lib.

Is it possible to create a zip from existing file/folder structures?

I would like to do something like:

const fs = require("fs");
const Zip = require('node-zip');
const zip = new Zip();

zip.folder('./src/existing-folder-with-files-and-subfolders');
var data = zip.generate({base64: false, compression: 'DEFLATE'});
fs.writeFileSync('./output/zipped-files-exactly-as-they-appear-in-filesystem.zip', data, 'binary');

Instead of creating new files and folders from within the zip utility. Is this possible?

Unable to write to a file

Hi,

I'm trying to write the zip to a file, but its unable to open the written file properly, saying: "the file is corrupted or is of unsupported format".

var fs = require("fs");
var zip = new require('node-zip')();
zip.file('test.txt', 'hello there');
var data = zip.generate({base64:true,compression:'DEFLATE'});
fs.writeFile('test.zip', data);

Using node 0.6.6 and 0.8.11 on OSX, tried opening with Zipeg and Archive Utility.

Thanks in advance.

JSZip 2.1.1

First of all, thanks for this module :)

The version 2.1.1 of JSZip is out and is now on npm.
If you whish, I can create a pull request for the update.
If you don't have the time for this module anymore, @Stuk and I are willing to help :)

Unable to clear Cache from ZIP

Hello Team,

I wrote the below piece of code to zip a file and then delete once the same is done.
At first instance it works fine , and create the zip.
But for 2nd instance it zips the new new file and also add the previous file to the bundle , so now in my zip i have 2 file.
Please note i am also deleting the files physically from disk.
Can you please confim on this if this a bug / or how can clear the cache / in-memory of ZIP before being called again.

  const zip = require('node-zip')();
  zip.file(`${resultFileName}`, fs.readFileSync(resultFilePath));
  const data = zip.generate({ base64: false, compression: 'DEFLATE' });
  fs.writeFileSync(path.join(resultDirectory, `${date}.zip`), data, 'binary');

     if (fs.existsSync(resultFilePath)) {
          fs.unlinkSync(resultFilePath);
        }
        if (fs.existsSync(path.join(resultDirectory, `${date}.zip`))) {
          fs.unlinkSync(path.join(resultDirectory, `${date}.zip`));
        }

Zipping a .tgz file corrupts the file

If I run yarn pack (which outputs a .tgz), then read in the the resulting file
let content = fs.readFileSync("package.tgz", "utf8")
then zip it
zip.file("package.tgz", content)
, trying to unzip the resulting file once the overall package is unzipped results in a corrupted .tgz whichtar -xzf is unable to unpack.

process out of memory

Hi,

I'm processing some big files and I'm getting a "process out of memory" error.

 - generating zip
 - FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - process out of memory
 - <--- Last few GCs --->
 -   166977 ms: Mark-sweep 181.8 (349.4) -> 164.6 (338.4) MB, 39.8 / 0 ms (+ 110.6 ms in 280 steps since start of marking, biggest step 9.0 ms) [allocation failure] [GC in old space requested].
 -   167072 ms: Mark-sweep 164.6 (442.5) -> 130.6 (382.5) MB, 95.1 / 0 ms [allocation failure] [GC in old space requested].
 -   167153 ms: Mark-sweep 130.6 (486.7) -> 129.8 (483.7) MB, 80.8 / 0 ms [last resort gc].
 -   167214 ms: Mark-sweep 129.8 (483.7) -> 128.6 (483.7) MB, 61.0 / 0 ms [last resort gc].
 - <--- JS stacktrace --->
 - ==== JS stack trace =========================================
 - Security context: 0x3c5bdb2b4629 <JS Object>
 -     1: Join(aka Join) [native array.js:133] [pc=0x167ded90d3a7] (this=0x3c5bdb2041b9 <undefined>,o=0x241786009bf9 <JS Array[250]>,v=250,C=0x3c5bdb204291 <String[0]: >,B=0x3c5bdb295289 <JS Function ConvertToString (SharedFunctionInfo 0x3c5bdb24a029)>)
 -     2: InnerArrayJoin(aka InnerArrayJoin) [native array.js:331] [pc=0x167dedd83f6a] (this=0x3c5bdb2041b9 <undefined>,C=0x3c5bdb204291 <String[0]: ...
 - error: Forever detected script was killed by signal: SIGABRT

Obviously, I'm out of memory because I'm trying to zip too much :-) Can the module be told to write directly to disk (stream)?

Thanks,
Sam

Question - How is this different from JSZip?

I'm using this module in my current project and I'm not having any issues. I just wonder how it is different from the original JSZip. The readme tells it is based on JSZip but not what is the difference or why I should use this instead of the original JSZip.

zip image from an url

Hello,
How can I fetch the data of an image from facebook via an URL (I already got the URL) and zip the file to my local storage? Any suggestion would be very helpful.

How to download binary?

var fs = require("fs");
zip.file("test.txt", "hello there");
var data = zip.generate({base64:false,compression:"DEFLATE"});
fs.writeFileSync("test.zip", data, "binary");

using FileSaver.js

saveAs(data,"Test.zip");
or
saveAs(new Blob([data]),"Test.zip");

does not work

Issue with unzip

Hello everyone I'm having some issues with unziping. I can get the folder structure however I can't seem to get the files to write to disk. The zip is being uploaded to the server and I'm processing it like this

var unzip = require("unzip");

var path = __dirname + "/uploads/powerPoints/" + name.slice(0, -4);
    var unziped = unzip.Extract({ path: path });
    var zip = file.pipe(unzip.Parse());

    zip.on('entry', function (entry) {
        console.log("entry");
        console.log(entry.path);
        entry.autodrain();
    });

    zip.on('error', function(err) {
        deferred.reject(err);
    });

    zip.on('finish', function(){
        deferred.resolve(file);
    });

I created the zip on my mac. All I have is a folder with an index.html file with a little bit of text. When it writes to disk all I get is the folder without the index.html file. Any idea?

Corrupted zip produced on specific text data on Mac OS X 10.8.5

First of all - awesome module.

The problem I have is with specific text data. I have isolated the scenario to a file that after zipping couldn't fully be unzipped.

This worked on Mac OS X 10.8.4 (there wasn't any problem), but this is no longer the case on 10.8.5 (where node-zip started producing corrupted zip on the same data).

I have made a reproducible sample with the data which can't be zipped properly.

To run it, copy the 2 files from this gist: https://gist.github.com/nicroto/6652540 like this:

sample
---bin
-------zip-test
---resources
-------data.txt

If you don't have node-zip globally installed, add a node_modules dir with the node-zip module in it (I guess npm install will work even without package.json).

Then navigate in the terminal to the bin dir and execute the following commands:

$ ./zip-test
$ unzip test.zip

At this point search for an error in the terminal and diff the original file with the produced from unzip.

Regards,
Nikolay Tsenkov

Compression failure

I tried to compress a file I have generated on disk, and it compresses without errors.

But when I try to extract it again node-zip throws a nasty error.If I write the zip to file then my WinRaR also won't extract it so it looks like the file corrupts in compression.

I'm on Windows Vista 64, running Node.js v0.10.10

I made a failing test for it here:

https://github.com/Bartvds/node-zip/blob/error/extraction_error/test/nodezip_spec.js#L42

Failures:

  1) nodezip when archiving a read file should be able to compress and extract content
   Message:
     Error: Bug : uncompressed data size mismatch
   Stacktrace:
     Error: Bug : uncompressed data size mismatch
    at Object.ZipEntry.readLocalPart (jszip/jszip-load.js:225:19)
    at Object.ZipEntries.readLocalFiles (jszip/jszip-load.js:425:18)
    at Object.ZipEntries.load (jszip/jszip-load.js:509:15)
    at Object.ZipEntries (jszip/jszip-load.js:341:15)
    at Object.JSZip.load (jszip/jszip-load.js:528:20)
    at Object.JSZip (jszip/jszip.js:38:12)
    at null.<anonymous> (D:\_Editing\github\node-zip\test\nodezip_spec.js:56:13)
    at jasmine.Block.execute (C:\Users\Bart\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node/jasmine-1.3.1.js:1064:17)
    at jasmine.Queue.next_ (C:\Users\Bart\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node/jasmine-1.3.1.js:2096:31)
    at jasmine.Queue.start (C:\Users\Bart\AppData\Roaming\npm\node_modules\jasmine-node\lib\jasmine-node/jasmine-1.3.1.js:2049:8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.