精品学习网->精美文摘

上一篇    全部文章


js通过jszip和JSZipUtils实现本地上传zip文件
或者远程zip文件解压并获取文件中的内容


参考jszip使用文档  https://stuk.github.io/jszip/

测试:

1、
html中引入js库




2、拉取文件并解压的方法

function  getRomoteZipFile(){
  return  new  Promise(function(resolve,  reject)  {
                                //    step1  请求文件
                                JSZipUtils.getBinaryContent("http://**/***.zip",  function(err,  data)  {
                                        if  (err)  {
                                                reject(err);
                                        }  else  {
                                                resolve(data);
                                        }
                                });
                        })
                        .then(function(files)  {
                                //    step2  解压
                                return  JSZip.loadAsync(files);
                        })
                        .then((files)  =>  {
                                //  console.log("files:",  files);
                                //  step3  将文件列表中想要的文件转成想要的格式
                                const  fileName  =  Object.keys(files.files)[0];  //  测试就拿第一个文件
                                //arraybuffer表示文件为二进制,你想要什么文件类型就写什么,比如txt就写string等
                                return  files.file(fileName).async("arraybuffer");
                        })
}
3、调用方法

getRomoteZipFile().then(data  =>  {
                console.log("文件中的内容:",  data);
        })

FALSE

     返回顶部
JS上传或下载zip文件