【问题标题】:unzipping ePub file doesn't work解压缩 ePub 文件不起作用
【发布时间】:2014-03-21 13:19:53
【问题描述】:

我正在开发一个应用程序来将 .epub 文件解压缩到 Android 中的 SDCARD 中。 我已经阅读了Can't Unzip EPub File TOPIC。 它适用于 .zip 文件,但不适用于 .epub 文件。 有人能告诉我问题出在哪里吗? 这是异常日志:

03-21 13:35:44.281: W/System.err(1255): java.io.FileNotFoundException: /mnt/sdcard/unzipped11/META-INF/container.xml: open failed: ENOENT (No such file or directory)

我正在使用此代码:

private void decom() throws IOException {
    ZipFile zipFile = new ZipFile(Environment.getExternalStorageDirectory()+"/dir.zip");
    String path = Environment.getExternalStorageDirectory() + "/unzipped10/";

    Enumeration<?> files = zipFile.entries();
    _dirChecker("");
    while (files.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) files.nextElement();
        Log.v("ZipEntry", ""+entry);
        Log.v("isDirectory", ""+entry.isDirectory());

        if (entry.isDirectory()) {
            File file = new File(path + entry.getName());
            file.mkdir();
            System.out.println("Create dir " + entry.getName());
        } else {
            File f = new File(path + entry.getName());
            FileOutputStream fos = new FileOutputStream(f);
            InputStream is = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            fos.close();
            System.out.println("Create File " + entry.getName());
        }
    }
}

【问题讨论】:

  • 在获得它尝试创建文件 META-INF/container.xml 的日志输出之前,您是否获得了它创建 META-INF 目录的日志输出?
  • 不,我不知道为什么
  • 它甚至没有检测到它是一个目录

标签: android epub epub3


【解决方案1】:

根据您对我的评论的回复,听起来在尝试写入存档中文件的条目之前未创建文件的父目录。

听起来您可能需要更改处理 zip 文件中文件条目的代码以创建父目录(如果它们尚不存在)。您可能还需要更改创建目录的代码,以在创建目录之前检查目录是否已存在。

试试这样的:

    while (files.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) files.nextElement();
        Log.d(TAG, "ZipEntry: "+entry);
        Log.d(TAG, "isDirectory: " + entry.isDirectory());

        if (entry.isDirectory()) {
            File file = new File(path + entry.getName());
            file.mkdir();
            Log.d(TAG, "Create dir " + entry.getName());
        } else {
            File f = new File(path + entry.getName());
            f.getParentFile().mkdirs();
            FileOutputStream fos = new FileOutputStream(f);
            InputStream is = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            fos.close();
            Log.d(TAG, "Create File " + entry.getName());
        }
    }
    Log.d(TAG, "Done extracting epub file");

对我来说,这会使用测试 epub 产生以下输出(来自 Google 的 epub 示例的 moby dick:https://code.google.com/p/epub-samples/downloads/list

ZipEntry: mimetype
isDirectory: false
Create File mimetype
ZipEntry: META-INF/
isDirectory: true
Create dir META-INF/
ZipEntry: META-INF/container.xml
isDirectory: false
Create File META-INF/container.xml
ZipEntry: OPS/
isDirectory: true
Create dir OPS/
ZipEntry: OPS/chapter_001.xhtml
isDirectory: false
Create File OPS/chapter_001.xhtml
ZipEntry: OPS/chapter_002.xhtml
isDirectory: false
Create File OPS/chapter_002.xhtml
ZipEntry: OPS/chapter_003.xhtml
isDirectory: false
...
Create File OPS/toc-short.xhtml
ZipEntry: OPS/toc.xhtml
isDirectory: false
Create File OPS/toc.xhtml
Done extracting epub file

【讨论】:

  • 它不起作用,(顺便说一句,我正在使用虚拟设备)
  • 你能给我更多的信息吗?是不是和以前一样的错误?
  • 我刚刚更新了代码示例以解决一个问题(将 mkdirs() 调用移动了一行),并且我将日志记录更改为使用 Android 的日志记录。我添加了从示例 Android 应用程序中获得的输出,它表明我能够使用提供的代码提取 epub。
  • 如果此代码仍然无法正常工作,您可以尝试对您的 epub 运行 epubcheck。 github.com/IDPF/epubcheckvalidator.idpf.org
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
相关资源
最近更新 更多