Specifying the
zip
Compression Method
If the zip
extension is installed, PHP can create
and un-compress zip files. When adding content to a zip file, either
from a file using the addFile()
method, or from a
string using the addFromString()
method, you can
specify a so-called local name.
PHP 7 adds a new feature that allows you to specify the compression method individually for each item. To do this, you refer to the local name:
$zipArchive->setCompressionName('localname', ZipArchive::CM_DEFLATE);
Alternatively, you can refer to the item by index:
$zipArchive->setCompressionIndex(0, ZipArchive::CM_DEFLATE);
Setting individual compression methods for each file might reduce the overall size, since the most suitable method can be chosen. For content that is already compressed, like JPEG images, for instance, just adding them to the archive without re-compression will save on CPU time. After all, it usually does not make sense to re-recompress content, even if the compression method is different.
You can choose from the three compression methods
ZipArchive::CM_DEFAULT
,
ZipArchive::CM_STORE
which will just store the content
without compressing it, and ZipArchive::CM_DEFLATE
.