3

I am using the following code to unzip a set of files (containing folders as well):

private boolean unpackZip(String path, String zipname)
{       
     InputStream is;
     ZipInputStream zis;
     try 
     {
         String filename;
         is = new FileInputStream(path + zipname);
         zis = new ZipInputStream(new BufferedInputStream(is));     
         ZipEntry ze;
         byte[] buffer = new byte[1024];
         int count;

         while ((ze = zis.getNextEntry()) != null) 
         {
             // zapis do souboru
             filename = ze.getName();

             // Need to create directories if not exists, or
             // it will generate an Exception...
             if (ze.isDirectory()) {
                File fmd = new File(path + filename);
                fmd.mkdirs();
                continue;
             }

             FileOutputStream fout = new FileOutputStream(path + filename);

             // cteni zipu a zapis
             while ((count = zis.read(buffer)) != -1) 
             {
                 fout.write(buffer, 0, count);             
             }

             fout.close();               
             zis.closeEntry();
         }

         zis.close();
     } 
     catch(IOException e)
     {
         e.printStackTrace();
         return false;
     }

    return true;
}

The code fails on FileOutputStream fout = new FileOutputStream(path + filename) with the error:

java.io.FileNotFoundException: /storage/emulated/0/BASEFOLDER/FOLDER1/FILE.png 

BASEFOLDER already exists, that is where I am trying to unzip the folder to. If I manually (or programmatically) create FOLDER1, the code runs fine and successfully unzips. I believe it is crashing because the very first file (ze) is named FOLDER1/FILE.png and FOLDER1 hasn't been created yet. How do I get around this? I know other people have used this code, I find it unlikely that it randomly doesn't work for me...

Community
  • 1
  • 1
easycheese
  • 5,859
  • 10
  • 53
  • 87

3 Answers3

0

Have you got this in your AndroidManifest.xml file?

Add Write to external Storage permission

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

Hani Hussein
  • 159
  • 4
0

I had the same problem. After several investigation I found that. put following single line in your code:

if (ze.isDirectory()) {
        File fmd = new File(path + filename);
        fmd.mkdirs();
        zis.closeEntry(); //  <<<<<< ADD THIS LINE
        continue;
    }
Behrouz.M
  • 3,445
  • 6
  • 37
  • 64
0

Sometime the extract files has been extracted before its parent directory is created, for example: File A inside directory B. But B directory is not created, index of files listing below cause the issue:

  • dir_b/file_a.txt
  • dir_b/
  • dir_b/file_c.txt

So, to sure directory created before file extracting, you need to create parent directories first, for example:

val targetFile = File(tempOutputDir, zipEntry.name)
if (zipEntry.isDirectory) {
    targetFile.mkdirs()
} else {
    try {
        try {
            targetFile.parentFile?.mkdirs()  // <-- ADD THIS LINE
        } catch (exception: Exception) {
            Log.e("ExampleApp", exception.localizedMessage, exception)
        }
        val bufferOutputStream = BufferedOutputStream(
            FileOutputStream(targetFile)
        )
        val buffer = ByteArray(1024)
        var read = zipInputStream.read(buffer)
        while (read != -1) {
            bufferOutputStream.write(buffer, 0, read)
            read = zipInputStream.read(buffer)
        }
        bufferOutputStream.close()
    } catch (exception: Exception) {
        Log.e("ExampleApp", exception.localizedMessage, exception)
    }
}
dphans
  • 1,543
  • 19
  • 20