DEV Community


Posted on


Today i was looking for various means to compress all files using PHP without using any third party online tools but unfortunately what i found was zip archives compress, zar and the rest. There was no single library on the internet designed with PHP to handle file compression including compressing zip files. I was curious and i had to design one.

I eventually did the design which i tested with a zip file having size of about 9.5MB and my PHP Class was able to compress this down to 8.2MB which was still okay for me. At least over 1.3MB was removed from the actual zip file.

I know you will want to ask whats the difference. The truth is that assuming you have a whole bunch of those zip files lets say 10 in which each had 9.5MB. Calculating this will give you a bunch of 95MB, thats lots of memory.

Looking through this you will see that over 13MB difference was removed when compressed. This 13MB could be used to save other files.

Designing a class that does this job took me 4 hours with over 15 test analysis.

If you are also curious about me and wish to use my library for your applications below is how to use it.


Use composer to install

composer require mitmelon/compress

Usage :

require_once __DIR__."/vendor/autoload.php";

// Initialize library class
$compress = new Compress\Compress;

 * @param $filePath string
 * File location to be compressed
 * @param $storePath string
 * Path to output compressed binary file to
$compress::compressFile($filePath, $storePath);

//Compress Image file
$compress::compressFile(__DIR__.'/image.png', __DIR__.'/image.txt'));

// Compress PDF
$compress::compressFile(__DIR__.'/file.pdf', __DIR__.'/file.txt'));
//Compress as lot files you want including zip files

 * UnCompress Image file [Get original file back from stored binary]
 * @param $storePath string
 * Path containing binary file which was compressed
 * @param $fileOutputPath string
 * Path to output original file to
$compress::uncompressFile($storePath, $fileOutputPath);

//Uncompress Image
$compress::uncompressFile(__DIR__.'/image.txt', __DIR__.'/image.png');

// Uncompress PDF
$compress::uncompressFile( __DIR__.'/file.txt', __DIR__.'/file.pdf')

You can also find the github library here.

Please feel free to make a pull request incase you find any bugs or want to add something to it.

Best of wishes.

Discussion (2)

bycelso profile image
Celso • Edited

Hello Friend.
I'm from Brazil, forgive me my English.
I did the test of its class the PDF file is created normally but when trying to open the file an error appears in the PDF reader. "dis that this file cannot be loaded"

manomitehq profile image
Adeyeye George • Edited

Sorry for the late reply...

The original file must have been corrupted...

Try to recompress it and uncompress same file... It should work...