0

I'm trying to download a zip file with rn-fetch-blob, then when I got this file I unzip it with React-native-zip-archive.

It often works well, but sometimes, the "unzipFile()" function I've created can't unzip the file, like if it is corrupted. Someone already got this problem ?

Here is my code :

downloadZipFile(res => {
        unzipFile(res.path(), (boolean, path) => {
          if (boolean !== false) {
            db = SQLite.openDatabase({
              name: "addb.sqlite",
              location: "default",
              createFromLocation: path
            }).then(DB => {
              db = DB;
              db.transaction(tx => {
                tx.executeSql(
                  "SELECT * FROM sqlite_master",
                  [],
                  (tx, results) => {
                    console.log("Logs sqlite_master");

                    const rows = results.rows;

                    for (let i = 0; i < rows.length; i++) {
                      console.log(_getCurrentDate());
                      datas.push({
                        ...rows.item(i)
                      });
                    }
                    console.log(datas);

                    callback(true);
                  },
                  (tx, err) => {
                    console.log(err)
                  }
                );
              });
            });
          } else {
            console.log("Can't create database");
            callback(false);
          }
        });
      });

And the functions I used :

export function downloadZipFile(callback) {
  RNFetchBlob.config({
    fileCache: true
  })
    .fetch(
      "GET",
      "MY LINK"
    )
    .then(res => {
      console.log("The file saved to ", res.path());
      callback(res);
    })
    .catch((errorMessage, statusCode) => {
      // error handling
      console.log(
        "erreur : " + errorMessage + " and statuscode : " + statusCode
      );
    });
}

export function unzipFile(sourcePath, callback) {
  const charset = "UTF-8";
  const targetPath = "/data/user/0/com.myapp/databases/";

  unzip(sourcePath, targetPath, charset)
    .then(path => {
      console.log(`unzip completed at ${path}`);

      callback(true, path);
    })
    .catch(error => {
      console.log("there is an error" + error);
      callback(false, null);
    });
}

Others informations :

  • The file is a database that I have to put in the "databases" folder's application. I tried to put a console.log(path) everywhere in the "unzipFile()" function to see if the file is really created when I try to unzip it, and it seems he is here… And when the file is impossible to unzip, it does the same size as the others which work.

  • rn-fetch-blob calls an api which copy an existant distant database and zip it as an axd file. Is there any problem with this format ? Can the api be the problem ?

  • The axd file created by the api is used by an existant application and seems to work correctly for the existant application. Moreover, when we download the file without rn-fetch-blob (by copying the link in my navigator), it works correctly everytime I tried.

  • I tried to download the file directly,the api always sent me the same file (a zip file or an axd file), and it works without problem (20 try). Can the problem be the delay to download the file ? With the api, it takes 5 or 6 seconds, without it takes 2 seconds. But I think my unzipFile() function only start when the file is downloaded, no ? And as I said, when I put a console.log(path) in the unzipFile() function, the file is here, with same size as others...

I Don't know how to make it works everytime, hope someone can help me :) Ty !

4

1 回答 1

0

我试图放一个 for(let i = 1; i < 101; i++) 做 RNFB 100 次:它工作 97 次 / 100 和 96 次 /100 ......然后我试着放一个计时器,可以肯定文件下载完成,工作 3 次 / 100... 我删除了计时器,现在它不再工作了,或者 5 次 / 100...

我真的不明白是什么问题:(

于 2019-10-09T09:06:07.677 回答