Reputation: 1
i am not good at english,but i have a question about use jszip. the code like this
for (var i = 0; i < files.length; i++) {
compressFiles(files[i], compressName);
}
function compressFiles(file,compressName) {
var fileContent = file.file;
var fileName = file.name;
var zip = new JSZip();
zip.file(fileName, fileContent);
zip.generateAsync({ type: "blob" }).then(function (content) {
saveAs(content, compressName);
});
}
my question is when my file in files are very big ,about 88m. some file compress are about 0m i guess the reason is async,the loop put file stream into memory one by one,if the memory is full,compress filed. so who can tell me the real reason?thank you!
Upvotes: 0
Views: 1790
Reputation: 11
You can use an array to keep the zip.file calls and jQuery.when to wait for all of them to finish, and then call zip.generateAsync. Sample code below.
Helpful links:
jQuery.when
What does $.when.apply($, someArray) do?
function zipFiles(images) {
var zip = new JSZip();
deferreds = [];
images.forEach(function(image) {
deferreds.push(zip.file(image.name, image.data, { binary: true }));
});
$.when.apply($, deferreds).then(function() {
zip.generateAsync({ type: "blob" }).then(function(content) {
saveAs(content, 'Filename.zip'); //FileSaver.js
});
});
}
Upvotes: 1