Search code examples
androidandroid-5.0-lollipop

how to read from a zipfile in a folder which was selected by the ACTION_OPEN_DOCUMENT_TREE Intent?


how to read from a zipfile in a folder which was selected by the ACTION_OPEN_DOCUMENT_TREE Intent ?

My app let the user choose a folder through the ACTION_OPEN_DOCUMENT_TREE Intent. In that folder i will have a Zipfile with a specific name. Goal is to read that Zipfile with java.util.zip.ZipFile.

How do I instantiate a ZipFile with this specific Zipfilename from the provided URI (Folderinfo) in onActivityResult ?

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri treeUri = data.getData();
    String sPath=treeUri.getPath();

    java.util.zip.ZipFile myzip=new java.util.zip.ZipFile("mypath"); <-- Constructor expects File or Path as String. Howto handle this with the Uri ?

Solution

  • How do I instantiate a ZipFile with this specific Zipfilename from the provided URI (Folderinfo) in onActivityResult ?

    You can't, as there is no file, and ZipFile needs a file. You can only get an InputStream, using openInputStream() on a ContentResolver, and that only if you get a Uri to that specific file that you are looking for.

    Your options appear to be:

    • Use ZipInputStream, which can wrap an InputStream

    • Find some third-party library that accepts an InputStream as input and gives you a better API

    • Copy the ZIP flie to your app's portion of internal storage and read its contents using ZipFile