PHPackages                             pinimize/laravel-compression-and-archive - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. pinimize/laravel-compression-and-archive

ActiveLibrary[File &amp; Storage](/categories/file-storage)

pinimize/laravel-compression-and-archive
========================================

A Laravel package for compression and archiving

v0.3.0(1y ago)1152.5k↑93.3%[3 PRs](https://github.com/pinimize/laravel-compression-and-archive/pulls)2MITPHPPHP ^8.2CI passing

Since Aug 26Pushed 1y agoCompare

[ Source](https://github.com/pinimize/laravel-compression-and-archive)[ Packagist](https://packagist.org/packages/pinimize/laravel-compression-and-archive)[ Docs](https://github.com/pinimize/laravel-compression-and-archive)[ RSS](/packages/pinimize-laravel-compression-and-archive/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (12)Versions (8)Used By (2)

Pinimize
========

[](#pinimize)

[![Latest Version on Packagist](https://camo.githubusercontent.com/59609a1841b17188db7808b1745a71a12129b471e6d61994a663432e990db975/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70696e696d697a652f6c61726176656c2d636f6d7072657373696f6e2d616e642d617263686976652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pinimize/laravel-compression-and-archive)[![Total Downloads](https://camo.githubusercontent.com/9e7c403e389df1062a1f8b319b58abf4de70cd57330574a833d1f4ae9e3d440c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70696e696d697a652f6c61726176656c2d636f6d7072657373696f6e2d616e642d617263686976652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pinimize/laravel-compression-and-archive)[![Tests](https://github.com/pinimize/laravel-compression-and-archive/actions/workflows/phpunit.yml/badge.svg?branch=main)](https://github.com/pinimize/laravel-compression-and-archive/actions/workflows/phpunit.yml)[![License](https://camo.githubusercontent.com/59848327feecd29b0c4878077551cb7d3f4342d74fc38baf67a02e73c89e3972/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f70696e696d697a652f6c61726176656c2d636f6d7072657373696f6e2d616e642d617263686976652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/pinimize/laravel-compression-and-archive)

Pinimize is a powerful Laravel package that simplifies file compression and decompression. It provides a clean and intuitive API for handling various compression and decompression tasks in your Laravel applications, with full support for Laravel's Storage system.

Archiving and unarchiving operations are in **coming very soon**.

[![Logo with brown western bar doors with western scene in background and text that says: Saloon, Your Lone Star of your API integrations](/docs/logo.jpg)](/docs/logo.jpg)

Paused
------

[](#paused)

Development on this project is currently paused because of the lack of time to maintain it.

Features
--------

[](#features)

- File compression and decompression
- String compression and decompression
- Stream compression and decompression
- File archiving and unarchiving
- Support for multiple compression algorithms and archive formats
- Facade-based API for easy integration
- Full integration with Laravel's Storage system
- Extensible architecture with support for custom drivers
- Actively maintained and regularly updated
- Written in clean, modern PHP code

Table of Contents
-----------------

[](#table-of-contents)

- [Features](#features)
- [Installation](#installation)
- [Configuration](#configuration)
    - [Default Driver](#default-driver)
    - [Compression Drivers](#compression-drivers)
    - [Compression Levels](#compression-levels)
    - [Storage Disk](#storage-disk)
- [Environment Variables](#environment-variables)
- [Basic Usage](#basic-usage)
    - [String Macros](#string-macros)
    - [Storage Macros](#storage-macros)
    - [Compressing Strings](#compressing-strings)
    - [Compressing Resources](#compressing-resources)
    - [Compressing Files](#compressing-files)
    - [Using Storage Disks](#using-storage-disks)
- [Supported Data Types](#supported-data-types)
- [Advanced Usage](#advanced-usage)
    - [Downloading Compressed Files](#downloading-compressed-files)
    - [Compression Ratio](#compression-ratio)
    - [Supported Algorithms](#supported-algorithms)
- [Drivers](#drivers)
    - [Gzip Driver](#gzip-driver)
    - [Zlib Driver](#zlib-driver)
- [Custom Drivers](#custom-drivers)
- [Testing](#testing)
- [Changelog](#changelog)
- [Credits](#credits)
- [License](#license)

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require pinimize/laravel-compression-and-archive
```

Configuration
-------------

[](#configuration)

Publish the configuration file:

```
php artisan vendor:publish --provider="Pinimize\PinimizeServiceProvider" --tag="config"
```

The `config/pinimize.php` file allows you to configure the compression and decompression settings for the Pinimize package. This file is created when you publish the package configuration.

### Default Driver

[](#default-driver)

You can set the default compression driver using the `COMPRESSION_DRIVER` environment variable or by directly modifying the `compression.default` value in the config file. The default is set to 'gzip'.

```
'default' => env('COMPRESSION_DRIVER', 'gzip'),
```

### Compression Drivers

[](#compression-drivers)

The package supports two compression drivers: 'gzip' and 'zlib'. Each driver has its own configuration options:

#### Gzip Driver

[](#gzip-driver)

```
'gzip' => [
    'level' => env('GZIP_LEVEL', -1),
    'encoding' => FORCE_GZIP,
    'disk' => env('COMPRESSION_DISK', null),
],
```

#### Zlib Driver

[](#zlib-driver)

```
'zlib' => [
    'level' => env('ZLIB_LEVEL', -1),
    'encoding' => ZLIB_ENCODING_DEFLATE,
    'disk' => env('COMPRESSION_DISK', null),
],
```

More drivers will be added in future releases. They are kept separate to avoid requiring you to install php extensions you might not need and to keep the codebase clean.

### Compression Levels

[](#compression-levels)

For both drivers, you can set the compression level:

- `-1`: default compression (recommended for most cases)
- `0`: no compression
- `1`: fastest compression
- `9`: best compression

### Storage Disk

[](#storage-disk)

The `disk` option allows you to specify which disk to use for file operations. This integrates with Laravel's Storage system:

- If set to `null` (default), the local filesystem will be used.
- You can set it to any configured disk in your `config/filesystems.php` file.

To set the disk, use the `COMPRESSION_DISK` environment variable or modify the `disk` value directly in the config file.

Environment Variables
---------------------

[](#environment-variables)

For easy configuration, you can use the following environment variables:

- `COMPRESSION_DRIVER`: Set the default compression driver ('gzip' or 'zlib')
- `GZIP_LEVEL`: Set the compression level for the gzip driver
- `ZLIB_LEVEL`: Set the compression level for the zlib driver
- `COMPRESSION_DISK`: Set the storage disk for file operations

Remember to update your `.env` file with these variables as needed.

Basic Usage
-----------

[](#basic-usage)

### String Macros

[](#string-macros)

The Pinimize package extends Laravel's `Str` facade with two convenient macros for string compression and decompression:

```
Str::compress($data)

// and

Str::decompress($compressedData)
```

These macros allow you to easily compress &amp; decompress data using the default compression driver.

```
use Illuminate\Support\Str;

// Using the default driver
$originalString = "This is a long string that will be compressed.";
$compressedString = Str::compress($originalString);

// Specifying a driver
$compressedStringGzip = Str::compress($originalString, 'gzip');
$compressedStringZlib = Str::compress($originalString, 'zlib');
```

To decompress compressed data:

```
use Illuminate\Support\Str;

$decompressedString = Str::decompress($compressedString);

// Specifying a driver
$decompressedStringGzip = Str::decompress($compressedStringGzip, 'gzip');
$decompressedStringZlib = Str::decompress($compressedStringZlib, 'zlib');
```

These macros provide a simple and convenient way to compress and decompress strings in your Laravel application, leveraging the power of the Pinimize package.

### Storage Macros

[](#storage-macros)

The Pinimize package extends Laravel's `Storage` facade with two convenient methods for file compression and decompression:

#### Compression:

[](#compression)

```
use Illuminate\Support\Facades\Storage;

Storage::compress(
    string $source,
    ?string $destination = null,
    bool $deleteSource = false,
    ?string $driver = null
): bool|string;
```

#### Parameters:

[](#parameters)

- `$source`: The path to the source file (relative to the storage disk).
- `$destination`: (Optional) The path where the compressed file should be saved. If null, it will use the source filepath with the extension appended.
- `$deleteSource`: (Optional) Whether to delete the source file after successful compression. Defaults to `false`.
- `$driver`: (Optional) The compression driver to use (e.g., 'gzip', 'zlib'). If null, it will use the default driver.

#### Return Value:

[](#return-value)

- If `$destination` is provided: Returns `true` on success, `false` on failure.
- If `$destination` is null: Returns the path of the compressed file on success, `false` on failure.

#### Decompression:

[](#decompression)

```
use Illuminate\Support\Facades\Storage;

Storage::decompress(
    string $source,
    ?string $destination = null,
    bool $deleteSource = false,
    ?string $driver = null
): string|bool;
```

#### Parameters:

[](#parameters-1)

- `$source`: The path to the compressed file (relative to the storage disk).
- `$destination`: (Optional) The path where the decompressed file should be saved. If null, it will use `$source` with the compression extension removed.
- `$deleteSource`: (Optional) Whether to delete the source file after successful decompression. Defaults to `false`.
- `$driver`: (Optional) The decompression driver to use (e.g., 'gzip', 'zlib'). If null, it will use the default driver.

#### Return Value:

[](#return-value-1)

- If `$destination` is provided: Returns `true` on success, `false` on failure.
- If `$destination` is null: Returns the path of the decompressed file on success, `false` on failure.

### Examples:

[](#examples)

```
use Illuminate\Support\Facades\Storage;

// Compress a file with default settings
$compressResult = Storage::compress('large_file.txt');
if ($compressResult !== false) {
    echo "File compressed successfully to: $compressResult\n";
}

// Compress a file with custom destination and driver
$compressResult = Storage::compress('document.pdf', 'compressed_doc.pdf.gz', false, 'zlib');
if ($compressResult === true) {
    echo "File compressed successfully\n";
}

// Decompress a file with default settings
$decompressResult = Storage::decompress('large_file.txt.gz');
if ($decompressResult !== false) {
    echo "File decompressed successfully to: $decompressResult\n";
}

// Decompress a file, delete the source, and use a specific driver
$decompressResult = Storage::decompress('archive.tar.gz', 'extracted_archive.tar', true, 'gzip');
if ($decompressResult === true) {
    echo "File decompressed and compressed version deleted\n";
}
```

These methods provide a simple and convenient way to compress and decompress files in your Laravel application's storage system, leveraging the power of the Pinimize package.

### Compressing Strings

[](#compressing-strings)

To compress data to a string, you can use the `string` method. Take a look at the [Supported Data Types](#supported-data-types) for this method. This method is useful for compressing small amounts of data in memory.

```
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Compression;

// With string input
$compressed = Compression::string('Hello, World!');

// With file input
$compressed = Compression::string(new File($path));

// With uploaded input
$compressed = Compression::string(new UploadedFile($path));
// etc.
```

The Decompression facade works in a similar way, only it decompresses the data:

```
use Illuminate\Support\Facades\Decompression;

$data = gzencode('Hello, World!');
$compressed = Decompression::string($data); // Hello, World!
```

### Compressing Resources

[](#compressing-resources)

You can compress data and have the compressed content returned as a resource by using the `resource` method. This method useful for working with data without loading everything into memory.

Take a look at the [Supported Data Types](#supported-data-types) for this method.

```
use Illuminate\Support\Facades\Compression;

// string input
$compressedResource = Compression::resource("I'm too big, make me smaller please!");

// resource input
$resource = fopen('path/to/input.txt', 'r');
$compressedResource = Compression::resource($resource);
```

The Decompression facade works in a similar way, only it decompresses the resource:

```
use Illuminate\Support\Facades\Decompression;

// resource input
$resource = fopen('path/to/compressed_data.txt.gz', 'r');

$decompressedResource = Decompression::resource($resource);
```

### Compressing Files

[](#compressing-files)

The `put` method is a versatile way to write compressed content to a file. It supports various input types and offers flexibility in where the compressed file is stored.

```
use Illuminate\Http\File;

// Storing on a local disk
Compression::put('local_output.gz', 'Content');

// Compressing a file and storing on a local disk
Compression::put('ftp_output.gz', new File($path));

// Storing on Google Cloud Storage
Compression::put('gcs_output.gz', 'Content');
```

The Decompression facade works in a similar way, only it decompresses the resource:

```
use Illuminate\Support\Facades\Decompression;

// resource input
$resource = fopen('path/to/compressed.txt.gz', 'r');

$decompressedResource = Decompression::put('local_output.txt', $resource);
$decompressedResource = Decompression::put('local_output.txt', 'path/to/compressed.txt.gz');
```

### Using Storage Disks

[](#using-storage-disks)

By default, the `put` method will use the filesystem provided in then config file, which defaults to the local filesystem. However, you can use Laravel's Storage facade to write compressed files to any configured disk by specifying the `disk` option:

```
use Illuminate\Support\Facades\Compression;

// Storing on an S3 bucket, the path is relative to the bucket's root
Compression::put('compressed/output.gz', 'Content to compress', [
    'disk' => 's3'
]);
```

This will compress the content and store it on the S3 disk (assuming you have configured an S3 disk in your `filesystems.php` configuration).

You can use any disk configured in your `config/filesystems.php`:

```
// Storing on a local disk
Compression::put('local_output.gz', 'Content', ['disk' => 'local']);

// Storing on an FTP server
Compression::put('ftp_output.gz', 'Content', ['disk' => 'ftp']);

// Storing on Google Cloud Storage
Compression::put('gcs_output.gz', 'Content', ['disk' => 'gcs']);
```

When using the `disk` option, the Compression service will utilize Laravel's Storage facade to handle file operations, allowing you to take advantage of all the benefits of Laravel's filesystem abstraction.

Note that when using storage disks, the path you provide as the first argument to `put` will be relative to the disk's configured root directory.

### Supported Data Types

[](#supported-data-types)

The `string`, `resource` and `put` methods can handle several types of input:

1. **Strings**: ```
    Compression::put('output.gz', 'String content to compress');
    ```

If the provided string is a path to a file, the package will first attempt to locate the file on the specified filesystem. If the file cannot be found, the package will treat the string as raw content to be compressed.

This behavior allows flexibility in handling both file paths and direct content compression within the same interface. If you want to be certain that a file specified by a path should be loaded, consider using the `Illuminate\Http\File` object or passing it in as a resource.

2. **PSR-7 StreamInterface**:

    ```
    use GuzzleHttp\Psr7\Stream;

    $stream = new Stream(fopen('path/to/file.txt', 'r'));
    Compression::put('output.gz', $stream);
    ```
3. **Laravel's File object**:

    ```
    use Illuminate\Http\File;

    $file = new File('path/to/file.txt');
    Compression::put('output.gz', $file);
    ```
4. **Laravel's UploadedFile object**:

    ```
    // In a controller method handling file upload
    public function handleUpload(Request $request)
    {
        $uploadedFile = $request->file('document');
        Compression::put('compressed_upload.gz', $uploadedFile);
    }
    ```
5. **PHP resource**:

    ```
    $resource = fopen('path/to/file.txt', 'r');
    Compression::put('output.gz', $resource);
    ```

Advanced Usage
--------------

[](#advanced-usage)

### Downloading Compressed Files

[](#downloading-compressed-files)

To create a download response for a compressed file:

```
return Compression::download('path/to/file.txt', 'downloaded_file.gz');
```

This also works for decompressing files:

```
return Decompression::download('path/to/file.txt.gz', 'file.txt');
```

### Compression Ratio

[](#compression-ratio)

To get the compression ratio between original and compressed data:

```
$ratio = Compression::getRatio('original_content', 'compressed_content');
```

### Supported Algorithms

[](#supported-algorithms)

To get a list of supported compression algorithms for the current driver:

```
$algorithms = Compression::getSupportedAlgorithms();
```

Drivers
-------

[](#drivers)

### Gzip Driver

[](#gzip-driver-1)

The Gzip driver uses the `gzencode` function for compression. It supports the `FORCE_GZIP` encoding and produces files with a `.gz` extension.

### Zlib Driver

[](#zlib-driver-1)

The Zlib driver uses the `zlib_encode` function for compression. It supports `ZLIB_ENCODING_RAW`, `ZLIB_ENCODING_GZIP`, and `ZLIB_ENCODING_DEFLATE` encodings. Files compressed with this driver have a `.zz` extension.

Custom Drivers
--------------

[](#custom-drivers)

You can create custom compression drivers by extending the `CompressionContract` interface and implementing the required methods. Then, register your custom driver in a service provider:

```
use Illuminate\Support\Facades\Compression;

public function boot()
{
    Compression::extend('custom', function ($app) {
        return new CustomCompressionDriver($app['config']['compression.custom']);
    });
}
```

After registering your custom driver, you can use it like any other compression driver in your application. The same process applies for creating custom decompression drivers.

Links
-----

[](#links)

- Article - [Simplifying File Compression in Laravel with Pinimize](https://einarhansen.dev/articles/2024-08-27-laravel-new-compression-package-pinimize)

Testing
-------

[](#testing)

Run the tests with:

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Credits
-------

[](#credits)

- [Einar Hansen](https://github.com/einar-hansen)
- [All Contributors](https://github.com/pinimize/laravel-compression-and-archive/graphs/contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance44

Moderate activity, may be stable

Popularity35

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~61 days

Total

4

Last Release

446d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7867c8e6cb949ab73460f6189d49e8423431864c495a389e650e82509e44d954?d=identicon)[einar-hansen](/maintainers/einar-hansen)

---

Top Contributors

[![einar-hansen](https://avatars.githubusercontent.com/u/49709354?v=4)](https://github.com/einar-hansen "einar-hansen (27 commits)")

---

Tags

laravelcompressionpinimize

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/pinimize-laravel-compression-and-archive/health.svg)

```
[![Health](https://phpackages.com/badges/pinimize-laravel-compression-and-archive/health.svg)](https://phpackages.com/packages/pinimize-laravel-compression-and-archive)
```

###  Alternatives

[spatie/laravel-google-cloud-storage

Google Cloud Storage filesystem driver for Laravel

2408.9M13](/packages/spatie-laravel-google-cloud-storage)[spatie/livewire-filepond

Upload files using Filepond in Livewire components

306452.7k3](/packages/spatie-livewire-filepond)[sopamo/laravel-filepond

Laravel backend module for filepond uploads

215272.2k3](/packages/sopamo-laravel-filepond)[elegantly/laravel-invoices

Store invoices safely in your Laravel application

23131.8k](/packages/elegantly-laravel-invoices)[mwguerra/filemanager

A full-featured file manager package for Laravel and Filament v5 with dual operating modes, drag-and-drop uploads, S3/MinIO support, and comprehensive security features.

718.5k1](/packages/mwguerra-filemanager)[kalynasolutions/laravel-tus

Laravel package for handling resumable file uploads with tus protocol and native Uppy.js support without additional tus servers

5261.8k1](/packages/kalynasolutions-laravel-tus)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
