我必须以编程方式编辑 epub 的 html 文件,所以我所做的就是解压缩 .epub 并创建一个解析器来对 html 文件进行必要的编辑。但是,当我使用代码将它们转换回 .epub 时,EpubChecker 显示:
Error: Required META-INF/container.xml resource is missing
当我解压缩编辑后的 .epub 时,container.xml 存在并且没有丢失。
我知道必须首先压缩 mimetype 和 META-INF。这是我将文件转换回 epub 的代码:
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
System.out.println("Output to Zip : " + zipFile);
writeMimeType(zos);
ZipEntry container = new ZipEntry("META-INF\\container.xml");
zos.putNextEntry(container);
FileInputStream inMime2 = new FileInputStream(SOURCE_FOLDER + File.separator + "META-INF\\container.xml");
int len2;
while((len2 = inMime2.read(buffer)) > 0){
zos.write(buffer, 0, len2);
}
inMime2.close();
for(String file : this.fileList){
if(!file.toString().equals("mimetype") && !file.toString().equals("META-INF\\container.xml")){
System.out.println("File Added : " + file);
ZipEntry ze= new ZipEntry(file);
zos.putNextEntry(ze);
FileInputStream in =
new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
in.close();
}
}
zos.closeEntry();
zos.close();
当我使用 WinRar 手动压缩目录时,没有看到错误并且工作正常。我不知道我做错了什么。有人可以帮我吗?谢谢。
最佳答案
看起来您在 Windows 上,因此您的 FileInputStream(SOURCE_FOLDER + File.separator + "META-INF\\container.xml");
语句对于操作系统来说是正确的,但我猜您需要更改其他 2 个字符串以使用 zipentry 路径的正斜杠。
ZipEntry container = new ZipEntry("META-INF\\container.xml");
尝试改为
ZipEntry container = new ZipEntry("META-INF/container.xml");
并改变
if(!file.toString().equals("mimetype") && !file.toString().equals("META-INF\\container.xml")){
至
if(!file.toString().equals("mimetype") && !file.toString().equals("META-INF/container.xml")){
相应地。
您可能还需要调整其他 ZipEntry
。来自 the ZIP spec (“4.4.17 文件名”部分):
All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'
关于java - 如何使用 java.util.zip 制作 epub,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14014236/