Question

I have read a lot of threads on this site regarding my issue, and the code I have is based off some of them. The problem I have currently is that unzipping an ePub results in only the mimetype file being extracted with LogCat getting errors in creating a folder from what I understand.

Following is what I use to decompress any zip file, I got it from Problem when Unzipping:

/**
 * @author jon
 */
public class Decompress {
    private String _zipFile;
    private String _location;
    ZipEntry ze = null;

    public Decompress(String zipFile, String location) {
        _zipFile = zipFile;
        _location = location;

        _dirChecker("");
    }

    public void unzip() {
        try {
            FileInputStream fin = new FileInputStream(_zipFile);
            ZipInputStream zin = new ZipInputStream(fin);
            while ((ze = zin.getNextEntry()) != null) {
                Log.v("Decompress", "Unzipping " + ze.getName());

                if (ze.isDirectory()) {
                    _dirChecker(ze.getName());
                } else {
                    FileOutputStream fout = new FileOutputStream(_location
                            + ze.getName());
                    for (int c = zin.read(); c != -1; c = zin.read()) {
                        fout.write(c);
                    }

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

            }
            zin.close();
        } catch (Exception e) {
            Log.e("Decompress", "unzip", e);
        }

    }

    private void _dirChecker(String dir) {
        File f = new File(_location + dir);

        Log.d("Decompress", f.getAbsolutePath().toString());

        if (!f.isDirectory()) {
            f.mkdirs();
        }
    }
}

Other articles I have referred to: Unzip a zipped file on sd card in Android application Why my decompress class don't make directories? Uzip folders recursively -android Render epub files in android Android EPUBLIB read/load content

Here is the LogCat Error I receive on the second file(/folder) after the mimetype file.

03-24 10:38:13.991: E/Decompress(23190): java.io.FileNotFoundException: /storage/emulated/0/unzipped/yamani/CDS-suggestions.docx: open failed: ENOENT (No such file or directory)

I'm currently using a regular zip file to test out my code.

Thanks for the help.

Was it helpful?

Solution

Was able to solve my problem by using another snippet found on this site.

Source:

public class unpackZip {
    public boolean unzip(String zipname, String path) {
        InputStream is;
        ZipInputStream zis;
        try {
            String filename;
            is = new FileInputStream(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;
                } else {
                    // Make this part of the code more efficient .code-revise
                    File fmd = new File(path, filename);
                    Log.d("Unzipping", fmd.getParentFile().getPath());
                    String parent = fmd.getParentFile().getPath();

                    File fmd_1 = new File(parent);
                    fmd_1.mkdirs();
                    // end of .code-revise
                }

                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;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top