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. However, when I convert them back into an .epub using my code, EpubChecker shows that:
Error: Required META-INF/container.xml resource is missing
When I uncompressed my edited .epub, the container.xml is present and is not missing.
I understand that the mimetype and META-INF has to be zipped first. Here is my code to convert the files back to 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. 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");
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.
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. From the ZIP spec (section "4.4.17 file name"):
All slashes MUST be forward slashes '/' as opposed to backwards slashes '\'