daraosn / node-zip

217 stars 38 forks source link

Unable to write to a file #1

Closed ticup closed 12 years ago

ticup commented 12 years ago

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.

daraosn commented 12 years ago

Use this instead:

var data = zip.generate({base64:false,compression:'DEFLATE'});
ticup commented 12 years ago

tried that, doesn't fix it.

daraosn commented 12 years ago

Ok, thanks for reporting. Found two solutions for this:

1) Save using base64 encoding:

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, 'base64');

2) Save using binary encoding:

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

It worked for me this time. Let me know how it goes.

ticup commented 12 years ago

Both solutions do indeed work!

Thanks for the quick fix, maybe add it to the readme?

cheers.

daraosn commented 12 years ago

Definitely, thanks for reporting.

Cheers.