File Compresser 2.0.0 is here
Tags: php, programming, opensource, github
Published: 2022-03-12 18:52:05

2 years ago I released a library called **compress** which compresses all files including zip, tar etc. using PHP without using any third party online tools.

At the initial testing, compress compressed a zip file from 9.5MB to 8.2MB, over 1.3MB was removed from the actual zip file which was cool.

Since the release, over 1,450k developers have downloaded the library without issues. This has been a very great score for me and has given me more strength to keep working on the library.

Today am here to announce that the version 2 of Compress is now available. 

In the version 2, I included AES-256-CBC encryption function for encrypting the files before compressing.

If you are upgrading to the new version, please note that there are few changes you will need to make to your code.

CompressFile() third argument now accepts optional array options. You can take a look at the changes below and compare it with the old version before upgrading.

If you are going to be using 
the encryption function, You have to set the compressFile option argument like this;

```php
$compress = new Compress\Compress;

$options = ["removeMeta" => false, "encrypt" => false, "key" => "password"];

$compress::compressFile($filePath, $storePath, $options);

```

Let me explain the option values.

**The removeMeta** : removes additional comments and junks from file. You can set this to true if you want to remove meta from file.

**Encrypt** : Set this to true if you want to use encryption.

**Key** : If you set encrypt to true, then you must provide the encryption key else compress will generate one for you and the response will be the key generated so you can store it in your database vault for future decryptions.

Now let's look at the few changes in the whole new version.

## Install:
Use composer to install
```php
composer require mitmelon/compress
```

## Usage :

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

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

/**
 * @param String $filePath
 * File location to be compressed
 * @param String $storePath
 * Path to output compressed binary file to
 * @param Array optional $options ["removeMeta" => false, "encrypt" => false, "key" => "password"]
 * Options to remove meta and encrypt file
 */
$compress::compressFile($filePath, $storePath, $options = []);

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

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

/**
 * UnCompress Image file [Get original file back from stored binary]
 * @param String $storePath
 * Path containing binary file which was compressed
 * @param String $fileOutputPath
 * Path to output original file to
 * @param String $encrypt_key
 * If your file was encrypted then provide the key for decryption as third argument
 */
$compress::uncompressFile($storePath, $fileOutputPath, $encrypt_key = null);

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

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

```
If you have not yet started using Compress and wish to, you can check it out using the link below;

https://github.com/mitmelon/Compress

In the near future, I will be adding more features. You can check the feature update on the readme to see the next features that will be added.

Also don't forget to donate for the project progress. Your donation will really help me on a long run.

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

Best of wishes.