jszip
jszip copied to clipboard
Unable to read zip archives created using macOS
I've implemented this library inside a vue project. All seems working fine if the archive are created using JSZip but I'm not able to read the content of archives that are created using macOS. I will only get one file from the archive and into console this error will be showed Uncaught (in promise) TypeError: Cannot read property 'async' of null.
It's related to the code I'm using to read the zip files content
let file = e.dataTransfer.files[0];
JSZip.loadAsync(file).then( (zip) => {
console.log(zip);
Object.keys(zip.files).forEach( (filename) => {
zip.file(filename).async('blob').then( (blob) => {
this.files.push({name: filename, data: blob});
});
});
});
Also during the debug I've seen that the zip files created using macOS built in compression utility will have a __MACOSX/ object for each file that is into the archive. Is there a solution for this?
This is a Mac thing. JSZip is functioning correctly.
The files are added by macOS, and will appear anywhere that the zip file is decompressed, except on a Mac, where the __MACOSX/ directory remains hidden, even if you enable Show Hidden Files. It's better to use the zip utility from the command line instead.
The following command will create a zip file named app.zip by compressing the current working directory, including every file in the directory, but without introducing anything new (like the __MACOSX stuff):
zip -r app .
Note: The resulting archive (app.zip) will be written to the current working directory once it's created.
If you want to exclude hidden files (and directories), use the -x flag (for exclude) with a list of regexs that match anything starting with a dot (each wrapped in quotes to prevent expansion):
zip -r data . -x ".*" "*/.*"
Or you can edit the regexs to just exclude the .DS_Store files:
zip -r data . -x ".DS_Store" "*/.DS_Store"
The suggestion to add options such as -x or whatever else are only applicable to the case when the archive is being created via a terminal. Unfortunately, the user can simply select three files in the file explorer, right click, and select "compress". This runs default archiving logic, which results in __MACOSX stuff being appended.
User can't do much about it and then, when the unzipping logic runs, it fails to unzip because it does not account for this edge-case. In my opinion this should be accounted for in the library. The devs will have easier time with the library.
What's interesting is that the files without the prefix are also end up being included. So the simplest thing is to just ignore all files that start with __MACOSX.
Hey, my issue is I cannot target specific folders to parse on archives created by the macOS and not sure how to make this work...