tags:

views:

36

answers:

2

I'm looking at a tutorial for creating an ePub file. It states that the zip that contains the ePub book must contain a textfile called mimetype that "must be first in the zip file, uncompressed". The example he gives uses a commandline tool, I was wondering how I could do the same thing in PHP.

I assume it would be first in the zip file as long as its the first thing I add in the code, but how to add it to the zip uncompressed. Or am I misreading this?

Thanks in advance.

A: 

You cannot do that with the native PHP ZipArchive class. But PEAR::Archive_Zip can - if you use the ARCHIVE_ZIP_PARAM_NO_COMPRESSION parameter when adding that specifc file.

A simpler solution would be to use a template. Create a stub zip file with your uncompressed "mimetype" entry (zip -0), then use that as temporary zip and afterwards just add new entries to it:

file_put_contents("epub.zip", base64_decode("UEsDBArvlt08b2GrLBQUCBxtaW1ldHlwZVVUCQOCJSpMgiUqTHV4CwEE6AME6ANhcHBsaWNhdGlvbi9lcHViK3ppcFBLAQIeAwrvlt08b2GrLBQUCBikgW1pbWV0eXBlVVQFA4IlKkx1eAsBBOgDBOgDUEsFBgEBTlY="));
$zip = new ZipArchive();
$zip->open("epub.zip");

$zip->addFiles(...);

(untested though)

mario
A: 

It seems the base64-encoded file above is a little buggy (ZipArchive refused to open it), but the following works:

// make the archive first            
file_put_contents($fileName, base64_decode("UEsDBAoAAAAAAOmRAT1vYassFAAAABQAAAAIAAAAbWltZXR5cGVhcHBsaWNhdGlvbi9lcHViK3ppcFBLAQIUAAoAAAAAAOmRAT1vYassFAAAABQAAAAIAAAAAAAAAAAAIAAAAAAAAABtaW1ldHlwZVBLBQYAAAAAAQABADYAAAA6AAAAAAA="));

// open archive
if (($err = $zipfile->open($fileName)) !== TRUE) {
    trigger_error("Could not open archive: " . $fileName, E_USER_ERROR);
}

$zipfile->add(...)

I tested this with my own epub-generating code and it worked fine. Epubcheck 1.05 validates it. By the way, if you're using "OPL's EPUB library", beware that it's quite buggy. I will probably post a fix to it soon with this solution baked in, but beware till then.

Sajid