Download the PHP package pinimize/laravel-compression-and-archive without Composer
On this page you can find all versions of the php package pinimize/laravel-compression-and-archive. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download pinimize/laravel-compression-and-archive
More information about pinimize/laravel-compression-and-archive
Files in pinimize/laravel-compression-and-archive
Package laravel-compression-and-archive
Short Description A Laravel package for compression and archiving
License MIT
Homepage https://github.com/pinimize/laravel-compression-and-archive
Informations about the package laravel-compression-and-archive
Pinimize
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.
Paused
Development on this project is currently paused because of the lack of time to maintain it.
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
- Features
- Installation
- Configuration
- Default Driver
- Compression Drivers
- Compression Levels
- Storage Disk
- Environment Variables
- Basic Usage
- String Macros
- Storage Macros
- Compressing Strings
- Compressing Resources
- Compressing Files
- Using Storage Disks
- Supported Data Types
- Advanced Usage
- Downloading Compressed Files
- Compression Ratio
- Supported Algorithms
- Drivers
- Gzip Driver
- Zlib Driver
- Custom Drivers
- Testing
- Changelog
- Credits
- License
Installation
You can install the package via composer:
Configuration
Publish the configuration file:
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
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'.
Compression Drivers
The package supports two compression drivers: 'gzip' and 'zlib'. Each driver has its own configuration options:
Gzip Driver
Zlib Driver
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
For both drivers, you can set the compression level:
-1
: default compression (recommended for most cases)0
: no compression1
: fastest compression9
: best compression
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
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 driverZLIB_LEVEL
: Set the compression level for the zlib driverCOMPRESSION_DISK
: Set the storage disk for file operations
Remember to update your .env
file with these variables as needed.
Basic Usage
String Macros
The Pinimize package extends Laravel's Str
facade with two convenient macros for string compression and decompression:
These macros allow you to easily compress & decompress data using the default compression driver.
To decompress compressed data:
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
The Pinimize package extends Laravel's Storage
facade with two convenient methods for file compression and decompression:
Compression:
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 tofalse
.$driver
: (Optional) The compression driver to use (e.g., 'gzip', 'zlib'). If null, it will use the default driver.
Return Value:
- If
$destination
is provided: Returnstrue
on success,false
on failure. - If
$destination
is null: Returns the path of the compressed file on success,false
on failure.
Decompression:
Parameters:
$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 tofalse
.$driver
: (Optional) The decompression driver to use (e.g., 'gzip', 'zlib'). If null, it will use the default driver.
Return Value:
- If
$destination
is provided: Returnstrue
on success,false
on failure. - If
$destination
is null: Returns the path of the decompressed file on success,false
on failure.
Examples:
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
To compress data to a string, you can use the string
method. Take a look at the Supported Data Types for this method. This method is useful for compressing small amounts of data in memory.
The Decompression facade works in a similar way, only it decompresses the data:
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 for this method.
The Decompression facade works in a similar way, only it decompresses the resource:
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.
The Decompression facade works in a similar way, only it decompresses the resource:
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:
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
:
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
The string
, resource
and put
methods can handle several types of input:
- Strings:
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.
-
PSR-7 StreamInterface:
-
Laravel's File object:
-
Laravel's UploadedFile object:
- PHP resource:
Advanced Usage
Downloading Compressed Files
To create a download response for a compressed file:
This also works for decompressing files:
Compression Ratio
To get the compression ratio between original and compressed data:
Supported Algorithms
To get a list of supported compression algorithms for the current driver:
Drivers
Gzip Driver
The Gzip driver uses the gzencode
function for compression. It supports the FORCE_GZIP
encoding and produces files with a .gz
extension.
Zlib Driver
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
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:
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
Testing
Run the tests with:
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-compression-and-archive with dependencies
illuminate/contracts Version ^11.0|^12.0
maennchen/zipstream-php Version ^3.1
nelexa/zip Version ^4.0
ext-fileinfo Version *
ext-zip Version *
ext-zlib Version *