Zip local files using input
I am using angular ngUpload and I want to use the JSZip in the beforeModelChanged hook so the files get zipped as soon as they are selected for upload but nothing works. I have posted question here
$scope.beforeChange = function(files) { console.log(files); <-- files array from input.files[] after selecting files var zip = new JSZip(files[0]);
zip.generateAsync({type:"blob"})
.then(function (blob) {
// saveAs(blob, "hello.zip");
$scope.upload([blob]); // upload function of ngUpload
});
}
The constructor new JSZip(files[0]); has been removed in JSZip 3 and now throw an exception:
Error: The constructor with parameters has been removed in JSZip 3.0, please check the upgrade guide.
If I understand your use case correctly, you may want to create an empty JSZip object and add files inside it (the deprecated constructor has been replaced by JSZip.loadAsync).
For folks that updated (2.x -> 3.x) and relied on the JSZip constructor
My deps at the moment:
- jszip: 3.2.1
- @types/jszip: 3.1.6
Since the constructor was removed in 3.0, the only way to generate an empty JSZip object is to use the JSZip.loadAsync() method, and provide an empty Zip buffer as the contents of the "existing" archive.
For example:
const zip = await JSZip.loadAsync(
Buffer.from("UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA==", "base64"),
{ base64: true });
The UEsFBgAAAAAAAAAAAAAAAAAAAAAAAA== value is output by the Unix base64 command, when an empty Zip archive is passed to in a Terminal:
zip empty.zip someFile
zip -d empty.zip someFile
base64 empty.zip