Search code examples
androidunzip

Unzip folder gives FileNotFound Exception in android


I am trying unzip zip folder to folder, it works and it unzipped some folder but When it takes index.html it gives error like this,I want to unzip files to folder but I got a error,How can I solve this problem?

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.kocsistem.pixageoneandroid/files/Contents/widgetContent/ab34f7fe018a45f0a43d3139d6986b84/index.html (No such file or directory)
        at java.io.FileOutputStream.open0(Native Method)
W/System.err:     at java.io.FileOutputStream.open(FileOutputStream.java:308)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:238)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:180)
        at com.kocsistem.pixageoneandroid.utils.Utils.unpackZip(Utils.java:454)
        at 

My unzip funtion,it works but for index.html it gives above error

public static void unpackZip(String src, String dest, String folderName){

    final int BUFFER_SIZE = 4096;

    BufferedOutputStream bufferedOutputStream = null;
    FileInputStream fileInputStream;
    try {
        fileInputStream = new FileInputStream(src);
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
        ZipEntry zipEntry;

        while ((zipEntry = zipInputStream.getNextEntry()) != null){

            String zipEntryName = zipEntry.getName();

            String name = dest.substring(dest.lastIndexOf("/")-1);

            File FileName = new File(dest);
            if (!FileName.isDirectory()) {
                try {
                    if (FileName.mkdir()) {
                    } else {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            File file = new File(dest+ "/" + folderName +"/" +zipEntryName);

            if (file.exists()){

            } else {
                if(zipEntry.isDirectory()){
                    file.mkdirs();
                }else{
                    byte buffer[] = new byte[BUFFER_SIZE];
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
                    int count;

                    while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
                        bufferedOutputStream.write(buffer, 0, count);
                    }

                    bufferedOutputStream.flush();
                    bufferedOutputStream.close();
                }
            }
        }
        zipInputStream.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Solution

  • build.gradle

    zip4j implementation 'com.github.joielechong:zip4jandroid:1.0.1'

    allprojects {
        repositories {
            maven { url "https://jitpack.io" }
    
        }
    }
    

    Code

    String source = destFilePath + "/" + fileNameList.get(i);
    String target = destFilePath.getAbsolutePath();
    
        try {
               ZipFile zipFile = new ZipFile(source);
               zipFile.extractAll(target);
             } catch (ZipException e) {
               e.printStackTrace();
               }