Adding a file to zip without compression using PHP

I am looking at a tutorial for creating an ePub file. It states that the zip containing the ePub book should contain a text file named mimetype, which "should be the first in the zip file, uncompressed." In the example that he gives, the command line tool is used, I was wondering how I can do the same in PHP.

I assume that it will be the first in the zip file, if its the first that I will add to the code, but how to add it to a ZIP file without compression. Or am I misunderstanding this?

Thanks in advance.

+4
source share
3 answers

You cannot do this with your own PHP class ZipArchive. But PEAR :: Archive_Zip can - if you use the ARCHIVE_ZIP_PARAM_NO_COMPRESSION parameter when adding this specified file.

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

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

(not verified)

+1
source

It seems that the base64 encoded file described above is a bit wrong (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(...) UEsDBAoAAAAAAOmRAT1vYassFAAAABQAAAAIAAAAbWltZXR5cGVhcHBsaWNhdGlvbi9lcHViK3ppcFBLAQIUAAoAAAAAAOmRAT1vYassFAAAABQAAAAIAAAAAAAAAAAAIAAAAAAAAABtaW1ldHlwZVBLBQYAAAAAAQABADYAAAA6AAAAAAA =")); // 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 generation code and it worked fine. Epubcheck 1.05 checks it. By the way, if you use the "OPL EPUB library", beware that it is pretty buggy. I’ll probably be posting a fix soon so that this solution is done, but be careful before that.

+5
source

I am currently working on an epub export tool using PHP, and I had good experience using PCLZip . It has the PCLZIP_OPT_NO_COMPRESSION option, which I use when I call add () when adding a file. I use this when adding a mimetype file and it works like a charm.

+2
source

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


All Articles