adm-zip
A Javascript implementation of zip for nodejs. Allows user to create or extract zip files both in memory or to/from disk
I have a built a method in ExpressJS that exports a document as an HTML page:
html: function (req, res) {
Project.findOne( { req.params.project },
function (err, project) {
res.contentType('text/html');
res.render('exporting/html', { project.name });
}
);
},
Additionally, I'd like to create a method that includes that generated HTML page, together with some static assets, in a ZIP archive.
Here's my current code:
zip: function (req, res) {
Project.findOne( { req.params.project },
function (err, project) {
res.contentType('application/zip');
res.setHeader('content-disposition', 'attachment; filename=' + project.name + '.zip');
var zip = new AdmZip();
zip.addFile("readme.txt", new Buffer("This was inside the ZIP!"));
//------ Here I'd like to use zip.addFile to include the HTML output from the html method above ------
res.send(zip.toBuffer());
}
);
}
How can I make the zip
method include the output from the html
method?
Source: (StackOverflow)
im trying to unzip a File with NodeJS and Express. I tried ADM-Zip.
I typed:
npm install adm-zip
and got:
npm WARN package.json express-namespace@0.1.1 No repository field.
Everything else went fine.
My Code is:
var AdmZip = require('adm-zip');
var zip = new AdmZip("/scenario/file/05ae5db61e83b7611570f2a313f778dd1c8-7c32.zip");
zip.extractAllTo("scenario/file/", true);
The zip-File is in my public Folder (Express). All I got is an console output with the text "undefined".
I'm a node newbie and have no idea what I should try.
It would be nice if someone could help me
Source: (StackOverflow)
As you can guess from the title, I am trying to write a small thing just for the sake of learning.
Here's my code in a Pastebin
As you can see, I am trying to grab a zipped repo from github, according to the express request's parameters, then serve it to the client. Originally I just piped the https response to the client's response, but the zipped archive has a top-level folder (as it comes from github) that I am trying to remove. I'm pretty new to this, and quite lost. Any advice on how do go about modifying the zip archive in memory?
Edit: I tried creating a separate archive, then iterating through each of the zipEntries and trying to add it to the new archive, and all of the files do end up on top, like I was aiming for, but they all appear as folders in winrar.
Here is the second attempt
Source: (StackOverflow)
im downloading a zip file from the internet. I recieve it using a XHR request (using node-webkit) and this means that the content of the zip comes as a string in xhr.responseText. I now want to save this file to the disk, however, i cant seem to get it saved as a noncurrupted zip archive.
I have basically used fs.writeFile, fs.write, fs.createWriteStream, but I cant seem to get it right.
I am using a node module named AdmZip which accepts a file buffer that then can be saved as a zip archive. So, I guess, this could be one way to go, but how to I make a buffer out the the string that i recieve?
btw: i can't use the http module to recieve the file from the internet due to a bug in node.js, therefore im using the xhr request.
Source: (StackOverflow)
I'm trying to create a .zip
file from a JSON object in Node.js. I'm using adm-zip to do that however I'm unable to make it work with this code:
var admZip = require('adm-zip');
var zip = new admZip();
zip.addFile(Date.now() + '.json', new Buffer(JSON.stringify(jsonObject));
var willSendthis = zip.toBuffer();
fs.writeFileSync('./example.zip', willSendthis);
This code creates example.zip
but I'm not able to extract it, I tried with a .zip
extractor but also with this code:
var admZip = require('adm-zip');
var zip = new admZip("./example.zip");
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function(zipEntry) {
console.log(zipEntry.data.toString('utf8'));
});
It returns Cannot read property 'toString' of undefined
at the line with console.log
.
I could use zip.writeZip()
for this example but I'm sending the .zip
file to Amazon S3 thus I need to use the method .toBuffer()
to do something like this after using adm-zip
:
var params = {Key: 'example.zip', Body: zip.toBuffer()};
s3bucket.upload(params, function(err, data) {...});
I don't see what is wrong, am I using the package correctly?
Source: (StackOverflow)
I'm trying to use the sqlite3
module to create an in memory SQLite database and the adm-zip
module to save it into a zip file. So far I have been able to create an in memory database and add data to it, but I have been unsuccessful in finding a way to store it in a zip made through adm-zip
as it requires either a file, buffer or string.
My question is this: does the sqlite3
module even support storing or saving as a buffer? If it doesn't then what would be an advisable solution for storing temporary files in Node.js when the script is used as both a requirable module and a command line script?
I've included the code I've been using to test with below and a cloneable gist.
main.js
var fs = require('fs'),
admzip = require('adm-zip'),
sqlite3 = require('sqlite3').verbose(),
zip = new admzip(),
db = new sqlite3.Database('test.sqlite');
// db = new sqlite3.Database(':memory:');
db.serialize(function() {
db.run('CREATE TABLE IF NOT EXISTS test(id INTEGER PRIMARY KEY, name TEXT);');
db.run('INSERT OR IGNORE INTO test(name) VALUES ("neogeek");');
});
zip.addFile('README.md', '#SQLite3 + ADM-ZIP Test');
// zip.addFile('db.sqlite', db);
db.close();
fs.writeFileSync('test.zip', zip.toBuffer(), 'utf8');
package.json
{
"private": true,
"dependencies": {
"sqlite3": "3.0.2",
"adm-zip": "0.4.4"
}
}
https://gist.github.com/neogeek/70c80c7ddaf998bee4bd
Source: (StackOverflow)
I use the adm-zip(Module NodeJs ) to zip Floder, with Method addLocalFolder().The operation end successfully, but when i try to unzip folder i get the Error 2 ' file or directory not exist!'as additional info i have the right size of zip when zip operation is end.
Thank you every one.
Source: (StackOverflow)
I'm using adm-zip for reading files and extraction of zip folder. But I've another requirement to read whole folder present inside zip file and copy to another location. Is it possible?
I saw there is boolean flag to detect folder. Any idea ?
Source: (StackOverflow)
I'm currently using adm-zip
to extract a zip file to a certain path but its extractAllTo
method is synchronous.
Is there a way I can extract zip files Asynchronously?
Source: (StackOverflow)
Im trying to read a zip file and then parse a json file at the root of the zip.
The json file is called manifest.json and will be called this in every zip file i read.
Currently i have the following function
function getFileContents(directory){
// reading archives
var zip = new AdmZip(directory);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function (zipEntry) {
if (zipEntry.entryName == "manifest.json") {
console.log('getData', zipEntry.getData());
console.log('data',zipEntry.data.toString('utf8'));
}
});
}
However i get the following exception in the console
getData <Buffer ff fe 7b 00 0a 00 20 00 20 00 22 00 62 00 75 00 69 00 6c 00 64 0
0 22 00 3a 00 20 00 22 00 34 00 2e 00 38 00 2e 00 37 00 32 00 31 00 39 00 22 00
2c 00 0a ...>
TypeError: Cannot call method 'toString' of undefined
at c:\direc\Custom_Modules\readZipFileModule\readZipFileModule.js:18:46
at Array.forEach (native)
Of the back of this ive tried:
function getFileContents(directory){
// reading archives
var zip = new AdmZip(directory);
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function (zipEntry) {
if (zipEntry.entryName == "manifest.json") {
var decompressedData = zip.readFile(zipEntry);
var data = zip.readAsText(zipEntry)
console.log(JSON.parse(data));
}
});
}
if i console.log data i get:
??{
" b u i l d " : " 4 . 8 . 7 2 1 9 " ,
" b r a n c h " : " s t e p h e n " ,
" t i m e s t a m p " : " 1 5 - 0 1 - 2 0 1 4 0 9 : 0 6 : 2 7 "
}
That is the correct data from the file however there are not spaces inbetween every character.
But when i try and parse it it obviously throws an error about '??' Where are the question marks coming from? I don't fully understand how to use adm-zip correctly what exactly am i doing wrong to read a json file from a zip in nodejs? It doesn't need to save the file just parse its data into an object.
Thanks for any and all assistance with this.
Source: (StackOverflow)
I am trying to download files from nseindia.com and unzip in memory. I am using nodejs webkit and adm-zip. I am getting error on console:
Uncaught Invalid or unsupported zip format. No END header found
Code:
var http = require('http'),
fs = require('fs'),
request = require('request'),
AdmZip = require('adm-zip'),
out = fs.createWriteStream('data/nseeqbhav.zip'); // For saving NSE Equity bhavcopy
// Downloading NSE Bhavcopy
request(
{ method: 'GET',
uri: 'http://www.nseindia.com/content/historical/EQUITIES/2012/DEC/cm19DEC2012bhav.csv.zip',
headers: { "User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.97 Safari/537.11",
"Referer": "http://www.nseindia.com/products/content/all_daily_reports.htm",
"Accept-Encoding": "gzip,deflate,sdch",
"encoding": "null",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Cookie": "cookie"
}
}
).pipe(out);
var zip = new AdmZip("data/nseeqbhav.zip"),
zipEntries = zip.getEntries();
zip.extractAllTo(/*target path*/"data/unzip/", /*overwrite*/true);
I tried following to end the stream but no success.
out.end();
out.destroy();
Thanks in advance.
Source: (StackOverflow)