tonymx227
tonymx227

Reputation: 5451

ZIP existing images using JSZip and NodeJS

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});
});

Upvotes: 0

Views: 1959

Answers (1)

JasmineOT
JasmineOT

Reputation: 2068

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.

Upvotes: 2

Related Questions