Using java.util.zip to create a valid epub

I built a method that recursively adds the contents of a folder to a zip document with an epub file extension, which is basically what epub does except for one thing:

the first document in the archive must have the name "mimetype", the type must be specified by application / epub + zip and it must begin with an offset of byte 38. Is there a way to add the mimetype type to the archive with an offset of 38?

The method I built almost works. He creates an epub that can be read by most e-books, but he does not check. EpubCheck reports this error:

mimetype contains wrong type (application/epub+zip expected) 

This is a problem that does not exist in the original test epub, but appears in the reconstructed epub. And I double checked that the contents of the file with the unpacked / modified mimetype is correct.

The method is too much to post here. But this is what I use to add the mimetype file to the archive:

 out = new ZipOutputStream(new FileOutputStream(outFilename)); FileInputStream in = new FileInputStream(mimeTypePath); out.putNextEntry(new ZipEntry("mimetype")); int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); out.close(); 
+6
source share
1 answer

As described on the Wikipedia Open Container Format , the mimetype file must be the first entry in the ZIP file, and it must be uncompressed.

Based only on your sample code, it is not clear if you indicate that the mimetype file should be STORED (uncompressed).

The following seems to get me past the "mimetype contains wrong type" error:

 private void writeMimeType(ZipOutputStream zip) throws IOException { byte[] content = "application/epub+zip".getBytes("UTF-8"); ZipEntry entry = new ZipEntry("mimetype"); entry.setMethod(ZipEntry.STORED); entry.setSize(20); entry.setCompressedSize(20); entry.setCrc(0x2CAB616F); // pre-computed zip.putNextEntry(entry); zip.write(content); zip.closeEntry(); } 

Confirmed: deleting the lines setMethod() , setSize() , setCompressedSize() and setCrc() results in the error "mimetype contains the wrong type" from epubcheck .

+7
source

Source: https://habr.com/ru/post/895048/


All Articles