UnifiedArchive PHP Library

repository·master·Indexed 43 days ago

https://github.com/wapmorgan/unifiedarchive

A PHP library providing a unified interface for managing various archive formats including zip, rar, 7z, tar, and more. It abstracts underlying drivers—ranging from PHP extensions and system utilities to pure PHP implementations—to ensure consistent archive management across different system environments. Includes a built-in console archive manager (cam) for CLI-based archive manipulation, inspection, and driver configuration.

Tokens
21.8K
Snippets
85
Records
104
Agent score
48%

What's inside UnifiedArchive

  1. Choose an installation driver type

    master

    UnifiedArchive supports three types of drivers, each with different trade-offs regarding speed, memory usage, and feature support:

    • PHP Extensions: The fastest option. Use these when the corresponding PHP extension is installed on your system. They support a wide range of functions but may lack specific features on older PHP versions (e.g., password protection in zip).
    • Utilities + Bridge: Uses system-level utilities (like 7za, tar, or unzip). These are very fast and suitable for large files or full archive extraction, but they do not support streaming.
    • Pure PHP: A fallback method using Composer-installed libraries. These work without extensions or system utilities but are slower and consume significantly more memory for large archives.
  2. Understand driver capability codes

    master

    When viewing the support matrix (e.g., via ./vendor/bin/cam system:formats), the drivers' capabilities are represented by single-letter codes:

    • o: open
    • O: open (+password)
    • t: get comment
    • x: extract
    • s: stream
    • a: append
    • d: delete
    • T: set comment
    • c: create
    • C: create (+password)
  3. How UnifiedArchive drivers work

    master

    UnifiedArchive uses a tiered driver system to ensure maximum compatibility across different system configurations. When you perform an operation, the library selects the best available driver using a top-down approach:

    1. PHP Extensions: Highest performance (e.g., zip, phar).
    2. Utilities + bridge: Uses system CLI tools (e.g., SevenZip, AlchemyZippy).
    3. Pure PHP: Fallback implementation (e.g., NelexaZip, SplitbrainPhpArchive).

    Because the interface is unified, your code remains the same regardless of which driver is actually performing the work. For example, zip format is supported in any configuration via zip/phar extensions, SevenZip utilities, or NelexaZip pure PHP.

  4. Resolve dependency conflicts between SevenZip and AlchemyZippy

    master

    If you need to install both SevenZip and AlchemyZippy in the same project, you must manage specific version requirements for symfony/process and archive7z to avoid conflicts:

    1. Force symfony/process to a 3.x.x version:
      composer require symfony/process:~3.4
    2. Install archive7z version 4.0.0:
      composer require gemorroj/archive7z:~4.0
    composer require symfony/process:~3.4
    composer require gemorroj/archive7z:~4.0
  5. Install the TarByPear driver

    master

    To use the TarByPear driver, you must install the pear/archive_tar library via Composer and ensure the necessary PHP extensions are available for your desired compression formats.

    Required Library:

    • pear/archive_tar

    Optional PHP Extensions:

    • zlib (for .tar.gz or .tgz)
    • bz2 (for .tar.bz2 or .tbz2)
    • xz (for .tar.xz)
    • LzwStreamWrapper (for .tar.z via LZW)
    composer require pear/archive_tar
  6. Configure drivers for common environments

    master

    Depending on your environment, follow these installation strategies:

    Common Case

    1. Install TarByPear and NelexaZip via Composer.
    2. If the 7za utility is available on your system, configure the SevenZip driver.
    3. If 7za is not available but tar or unzip utilities are present, configure the AlchemyZippy driver.

    Docker or VDS

    1. Install all necessary PHP extensions: zip, rar, phar, zlib, and bz2.
    2. Install the 7za utility (e.g., apt-get install p7zip-full on Ubuntu).
    3. Configure the SevenZip driver.
  7. Install and configure new drivers

    master

    UnifiedArchive relies on drivers to handle different archive formats. Drivers can be PHP Extensions, Utilities + bridge, or Pure PHP implementations. If a format is not supported, you may need to install additional drivers or system utilities.

    1. View installation instructions for available drivers:
    ./vendor/bin/cam system:drivers
    1. Install a missing PHP library (example for TarByPear):
    composer require pear/archive_tar
    1. Verify the new driver's capabilities:
    ./vendor/bin/cam system:formats TarByPear
    ./vendor/bin/cam system:drivers
  8. Install the RAR extension for UnifiedArchive

    master

    The Rar driver requires the PHP ext-rar extension to function. You can install it using PECL:

    pecl install rar
    pecl install rar
  9. Install UnifiedArchive via Composer

    master

    To add UnifiedArchive to your project, use Composer to require the package. After installation, you can use the built-in console archive manager (cam) to inspect your system's capabilities.

    To check which formats are supported by your currently installed drivers:

    ./vendor/bin/cam system:formats

    To check which specific functions are supported for a particular format (e.g., zip):

    ./vendor/bin/cam system:format zip
    composer require wapmorgan/unified-archive
  10. Capabilities of the SplitbrainPhpArchive driver

    master

    The SplitbrainPhpArchive driver provides the following abilities:

    • Abilities::OPEN
    • Abilities::APPEND
    • Abilities::CREATE
    • Abilities::CREATE_IN_STRING

    Limitations:

    • It does not support getFileContent (getting file content).
    • It does not support getFileStream (getting file streams).
    • It does not support extractFiles (extracting specific files from an archive).
    • It does not support encryption/passwords (throws UnsupportedOperationException if a password is provided).
  11. Install the TarByPhar driver

    master

    The TarByPhar driver requires the PHP phar extension. For compressed formats, you may also need the zlib or bz2 extensions.

    Requirements:

    • phar extension
    • zlib (optional, for TAR_GZIP)
    • bz2 (optional, for TAR_BZIP)

    Installation Instruction: install phar extension and optionally php-extensions (zlib, bz2)

  12. Use the UnifiedArchive CLI (cam)

    master

    The cam binary is the console archive manager for UnifiedArchive. It provides a unified interface for managing various archive formats on Windows and Linux.

    Before using the CLI, ensure you have installed dependencies via Composer:

    composer install
    composer require symfony/console
    # Example of running the CLI (usage depends on specific command arguments)
    ./bin/cam --help