简体   繁体   English

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

[英]ionic 2 Unzip zip file in assets

I did this: 我这样做:

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');
     });

i testes it in browser & android but it always "FAILED"; 我在浏览器和android中对其进行了测试,但始终“失败”;

Run it on android device and see the log on android studio device logging tool (android device monitor). 在android设备上运行它,并查看android studio设备日志记录工具(android设备监视器)上的日志。 You will find the exact cause of the error. 您将找到错误的确切原因。 I had this error. 我有这个错误。 The cause was ionic couldn't find the specified folder so i used this.file.externalDataDirectory or the directory you chose in a format like "file:///storage/emulated/0/yourPath". 原因是离子找不到指定的文件夹,因此我使用了this.file.externalDataDirectory或您选择的目录,格式为“ file:/// storage / emulated / 0 / yourPath”。

// 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'));

I can tell you that you cannot unzip from www/assets folder, you need to copy that zip file into system directory of the device like this.file.dataDirectory , then execute unzip in here. 我可以告诉您,您无法从www/assets文件夹解压缩,您需要将该zip文件复制到设备的系统目录中,例如this.file.dataDirectory ,然后在此处执行解压缩。

I've tested, it works. 我已经测试过,它可以工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM