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

在一个本机反应项目中,我在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)
           })
    }
a12473740 回答:如何将Assets文件夹中的压缩文件复制到DocumentDirectoryPath?

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

  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}`)
            })
    }

本文链接:https://www.f2er.com/3154116.html

大家都在问