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

2016-11-25  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读