jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Corrupted zip file

Open MentalGear opened this issue 8 years ago • 3 comments

First off, great extension. Second, I have an issue with the implementation. Everything works fine without errors, but the resulting .zip file is corrupted on open (.cpgz).

I already checked the FAQ, tried unzip and added the correct DEFLATE compression, but it seems the zip is either not properly transferred or not properly generated.

I spend the better part of the evening on it, and would appreciate it if someone more knowledgeable than I could take a look and make an educated guess.

Here's my code (yes, the image file exists)

  		fetch("/images/favicons/favicon-32x32.png")       // 1) fetch the url
  			.then(function (response) {                       // 2) filter on 200 OK
  				if (response.status === 200 || response.status === 0) {
  					console.log("fetch", response);
  					return Promise.resolve( response.blob() );
  				} else {
  					return Promise.reject( new Error(response.statusText) );
  				}
  			})
  			.then( JSZip.generateAsync )                       // 3) chain with the zip promise
  			.then(function ( fetchedData ) {
  				console.log("fetched & passed", fetchedData);
  				return zip.file( "picture.png", fetchedData );
  				// return zip.file("Hello.txt").async("string"); // 4) chain with the text content promise
  			})
  			.then(function success( returnedData ) {                    // 5) display the result
  				console.log("returnedData", returnedData);
  				return zip.generateAsync( {type:"blob", compression: "DEFLATE" } ) 
  					.then(
  						function ( blob ) {
  							// init download
  							var DownloadLink = document.createElement('a');
  							DownloadLink.href = blob;
  							DownloadLink.download = "pics.zip";
  							document.body.appendChild(DownloadLink);
  							DownloadLink.click()
  						},
  						function (err) {
  							//- $("#blob").text(err);
  							alert(err);
  						}
  					);
  			}, function error(err) {
  				alert(err);
  			});

MentalGear avatar Jan 20 '18 20:01 MentalGear

What happens if you remove .then( JSZip.generateAsync )? You're generating the zip file later, I don't think that line needs to be there.

And I think with promises you have to add a .catch() somewhere, but I'm not so sure.

In your last .then(), do you need to return the generated zip? There's no more then()s for it to go to.

Hopefully you've already fixed it or that points you in some direction. Post the question with updated code on stack overflow if there's still problems

turquoise-turtle avatar May 08 '18 06:05 turquoise-turtle

Actually, sorry, instead of .then( JSZip.generateAsync ) what if you try .then( JSZip.loadAsync )?

turquoise-turtle avatar May 20 '18 10:05 turquoise-turtle

Is there any new progress?

PanJiaChen avatar Sep 27 '19 07:09 PanJiaChen