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);
});
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With