简体   繁体   English

如何为epub使用java.util.zip

[英]How to use java.util.zip for epub

I have to edit the html files of epubs programmatically so what I did was to unzip the .epub and create a parser to make the necessary edits for the html files. 我必须以编程方式编辑epubs的html文件,所以我要做的是解压缩.epub并创建解析器以对html文件进行必要的编辑。 However, when I convert them back into an .epub using my code, EpubChecker shows that: 但是,当我使用代码将它们转换回.epub时,EpubChecker显示:

Error: Required META-INF/container.xml resource is missing

When I uncompressed my edited .epub, the container.xml is present and is not missing. 当我解压缩编辑的.epub时,container.xml存在并且不丢失。

I understand that the mimetype and META-INF has to be zipped first. 我知道必须先压缩mimetype和META-INF。 Here is my code to convert the files back to epub: 这是将文件转换回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();

When I manually zip the directory using WinRar, no errors are seen and it works properly. 当我使用WinRar手动压缩目录时,没有看到错误,并且可以正常工作。 I don't know what I am doing wrong. 我不知道我在做什么错。 Can somebody please help me?Thank you. 有人可以帮我吗?谢谢。

Looks like you're on Windows, so your FileInputStream(SOURCE_FOLDER + File.separator + "META-INF\\\\container.xml"); 看起来就像您在Windows上,因此您的FileInputStream(SOURCE_FOLDER + File.separator + "META-INF\\\\container.xml"); statement is correct for the OS, but I'd guess you need to change the other 2 strings to use the forward slash for the zipentry path. 声明对于操作系统是正确的,但是我想您需要更改其他2个字符串以对zipentry路径使用正斜杠。

ZipEntry container = new ZipEntry("META-INF\\container.xml");

try instead as 尝试作为

ZipEntry container = new ZipEntry("META-INF/container.xml");

and change 并改变

if(!file.toString().equals("mimetype") && !file.toString().equals("META-INF\\container.xml")){

to

if(!file.toString().equals("mimetype") && !file.toString().equals("META-INF/container.xml")){

accordingly. 相应地。

You may need to adjust your other ZipEntry 's as well. 您可能还需要调整其他ZipEntry From the ZIP spec (section "4.4.17 file name"): ZIP规范 (“ 4.4.17文件名”部分):

All slashes MUST be forward slashes '/' as opposed to backwards slashes '\\' 所有斜杠必须是正斜杠“ /”,而不是反斜杠“ \\”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM