使用JSZip实现在浏览器中操作文件与文件夹

1. 引言

浏览器中如何创建文件夹、写入文件呢?

答曰:可以借助JSZip这个库来实现在浏览器内存中创建文件与文件夹,最后只需下载这个.zip文件,就是最终得结果

类似的使用场景如下:

  • 在线下载很多图片,希望这些图片能分类保存到各个文件夹并最终下载成一个zip文件
  • 在线下载很多文档,希望这些文档能分类保存到各个文件夹并最终下载成一个zip文件

本质上都是希望浏览器能创建文件夹和创建文件,最终保存成一个文件来提供下载

JSZip的GitHub站点:Stuk/jszip: Create, read and edit .zip files with Javascript (github.com)

一个可用的中文站点:JSZip参考手册 (asprain.cn)

下面主要记录一下基础使用,详细的API请参考上述文档

2. 使用

2.1 安装

使用NPM:

shell 复制代码
npm install jszip

使用在线CDN:

html 复制代码
<script src="https://cdn.bootcdn.net/ajax/libs/jszip/3.10.1/jszip.js"></script>
  • 为了可以代码可以快速复现,笔者这里使用CDN的方式引入

2.2 创建zip实例

一个JSZip实例是读写.zip文件的基础

javascript 复制代码
const zip = new JSZip();

2.3 读取zip文件

读取官方的示例文件text.zip

javascript 复制代码
const zip = new JSZip();

fetch("https://stuk.github.io/jszip/test/ref/text.zip")       // 1) fetch the url
    .then(function (response) {                       // 2) filter on 200 OK
        if (response.status === 200 || response.status === 0) {
            return Promise.resolve(response.blob());
        } else {
            return Promise.reject(new Error(response.statusText));
        }
    })
    .then(data => zip.loadAsync(data))                            // 3) 加载数据
    .then(function (zip) {
        zip.forEach(function (relativePath, file) {  	// 4) 遍历压缩包内的文件
           console.log(`path: ${relativePath}, file: ${file.name}`)
           // 输出:path: Hello.txt, file: Hello.txt
        });
})

因为Hello.txt是个文本文件,可以直接使用string的方式读取内部的数据

javascript 复制代码
const zip = new JSZip();

fetch("https://stuk.github.io/jszip/test/ref/text.zip")       // 1) fetch the url
    .then(function (response) {                       // 2) filter on 200 OK
        if (response.status === 200 || response.status === 0) {
            return Promise.resolve(response.blob());
        } else {
            return Promise.reject(new Error(response.statusText));
        }
    })
    .then(data => zip.loadAsync(data))                            // 3) chain with the zip promise
    .then(function (zip) {
        return zip.file("Hello.txt").async("string"); // 4) 读取Hello.txt文件
    })
    .then(function success(text) {
        console.log(text); // 输出:Hello World
    }, function error(e) {
        console.error(e);
    });

2.4 创建zip文件

写入文件与数据

javascript 复制代码
zip.file("file.txt", "content");
new Promise((resolve, reject) => {
    resolve(zip.file("file.txt").async("string"))
}).then(data => {
    console.log(data); // 输出:content
})

写入指定文件夹下的指定文件

javascript 复制代码
zip.file("text/file.txt", "content");
zip.forEach(function (relativePath, file) {
    console.log(`path: ${relativePath}, file: ${file.name}`)
    // 输出:path: text/file.txt, file: text/file.txt
});

最后的目录结构可以参考下图

2.5 下载zip文件

这里将上面的file.txt下载为zip,使用a链接的方式

javascript 复制代码
zip.generateAsync({ type: "blob" }).then(function (content) {
    document.body.appendChild(document.createElement("a"));
    document.querySelector("a").href = URL.createObjectURL(content);
    document.querySelector("a").download = "test.zip";
    document.querySelector("a").click();
});

完整的代码如下:

html 复制代码
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/jszip/3.10.1/jszip.js"></script>
</head>

<body>

    <script>
        const zip = new JSZip();

        // fetch("https://stuk.github.io/jszip/test/ref/text.zip")       // 1) fetch the url
        //     .then(function (response) {                       // 2) filter on 200 OK
        //         if (response.status === 200 || response.status === 0) {
        //             return Promise.resolve(response.blob());
        //         } else {
        //             return Promise.reject(new Error(response.statusText));
        //         }
        //     })
        //     .then(data => zip.loadAsync(data))                            // 3) chain with the zip promise
        //     .then(function (zip) {
        //         return zip.file("Hello.txt").async("string"); // 4) chain with the text content
        //     })
        //     .then(function success(text) {
        //         console.log(text);
        //     }, function error(e) {
        //         console.error(e);
        //     });
        zip.file("text/file.txt", "content");
        zip.forEach(function (relativePath, file) { 
           console.log(`path: ${relativePath}, file: ${file.name}`)
        });

        zip.generateAsync({ type: "blob" }).then(function (content) {
            document.body.appendChild(document.createElement("a"));
            document.querySelector("a").href = URL.createObjectURL(content);
            document.querySelector("a").download = "test.zip";
            document.querySelector("a").click();
        });
    </script>

</body>

</html>

3. 参考资料

1\] [How to use JSZip (stuk.github.io)](https://stuk.github.io/jszip/documentation/examples.html) \[2\] [JSZip参考手册 (asprain.cn)](http://docs.asprain.cn/jszip/jszip.html#)

相关推荐
打小就很皮...12 天前
浏览器存储 Cookie,Local Storage和Session Storage
前端·缓存·浏览器
小妖66614 天前
chrome 浏览器怎么不自动提示是否翻译网站
浏览器
大名人儿18 天前
【浏览器网络请求全过程】
浏览器·网络请求·详解·全过程
windliang20 天前
Cursor 写一个网页标题重命名的浏览器插件
前端·浏览器
前端付豪20 天前
1、为什么浏览器要有渲染流程? ——带你一口气吃透 Critical Rendering Path
前端·后端·浏览器
啵啵学习20 天前
浏览器插件,提示:此扩展程序未遵循 Chrome 扩展程序的最佳实践,因此已无法再使用
前端·chrome·浏览器·插件·破解
前端南玖21 天前
通过performance面板验证浏览器资源加载与渲染机制
前端·面试·浏览器
mx95124 天前
真实业务场景:在React中使用Web Worker实现HTML导出PDF的性能优化实践
性能优化·浏览器
前端南玖1 个月前
浏览器如何确定最终的CSS属性值?解析计算优先级与规则
前端·css·浏览器
NSJim1 个月前
微软Edge浏览器字体设置
edge·浏览器·字体设置