我正在开发一个应用程序,将。epub文件解压缩到Android中的SDCARD。我已经读了Can't Unzip EPub文件主题。它适用于。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());
}
}
}
根据您对我的评论的回应,听起来好像在试图写入归档文件中的文件条目之前没有创建该文件的父目录。
听起来您可能需要修改处理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示例:https://code.google.com/p/epub-samples/downloads/list的moby dick)产生以下输出
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