ionic 2解压缩资产中的zip文件

时间:2017-07-21 14:16:08

标签: javascript angular ionic-framework unzip

我这样做了:

this.zip.unzip('../../assets/eval.zip', '../../assets/folder', (progress) => console.log('Unzipping, ' + Math.round((progress.loaded / progress.total) * 100) + '%'))
     .then((result) => {
      console.log();
       if(result === 0) console.log('SUCCESS');
       if(result === -1) console.log('FAILED');
     });

我在浏览器中测试它& android但它总是“失败”;

2 个答案:

答案 0 :(得分:0)

在Android设备上运行它,看到android studio device logging工具(android设备监视器)上的日志。您将找到错误的确切原因。 我有这个错误。原因是离子无法找到指定的文件夹,因此我使用this.file.externalDataDirectory或您选择的目录,格式如下 "文件:///存储/模拟/ 0 / yourPath&#34 ;.

// this.import(this.file.externalDataDirectory + '/Spark.epub', this.file.externalDataDirectory + '/spark');

this.file.checkDir(this.file.externalDataDirectory +'assets/folder','')
      .then(_ => {
      this.file.checkFile(this.file.externalDataDirectory +'assets/eval.zip','')
      .then(_ => {
      this.zip.unzip(this.file.externalDataDirectory +'assets/eval.zip', this.file.externalDataDirectory +'assets/folder', 
      (progress) => console.log('Unzipping, ' + Math.round((progress.loaded / progress.total) * 100) + '%'))
      .then((data) => {
        /* Wow everything goes good, but just in case verify data.success */
        console.log(data);
      }).then((error) => {
          /* Wow something goes wrong, check the error.message */
          console.log(error);
          debugger;
      })
      }).catch(err => console.log('file doesn\'t exist'));
      }).catch(err => console.log('Directory doesn\'t exist'));

答案 1 :(得分:0)

我可以告诉你,你无法从www/assets文件夹解压缩,你需要将该zip文件复制到设备的系统目录中,如 this.file.dataDirectory ,然后执行unzip in这里。

我已经测试过,它有效。

参考:https://github.com/MobileChromeApps/cordova-plugin-zip/issues/56