jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Zipping paused when other tabs are open

Open abkn9977 opened this issue 4 years ago • 11 comments

I want to use JSZip to zip max 1000 images from an array of urls. The max size of images could be 5 GB. Till now I have just zipped 2 GB. I am facing two problems :

  1. When I open new tab on browser or work on other software(maximized to full screen) then zipping pauses and as I go to that tab it resumes again.
  2. When I tested it with 1000 urls, sized 1.9 GB, it zipped normally but when it came to download my browser (chrome ) was not responding and my PC was slowed down too. Though it downloaded zip file after a while. It is not the case when I zip small sizes.

Here is what code I am using

var zip = new JSZip();
var count = 0;

var imageUrl= [
  'https://image-url-1',
  'https://image-url-2',
  'https://image-url-3'
];

imageUrl.forEach(function(image){
  var filename = "filename";
  // loading a file and add it in a zip file
  JSZipUtils.getBinaryContent(image, function (err, data) {
     if(err) {
        throw err; // or handle the error
     }
     zip.file(filename, data, {binary:true});
     count++;
     if (count == imageUrl.length) {
       zip.generateAsync({type:'blob'}).then(function(content) {
          saveAs(content, "ZippedImages.zip");
       });
    }
  });
});

abkn9977 avatar Jan 31 '21 16:01 abkn9977

Related to JSZip v3.5.0. Humm I have a different behaviour to report. JSZip doesn't download if the tab is not in the foreground. As soon as I bring the tab to the focus/foreground it starts downloading the zip. Having other tab opened doesn't create any problem.

miniGweek avatar Feb 08 '21 09:02 miniGweek

Related to JSZip v3.5.0. Humm I have a different behaviour to report. JSZip doesn't download if the tab is not in the foreground. As soon as I bring the tab to the focus/foreground it starts downloading the zip. Having other tab opened doesn't create any problem.

I actually wanted to say this when I said "working on other software"

abkn9977 avatar Feb 08 '21 11:02 abkn9977

@miniGweek I see a similar problem (zip doesn't work when browser is not in foreground) but it occurs in 3.2.0 That is: jszip version 3.1.5 does not show the problem, version 3.2.0 does. I suspect it has something to do with the web.immediate module being removed. However, I have no idea what that module is.

dteviot avatar Sep 26 '21 01:09 dteviot

@miniGweek

I see a similar problem (zip doesn't work when browser is not in foreground) but it occurs in 3.2.0

That is: jszip version 3.1.5 does not show the problem, version 3.2.0 does.

I suspect it has something to do with the web.immediate module being removed. However, I have no idea what that module is.

ok thanks. will try that out!

miniGweek avatar Sep 26 '21 01:09 miniGweek

@miniGweek, @stuk More information, I think web.immediate is provides a stub for https://developer.mozilla.org/en-US/docs/Web/API/Window/setImmediate. According to docs "This method is used to break up long running operations and run a callback function immediately after the browser has completed other operations such as events and display updates".

Anyway, with version 3.7.1 if I change https://github.com/Stuk/jszip/blob/3f2f0da8b29c2477bca982911caa8392679c13b2/lib/utils.js#L379-L383

to this (i.e. Ignore setImmediate, because in my context it's already running a Promise)

exports.delay = function(callback, args, self) {
    callback.apply(self || null, args || []);
};

it works for me

dteviot avatar Sep 26 '21 02:09 dteviot

@miniGweek @abkn9977 Workaround I was given by @Synteresis, is to polyfill setImmediate() before loading the jszip library e.g.

window.setImmediate = (fn) => {fn();};

It works in my case, because I'm already calling jszip inside a Promise.

dteviot avatar Nov 18 '21 10:11 dteviot

I am still facing that issue. Zipping pauses when I switch to another tab.

sakibul-islam avatar Mar 07 '22 15:03 sakibul-islam

is there any solution?

liudaohui404 avatar Nov 07 '23 07:11 liudaohui404

@liudaohui404 As I said above, polyfill setImmediate() works, provided you're already calling jszip inside a Promise.

dteviot avatar Nov 07 '23 08:11 dteviot

@liudaohui404 As I said above, polyfill setImmediate() works, provided you're already calling jszip inside a Promise.

I apologize for the confusion. I forgot to mention my usage environment. I am using jszip in a Node environment. When my app is minimized, I cannot unzip files, but it works fine when the window is restored. Is this situation similar to yours?

liudaohui404 avatar Nov 07 '23 09:11 liudaohui404

@liudaohui404 I'm running in a Chrome extension. So, somewhat similar. You could try the pollyfill approach.

dteviot avatar Nov 07 '23 09:11 dteviot