[GH-ISSUE #998] Add ZipArchive::setCompressionName for systems that support it #644

Open
opened 2026-03-02 16:00:30 +03:00 by kerem · 0 comments
Owner

Originally created by @ner00 on GitHub (Mar 22, 2023).
Original GitHub issue: https://github.com/prasathmani/tinyfilemanager/issues/998

I've looked at this and implemented it for myself, but I think this should be carefully implemented with an option in the settings for people to use it properly. The most useful part of not using compression at all is to just pack a whole folder tree into one zip file and then download it.

When comparing the multiple compression options for setCompressionName (available only for PHP7+), I was very surprised at how fast no compression could be. For example, a folder tree that has 33 files and 2 subfolders, totaling 837MB, would take around 25 seconds to compress with the default compression method (deflate) and quite a bit of CPU power too. In comparison, using no compression at all (CM_STORE), it took 3 seconds and barely any CPU power. Since the data itself was FLAC audio, the end result in terms of data volume was virtually identical.

This is my own crude implementation at the moment, inside FM_Zipper->create() for files and FM_Zipper->addDir() for recursive folders:

    $zip_compression = 'none'; // none (store), deflate, lzma2 (xz)
    $zip_compression == 'deflate' ? $c = ZipArchive::CM_DEFLATE : ($zip_compression == 'lzma2' ? $c = ZipArchive::CM_XZ: $c = ZipArchive::CM_STORE);
    if (method_exists('ZipArchive', 'setCompressionName')) {
        $this->zip->setCompressionName($path . '/' . $file, $c); // CM_STORE, CM_DEFLATE, CM_XZ
    }

I could also have added isCompressionMethodSupported as a control but that's even worse seeing as it only exists in PHP8+, so I'm not checking if the compressions are actually supported and only if the method of setting a compression is supported.

Originally created by @ner00 on GitHub (Mar 22, 2023). Original GitHub issue: https://github.com/prasathmani/tinyfilemanager/issues/998 I've looked at this and implemented it for myself, but I think this should be carefully implemented with an option in the settings for people to use it properly. The most useful part of not using compression at all is to just pack a whole folder tree into one zip file and then download it. When comparing the multiple compression options for `setCompressionName` (available only for PHP7+), I was very surprised at how fast no compression could be. For example, a folder tree that has 33 files and 2 subfolders, totaling 837MB, would take around 25 seconds to compress with the default compression method (deflate) and quite a bit of CPU power too. In comparison, using no compression at all (CM_STORE), it took 3 seconds and barely any CPU power. Since the data itself was FLAC audio, the end result in terms of data volume was virtually identical. This is my own crude implementation at the moment, inside `FM_Zipper->create()` for files and `FM_Zipper->addDir()` for recursive folders: ```php $zip_compression = 'none'; // none (store), deflate, lzma2 (xz) $zip_compression == 'deflate' ? $c = ZipArchive::CM_DEFLATE : ($zip_compression == 'lzma2' ? $c = ZipArchive::CM_XZ: $c = ZipArchive::CM_STORE); if (method_exists('ZipArchive', 'setCompressionName')) { $this->zip->setCompressionName($path . '/' . $file, $c); // CM_STORE, CM_DEFLATE, CM_XZ } ```` I could also have added `isCompressionMethodSupported` as a control but that's even worse seeing as it only exists in PHP8+, so I'm not checking if the compressions are actually supported and only if the method of setting a compression is supported.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tinyfilemanager#644
No description provided.