ActiveXObject调用本地unzip.exe解压zip文件

问题场景

利用ActieXObject调用本地unzip.exe程序解压本地zip文件到本地目录

代码实现

安装unzip.exe,或者可以调用本地其它解压软件程序

zip.rar下载, 下载后解压,我放置的目录是:D:\Drivers\zip\unzip.exe,解压命令就是:"D:\Drivers\zip\unzip.exe" C:\Users\cjq\Desktop\images.zip -d C:\Users\cjq\Desktop\images, 也可以配置环境变量,命令就是:"unzip.exe" C:\Users\cjq\Desktop\images.zip -d C:\Users\cjq\Desktop\images

unzipDemo.vue

        <!--start-->
        <template>
          <div>
            <h1>Reading a local file </h1>
            <div>
              添加本地zip文件:<input type="text" v-model="zipPath" /><br>
              将zip解压到目录:<input type="text" v-model="path" /><br>
              <button @click="unZip()">解压</button>
            </div>
            <hr>

            <button @click="readFile()">读取文件</button>
            <el-row id="result">
             <el-col :span="8" v-for="(item,i) in results" :key="i">
               <img :src="item"/>
             </el-col>
            </el-row>
          </div>
        </template>

        <script>
          export default {
              name: 'demo',
              data() {
                  return {
                      results:[],
                      path:'C:\\Users\\cjq\\Desktop\\images',// 解压到的目录
                      zipPath:'C:\\Users\\cjq\\Desktop\\images.zip',// 解压文件
                  }
              },
              mounted(){
                  if(!(!!window.ActiveXObject||"ActiveXObject" in window)){
                      alert("请使用ie浏览器");
                  }
              },
              methods:{
                  unZip(){
                      try{
                          var shell=new ActiveXObject("wscript.shell");
                          var fso=new ActiveXObject("Scripting.FileSystemObject");
                          if(fso.FolderExists(this.path)){
                              // 删除之前解压的目录
                              fso.DeleteFolder(this.path);
                          }
                          // 解压文件
                          var shellStr='"D:\\Drivers\\zip\\unzip.exe" '+this.zipPath+' -d '+this.path
                          console.log(shellStr);
                          shell.Run(shellStr);
                      }catch (e) {
                          alert(JSON.stringify(e))
                      }
                  },
                  readFile(){
                      var fso=new ActiveXObject("Scripting.FileSystemObject");
                      if(fso.FolderExists(this.path)){
                          // 读取文件
                          var objFolder = fso.GetFolder(this.path);
                          var colFiles = new Enumerator(objFolder.Files);
                          for (; !colFiles.atEnd(); colFiles.moveNext()){
                              var objFile = colFiles.item();
                              this.results.push(objFile)
                          }
                      }else{
                          alert(this.path+"目录不存在");
                      }
                  }
              },
          };
        </script>
        <style scoped>
          input[type='text']{
              width: 200px;
          }
          #result{
              margin-top:30px;
          }
          #result img{
              height: 200px;
          }
        </style>
        <!--end-->
posted @ 2021-01-18 11:49  卷叶小树  阅读(95)  评论(0)    收藏  举报