# Specifying the `zip` Compression Method

> Section in “New and Extended Functionality › I/O and Filesystem” of the book [PHP 7 Explained](https://php7explained.com/) by Sebastian Bergmann and Stefan Priebsch, published by thePHP.cc. The book covers PHP 7.0 to 7.4 and was last updated in November 2020. HTML version: <https://php7explained.com/new-and-extended-functionality/io-and-filesystem/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:

``` php
$zipArchive->setCompressionName('localname', ZipArchive::CM_DEFLATE);
```

Alternatively, you can refer to the item by index:

``` php
$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`.

---

Previous: [Optional Level Parameter for dirname()](https://php7explained.com/new-and-extended-functionality/io-and-filesystem/optional-level-parameter-for-dirname/index.html.md) | Next: [Stream-Based Compression](https://php7explained.com/new-and-extended-functionality/io-and-filesystem/stream-based-compression/index.html.md)
