Vue 文件压缩与解压缩
0. 安装
npm install jszip
import JSZip from 'jszip';
1. 压缩
async compressFiles() {
const zip = new JSZip();
const file1 = 'Hello World';
zip.file('file1.txt', file1);
const dir = zip.folder('dir');
dir.file('file2.txt', 'Hello Vue');
dir.file('file3.txt', 'Hello JavaScript');
const content = await zip.generateAsync({ type: 'blob' });
saveAs(content, 'example.zip');
}
2. 解压
async decompressFiles(file) {
const zip = new JSZip();
const zipFile = await zip.loadAsync(file);
const file1 = zipFile.file('file1.txt');
const content = await file1.async('string');
console.log(content);
}