0

I want to zip images using JSZip and NodeJS but it doesn't work, it works with simple file like .txt ... But with images it doesn't work and I don't know why...

My code :

var newFileName = pathDir + '/' + id + '.jpg';
fs.readFile(newFileName, function(err, data) {
    zip.file(id+'.jpg', data, {base64: true});
});
4

1 回答 1

2

Try:

var newFileName = pathDir + '/' + id + '.jpg';
var data = fs.readFileSync(newFileName);
zip.file(id+'.jpg', data, {base64: true});

In your case, you overwrite the id.jpg file of your zip instance using chunk data again and again...

    // create a file
zip.file("hello.txt", "Hello[p my)6cxsw2q");
// oops, cat on keyboard. Fixing !
zip.file("hello.txt", "Hello World\n");

The content of hello.txt is "Hello World\n" rather than "Hello[p my)6cxsw2qHello World\n". Hope it helps.

于 2014-11-21T02:31:39.840 回答