首页 文章

在Android中创建文件夹解压缩epub

提问于
浏览
1

我已经在这个网站上阅读了很多关于我的问题的帖子,我的代码基于其中一些 . 我目前的问题是解压缩ePub导致只有使用LogCat提取的mimetype文件在我理解的创建文件夹时出错 .

以下是我用来解压缩任何zip文件,我从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();
        }
    }
}

我提到过的其他文章: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

这是我在mimetype文件后面的第二个文件(/文件夹)上收到的LogCat错误 .

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)

我目前正在使用常规zip文件来测试我的代码 .

谢谢您的帮助 .

1 回答

  • 1

    能够通过使用此站点上找到的另一个片段来解决我的问题 .

    资源:

    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;
        }
    }
    

相关问题