美文网首页
下载 + 解压(zip) react native 实现

下载 + 解压(zip) react native 实现

作者: 楚怀哲 | 来源:发表于2016-11-25 15:21 被阅读0次
import React, { Component } from 'react';


import * as Config from './Config.js';
import NetUtil from './NetUtil.js';
var RNFS = require('react-native-fs');

var Zip = require('@remobile/react-native-zip');

// const ZipArchive = require('react-native-zip-archive')

const jobId = -1;
const downloadDestName = RNFS.DocumentDirectoryPath+"/test.zip";

class LogicFuction  {
    constructor() {}

    //下载
    downNewCourse()
    {
        
        const progress = data => {
          const percentage = ((100 * data.bytesWritten) / data.contentLength) | 0;
          const text = `Progress ${percentage}%`;
          console.log(text);
        };
        const begin = res => {
          console.log('Download has begun' );
        };
        const progressDivider = 1;

        const ret = RNFS.downloadFile({ 
                            fromUrl: Config.APP_Course_New, 
                            toFile: downloadDestName, 
                            begin, 
                            progress,
                            false, 
                            progressDivider 
        });

        jobId = ret.jobId;

        ret.promise.then(res => {
            console.log("file download ");
            console.log(downloadDestName);
            console.log(res);
            
            // 调用解压函数
            this.unzipNewCourse();


        }).catch(err => {
          console.log(err)
          jobId = -1;
        });

    }

    //解压
    unzipNewCourse()
    {
      // zipPath:zip的路径
     // documentPath:解压到的目录
      Zip.unzip(downloadDestName, RNFS.DocumentDirectoryPath, (err)=>{
        if (err) 
        {
          // 解压失败
        } 
        else 
        {
          // 解压成功,将zip删除
          RNFS.unlink(downloadDestName).then(() => {
          });
        }
      });


        jobId = -1;
    }
    // 



}


var instance = new LogicFuction();

module.exports = instance;


{9A47FFB6-0C84-4359-13BC-8436A460728C}.png

相关文章

网友评论

      本文标题:下载 + 解压(zip) react native 实现

      本文链接:https://www.haomeiwen.com/subject/qxfwpttx.html