0

I'd like to apply zip4j in my android app. I put a demo zip file into the assets folder (main/assets/test.zip). I'd like to open it with zip4j:

import net.lingala.zip4j.core.ZipFile;
...
ZipFile zipFile = new ZipFile(new File("an/absolute/path/is/required"));

However, the constructor required for applying zip4j requires a String object (the absolute path of the file) or a File object. But the AssetManger of Android only returns an InputStream. Also please not: The AssetManger does not provide an absolute path or something.

So, what do I have to do in order to transform the input stream of InputStream is = getResources().getAssets().open(filename); into a java.io.File object which I then can apply to ZipFile

toom
  • 12,864
  • 27
  • 89
  • 128
  • `file:///android_asset/` is the absolute path to the asset directory. – ieatacid Sep 23 '15 at 15:35
  • I already tried that. It does not work. – toom Sep 23 '15 at 15:37
  • Any particular reason for the zip file to be located in the assets folder? Is it your intention to eventually bundle one with your app that way? Perhaps downloading/copying the zip file to local storage (from which you can then get a `File`) is an option too? Alternatively, have a look at the [`java.util.zip.ZipInputStream`](http://developer.android.com/reference/java/util/zip/ZipInputStream.html) that comes with the Android SDK: it acts as a wrapper for any `InputStream`, meaning you could use it directly with the result of `getAssets().open(...)`. – MH. Sep 23 '15 at 15:40
  • Yes, `ZipInputStream` is exactly what I am currently using. `Zip4j` is probably not recommended for using in Android projects – toom Sep 23 '15 at 15:42
  • Why? What does this package provide beyond `java.util.zip`? – user207421 Sep 24 '15 at 09:46

1 Answers1

-1

Quite a a common Task when dealing with assets:

  1. Get a AssetFileDescriptor from the AssetManager : openFD(String fileName).
  2. Get a FileDescriptor from that AssetFileDescriptor : getFileDescriptor()
  3. Construct a new File from the FileDescriptor.
user207421
  • 305,947
  • 44
  • 307
  • 483
Peter
  • 1,769
  • 1
  • 14
  • 18