Any way to unzip file on react-native

蓝咒 提交于 2019-12-11 02:39:54

问题


Managed to download .zip file to my filesystem on mobile phone. But after a while realised I can't find a way how to unzip that file. As I tried with:

  • https://github.com/plrthink/react-native-zip-archive
  • https://github.com/remobile/react-native-zip

First one dies immidiately after requiring, getting error "Cannot read property 'unzip' of undefined" (followed instructions carefully)

And the second one dies because it's dependant on codrova port to react native which also doesn't work.

Any suggestions or way to solve these problems?

Using react-native 0.35, testing on Note4 with android 5.1.1.


回答1:


I did manage in the end solve my problem:

using react-native-zip-archive

the solution was to change code inside: RNZipArchiveModule.java file which is inside module

The changes that needed to be applied are written in this comment: https://github.com/plrthink/react-native-zip-archive/issues/14#issuecomment-261712319

So credits to hujiudeyang for solving problem.




回答2:


go to this direction : node_modules\react-native-zip-archive\android\src\main\java\com\rnziparchive\RNZipArchiveModule.java

and replace this codes instead of unzip method

public static void customUnzip(File zipFile, File targetDirectory) throws IOException {
    ZipInputStream zis = new ZipInputStream(
            new BufferedInputStream(new FileInputStream(zipFile)));
    try {
      ZipEntry ze;
      int count;
      byte[] buffer = new byte[8192];
      while ((ze = zis.getNextEntry()) != null) {
        File file = new File(targetDirectory, ze.getName());
        File dir = ze.isDirectory() ? file : file.getParentFile();
        if (!dir.isDirectory() && !dir.mkdirs())
          throw new FileNotFoundException("Failed to ensure directory: " +
                  dir.getAbsolutePath());
        if (ze.isDirectory())
          continue;
        FileOutputStream fout = new FileOutputStream(file);
        try {
          while ((count = zis.read(buffer)) != -1)
            fout.write(buffer, 0, count);
        } finally {
          fout.close();
        }
            /* if time should be restored as well
            long time = ze.getTime();
            if (time > 0)
                file.setLastModified(time);
            */
      }
    } finally {
      zis.close();
    }
  }

  //**************************

  @ReactMethod
  public void unzip(final String zipFilePath, final String destDirectory, final String charset, final Promise promise) {
    new Thread(new Runnable() {
      @Override
      public void run() {
        try {
          customUnzip(new File(zipFilePath ) , new File(destDirectory));
        } catch (IOException e) {
          e.printStackTrace();
        }




         }
    }).start();
  }


来源:https://stackoverflow.com/questions/41630882/any-way-to-unzip-file-on-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!