1、所需物料准备
1、jszip.min.js 仓库地址:https://github.com/Stuk/jszip CDN地址:https://cdn.bootcdn.net/ajax/libs/jszip/3.10.0/jszip.min.js 2、FileSaver.min.js 仓库地址:https://github.com/eligrey/FileSaver.js CDN地址:https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
2、准备好所需物料后废话不多说,可以上代码了
<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="html" cid="n37" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><body>
<input id="uploadFile" type="file" onchange="changeFile()" />
<script src="https://cdn.bootcdn.net/ajax/libs/jszip/3.10.0/jszip.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js"></script>
<script>
function changeFile() {
const uploadFileEle = document.querySelector("#uploadFile");
let file = uploadFileEle.files[0];
JSZip.loadAsync(file).then((res) => {
console.log(res.files);
res.forEach((ele, obj) => {
if (!obj.dir) {//判断是否为文件
/**
转换成Base64方式,根据需求引用
let binary = '';
let bytes = obj._data.compressedContent;
for (let index = 0; index < bytes.length; index++) {
const element = bytes[index];
binary += String.fromCharCode(element);
}
binary = window.btoa(binary);
*/
// 压缩包内文件名称
let fileName = obj.name;
//压缩包内文件大小
let unsize = obj._data.uncompressedSize / 1024;
let fileSize = unsize.toFixed(2) + "KB";
//下载操作
let base = res.file(obj.name).async('blob');
base.then(rr => {
saveAs(rr, obj.name);
})
}
})
})
}
</script>
</body>
</pre>
3、温馨提示
1、代码可自行复制测试,下载操作根据实际需求更改即可 2、上述代码文件上传没做类型限制,本人在测试过程中发现:.apk,.zip,.rar可读取包内文件。.7z是不支持的哇 3、如压缩包内文件命名为中文是,解压出来文件名可能显示乱码,下载后重命名即可。
结语
专业JS加密解密,有需要到JS在线加密解密,定制需求网站底部有我联系方式。
jsjiami.com