如何将Assets文件夹中的压缩文件复制到DocumentDirectoryPath?

时间:2019-11-06 07:55:06

标签: react-native android-assets

在一个本机反应项目中,我在android_assets文件夹中有一个压缩文件。我想解压缩该文件夹并将其复制到DocumentDirectoryPath中。我已经导入了react-native-zip-archive并使用了unzipAssets,但是它似乎不起作用。我使用了以下代码,但出现错误:无法打开“ ./ICF-Package”。

const assetPath ='./ICF-Package.zip'   const targetPath = ${DocumentDirectoryPath}/ICF-Package.zip

  copyfile() {
      unzipAssets(assetPath, targetPath)
         .then(() => {
           console.log('unzip completed!')
           })
           .catch((error) => {
             console.log(error)
           })
    }

1 个答案:

答案 0 :(得分:0)

您可以安装它并尝试解决问题。

  1. $ npm install react-native-fetch-blob
  2. $ react-native link react-native-fetch-blob
import RNFetchBlob from 'react-native-fetch-blob';
import { unzip } from 'react-native-zip-archive';

let dirs = RNFetchBlob.fs.dirs
const documentPath = dirs.DocumentDir
const resourceUrl = 'file:///android_asset/target.zip'

    downloadResource = () => {

        // Set the path to store on the device
        const dirs = RNFetchBlob.fs.dirs.DocumentDir
        const homePath = dirs + '/target.zip'

        RNFetchBlob
            .config({
                path: homePath
            })
            .fetch('GET', resourceUrl)
            .progress((received, total) => {
                const percentage = Math.floor(received / total * 10000) / 100 + '%'
                console.log(percentage)
            })
            .then(resourceFile => {
                console.log('target.zip file download success')

                this.unzip(resourceFile);
            })
            .catch((errorMessage, statusCode) => {
                console.log(errorMessage);
            })
    }


    unzip = resourceFile => {
        const resourcePath = resourceFile.path()
        unzip(resourcePath, documentPath)
            .then(path => {
                console.log(`unzip sucess: ${path}`)
            })
    }