phpziparchivephp-8.2

Add files to archive without rebuilding archive


I'm trying to add files to a big archive (currently using zip but any format is fine) but any time I add even a small file it will rebuild the archive. Even though this is fairly fast, I want this script to run as often as possible.

Is it possible to somehow add files instantly, for small files at least, with or without compression? Any format other than zip, whether that be compressed or not, to use for this is also fine.

The use case here is creating a single file that can be archived on the cloud without it having to track a lot of files.


Solution

  • It's possible, but you will likely need to write your own code.

    The structure of a zip file is a series of local entries followed by a central directory and end record. You can append to a zip file by overwriting the central directory and end record with a new local entry, and then writing an updated central directory and end record after that. For adding a small entry to a large zip file, that would be much faster than copying the entire zip file to a new one, which is very likely what PHP's ZipArchive is doing.

    The complete copy approach is safer, in that if it is interrupted at some point, you still have the original zip file. If you interrupt the fast approach, you end up with a corrupted zip file.

    The zip format turns out to be well suited to your desired operation. Most other formats would not be, e.g. .tar.gz. You can find a description of the zip file format here.

    Since you say it's ok to not be compressed, then you can make up your own uncompressed format. For each file, write a nul-terminated path name, an eight-byte file length, and then that many bytes of file data. To append another file, you just append. Super simple.