PHP Luminova: Create ZIP Archives
Package a file or directory into a ZIP archive, exclude entries with patterns, and track file counts.
Use Archive when you need to package local files for export or backup. ZIP creation requires PHP’s Zip extension. Use a writable destination outside the directory being archived, and choose a new output name to preserve an existing archive.
Usage
Run this from a bootstrapped Luminova application. Set $reportPath to an existing local file and $zipPath to a writable destination such as /tmp/report.zip.
use Luminova\Storage\Archive;
$added = 0;
$skipped = 0;
if (Archive::zip($zipPath, $reportPath, added: $added, skipped: $skipped)) {
echo "Files counted as added: {$added}";
}Class Definition
- Full namespace:
Luminova\Storage\Archive - This class is a concrete class.
Methods
zip
Create or overwrite a ZIP archive. For a directory, traverse its entries and apply the ignore patterns to candidate paths and basenames. The current implementation can store entry names with a leading slash. Inspect the stored names and file contents before distributing a ZIP.
public static function zip(
string $destination,
string $source,
array $ignores = [],
int &$added = 0,
int &$skipped = 0,
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$destination | string | Destination ZIP path. |
$source | string | Existing local file or directory. |
$ignores | string[] | Glob patterns for excluded paths or basenames (default: []). |
$added | int | Output counter for files counted as added (default: 0). |
$skipped | int | Output counter for skipped entries (default: 0). |
Return:
bool - true when at least one file was counted as added. The counter does not confirm that each write or archive close succeeded.
Throws:
- Luminova\Exceptions\FileException if an exception occurs during ZIP creation.
tarGz
Build a TAR archive using Phar and request Gzip compression. The current implementation then renames the original TAR over the compressed output. A successful result can therefore leave an uncompressed TAR with a .tar.gz suffix. Use zip() when you need a compressed archive from this class.
public static function tarGz(string $destination, string $source): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$destination | string | Output path or basename. A trailing .tar or .tar.gz is normalized to .tar.gz. |
$source | string | Existing local file or directory. |
Return:
bool - Rename result, or false when the source or intermediate TAR is missing.
Throws:
- Luminova\Exceptions\FileException if creating or compressing the TAR throws an exception.