Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js jszip library to extract

I am writing some node code, and using jszip to zip and unzip some files. I know how to zip, but cannot figure out how to unzip, or decompress. There are a couple of links on stackoverflow that do not work. Anyone has any solution? Following is what I have tried

var fs = require('fs');
var JSZip   = require('jszip');
var zipName = "C:/test.zip";
var unzip = "C:/unzip";


fs.readFile(zipName, function (err, data) {
    if (err) throw err;
    var zip = new JSZip();
    zip.folder(unzip).load(data);
});
like image 408
user2689782 Avatar asked Mar 13 '15 18:03

user2689782


People also ask

How do I read a zip file in node JS?

Inside the loop, you convert the object into a string that represents the object using the Node. js toString() method, then log it in the console using the console. log() method. Finally, you invoke the readZipArchive() function with the ZIP archive file path as an argument.

What is JSZip used for?

JSZip is a javascript library for creating, reading and editing . zip files, with a lovely and simple API. License : JSZip is dual-licensed. You may use it under the MIT license or the GPLv3 license.


1 Answers

JSZip has no method to write files on the disk. To do it, you need to iterate over zip.files :

var path = require("path");
Object.keys(zip.files).forEach(function(filename) {
  var content = zip.files[filename].asNodeBuffer();
  var dest = path.join(unzip, filename);
  fs.writeFileSync(dest, content);
}

Inside a zip file, folders are represented with a forward slash '/', I think that path.join() will create a correct path but I can't test this.

like image 89
David Duponchel Avatar answered Sep 20 '22 11:09

David Duponchel