【发布时间】:2020-12-29 17:04:53
【问题描述】:
我正在测试fflate 以替换有时无法打开使用 macOS 或 Windows 内置压缩实用程序创建的档案的 JSZip。该库工作正常,我能够创建和读取 zip 文件内容。我已经对使用 macOS 创建的存档进行了测试,我注意到在存档内部,压缩实用程序将为存档的每个条目创建一些 __MACOSX/_.filename.ext 文件。当用户使用我的应用程序打开 zip 文件时,我想从文件列表中删除这些文件,但我不知道如何继续。有没有我可以使用 vue 和 javascript 来实现这一点的解决方案?
这是我在我的 vue 方法中用来读取 zip 文件的代码
async handleFiles(e) {
const file = await this.readFile(e.dataTransfer.files[0]);
const unzip = unzipSync(file);
Object.keys(unzip).forEach( (key) => {
this.files.push({name: key, data: unzip[key]});
});
},
readFile(file) {
return new Promise( (resolve,reject) => {
const reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onerror = reject;
reader.onloadend = () => {
resolve(new Uint8Array(reader.result));
}
});
}
【问题讨论】:
-
这是 Finder 内置 ZIP 实用程序 (superuser.com/questions/104500/what-is-macosx-folder) 的一个众所周知的“功能”。检查
!key.startsWith('__MACOSX/')进行过滤是最好的解决方案。旁注,您可能希望使用异步fflate.unzip进行并行化:github.com/101arrowz/fflate/blob/master/docs/README.md#unzip
标签: javascript vue.js zip