Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a file to a zip uncompressed with PHP

Tags:

php

zip

epub

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.

like image 525
usertest Avatar asked Feb 26 '23 18:02

usertest


1 Answers

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.

like image 114
Sajid Avatar answered Mar 08 '23 03:03

Sajid