zip.js除了能读取还能写入zip,这里只记录读取文件名的操作
- 正常使用npm下载到项目里
- 如果直接使用import * as JSZip from “@zip.js/zip.js”;在当前(2.4.5)版本会报错
- 这里如下引入
const JSZip = require("@zip.js/zip.js/dist/zip.min.js");
- 测试方法
// 获取zip内文件名
export async function getZIPFileNames(file: Blob | File | string): Promise<any> {
let reader: any;
if (typeof file === "string") {
reader = new JSZip.ZipReader(new JSZip.HttpReader(file, { useXHR: false }));
} else if (file instanceof Blob) {
reader = new JSZip.ZipReader(new JSZip.BlobReader(file));
}
const entries: any = await reader.getEntries();
reader.close();
return entries.map((entry: any) => entry.filename);
}