1

I'm in the process of writing some epub creation functionality using php5. Currently I am attempting to use ZipArchive but have run into a couple annoyances with it. First of all, there is no functionality to set the compression level. Second of all, ZipArchive::addFile() seems to fail silently and create a corrupt archive whenever I use it. I have been using file_get_contents() + ZipArchive::addFromString() instead but would prefer to just use the documented function for adding files.

I will not post code samples unless someone would really like to help me debug this issue, but rather I'm wondering if there are any other libraries for creating zip (pkzip) archives in PHP that you would recommend. So far, I have seen PclZip, whose site does not seem to be loading, but not much else. I have also considered using exec() + zip (unix command). This code will only run on this one particular linux box so portability is not an issue.

Thanks in advance for any suggestions!

hakre
  • 193,403
  • 52
  • 435
  • 836
nini
  • 123
  • 10
  • depending what machine you are running this on you could use the Linux command line zip. I prefer this for small jobs. –  Jan 10 '11 at 21:36

2 Answers2

1

I'd use exec() and the Unix command. A native-to-the-system way to solve the problem - the unix utils will always be a step or two ahead from their PEAR counterparts.

Alex Weinstein
  • 9,823
  • 9
  • 42
  • 59
1

PCLZip is pretty good alternative, with zlib as its only dependency, if you can get access to the site. It's probably temporary, it was certainly accessible between Christmas and New Year.

It's also pretty efficient, even in comparison with ZipArchive

EDIT

You say that you've had problems with ZipArchive's addFile() method. Is this in a Windows environment, or on your Linux server? I know that there have been a few buggy releases of the php_zip library on Win32 that can give this problem, although the latest versions seem OK, and I've not encountered the same problem on other platforms (even the WIN64 version).

Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • I'm having trouble with it on the linux dev server. The actual addFile call is returning true, but a file of size 0 is being added, causing the archive to be unreadable. There is one comment on php.net (http://www.php.net/manual/en/function.ziparchive-addfile.php#84529) which says to use a relative path to the file on the server otherwise it will cause an error. I am using an absolute path, so I'm taking a look at that as a last resort. – nini Jan 10 '11 at 22:28
  • Have you looked at the responses to this question? http://stackoverflow.com/questions/4651276/php-no-error-when-creating-zip-but-it-doesnt-get-created – Mark Baker Jan 10 '11 at 22:41
  • Ah, I see, I think that's my problem with addFile. Thanks for the link. It would be great if addFile actually threw a proper error while trying to add, but what can you do. I'm still having trouble with my epub file, however, because one file in the archive must have 0 compression, and I'm unable to set it with ZipArchive. The epub won't open correctly in a reader without this file correctly archived, so as far as I can tell I will have to use something other than ZipArchive to do it. – nini Jan 11 '11 at 16:42
  • It looks like the PCLZip site is still unavailable; but that library does allow adding a file to an archive with a PCLZIP_OPT_NO_COMPRESSION option... I haven't used that option myself, though I've been reasonably happy with the library generally. The only real grouse that I've had with it is no built-in method to add a file to an archive from a string of data rather than from an actual filename, the equivalent of ZipArchive's addFromString(), but I'll probably end up extending/modifying the class to provide that option. – Mark Baker Jan 11 '11 at 17:12
  • Yep, that's the method I was using to create the epub (addFromString), but I worked around it. I grabbed the PCLZip source from a mirror and plugged it in, and everything is working perfectly, and I'm producing a valid epub now. Thanks for the suggestions :) – nini Jan 12 '11 at 00:59