react-native - 如何将 Assets 文件夹中的压缩文件复制到 DocumentDirectoryPath?
问题描述
在一个 react-native 项目中,我有一个 android_assets 文件夹中的压缩文件。我想解压缩此文件夹并将其复制到 DocumentDirectoryPath。我已经导入了 react-native-zip-archive 并使用了 unzipAssets,但它似乎不起作用。我使用了以下代码,但出现错误:“./ICF-Package”无法打开。
const assetsPath = './ICF-Package.zip' const targetPath =${DocumentDirectoryPath}/ICF-Package.zip
copyfile() {
unzipAssets(assetPath, targetPath)
.then(() => {
console.log('unzip completed!')
})
.catch((error) => {
console.log(error)
})
}
解决方案
您可以安装它并尝试解决问题。
$ npm install react-native-fetch-blob
$ 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}`)
})
}
推荐阅读
- angular - 以角度导入许多文件
- reactjs - 有没有办法在 SPFX 的应用程序定制器扩展中使用 ComponentDidMount() 和 ComponentDidUpdate()
- database - 用sybase语言显示数据库表
- django - Django 独特的多对多字段
- java - 我希望我的警报对话框触发并保持显示错误消息
- sql - 使用 regexp_like
- django - 禁止 (403) CSRF 验证失败
- google-cloud-run - Cloud Run 区域间出站流量
- git - 尝试在登台服务器上切换到 Git 分支时未检测到它
- keras - 从前馈神经网络获取边列表/邻接矩阵