0

In a react-native project,I have a zipped file which is in android_assets folder. I want to unzip this folder and copy it into the DocumentDirectoryPath. I have imported react-native-zip-archive and used unzipAssets,but it doesn't seem to work. I have used the following code but I get Error: "./ICF-Package" could not be opened.

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)
           })
    }
Aishwarya R M
  • 123
  • 1
  • 10

1 Answers1

0

You can install it and try to fix the problem.

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

hong developer
  • 13,291
  • 4
  • 38
  • 68