定制 temant/zip-archiver 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

temant/zip-archiver

Composer 安装命令:

composer require temant/zip-archiver

包简介

A general-purpose compression/decompression library supporting ZIP, TAR, GZ, BZ2, RAR and more

README 文档

README

CI Latest Stable Version Total Downloads License PHP Version Require PHPStan Level

A general-purpose compression/decompression library for PHP 8.1+ supporting ZIP, TAR, TAR.GZ, TAR.BZ2, GZIP, BZIP2, and RAR formats with a unified API.

Features

  • 7 archive formats -- ZIP, TAR, TAR.GZ, TAR.BZ2, GZIP, BZIP2, RAR (decompress only)
  • Unified facade -- single Archiver class handles all formats transparently
  • Auto-detection -- format resolved automatically from file extension
  • Driver architecture -- strategy pattern with per-format drivers, easily extensible
  • Password protection -- AES-256 encryption for ZIP archives
  • Compression levels -- 6 levels from Fastest to Best via a clean enum
  • Include/exclude filters -- glob-based patterns for selective compress/extract
  • Progress callbacks -- real-time feedback during compress and decompress operations
  • Archive inspection -- list entries, get metadata, compression ratios, search/filter
  • Archive verification -- test integrity without extracting
  • Archive comments -- read and write comments (ZIP)
  • Custom drivers -- register your own format drivers via the factory
  • PHPStan max level -- fully statically analysed with zero errors
  • Zero dependencies -- only requires ext-phar (format extensions are optional)

Requirements

  • PHP 8.1 or higher
  • Composer
  • ext-phar (bundled with PHP)

Optional extensions (required only for their respective formats):

Extension Formats
ext-zip ZIP
ext-zlib GZIP, TAR.GZ
ext-bz2 BZIP2, TAR.BZ2
ext-rar RAR (decompress only)

Installation

composer require temant/archiver

Quick Start

use Temant\Archiver\Archiver;

$archiver = new Archiver();

// Compress a directory to ZIP (format auto-detected from extension)
$archiver->compress('/path/to/directory', '/path/to/archive.zip');

// Decompress
$archiver->decompress('/path/to/archive.zip', '/path/to/output');

Usage

Creating the Archiver

use Temant\Archiver\Archiver;

$archiver = new Archiver();

The Archiver facade auto-detects the format from the file extension and delegates to the appropriate driver.

Compressing Files

Directory to Archive

// ZIP
$archiver->compress('/path/to/directory', '/output/archive.zip');

// TAR
$archiver->compress('/path/to/directory', '/output/archive.tar');

// TAR.GZ
$archiver->compress('/path/to/directory', '/output/archive.tar.gz');

// TAR.BZ2
$archiver->compress('/path/to/directory', '/output/archive.tar.bz2');

Single File (GZIP / BZIP2)

// GZIP -- single file only
$archiver->compress('/path/to/file.log', '/output/file.log.gz');

// BZIP2 -- single file only
$archiver->compress('/path/to/file.log', '/output/file.log.bz2');

Decompressing Archives

// Extract to a directory
$archiver->decompress('/path/to/archive.zip', '/output/directory');

// Works with any supported format
$archiver->decompress('/path/to/archive.tar.gz', '/output/directory');
$archiver->decompress('/path/to/archive.rar', '/output/directory');

Compression Levels

use Temant\Archiver\Enum\CompressionLevel;

$archiver->compress($source, $dest, [
    'level' => CompressionLevel::Best,    // Maximum compression
]);

// Available levels: None, Fastest, Fast, Normal, Good, Best

Password Protection (ZIP)

// Compress with AES-256 encryption
$archiver->compress($source, 'secret.zip', [
    'password' => 'my-secret-password',
]);

// Decompress with password
$archiver->decompress('secret.zip', $output, [
    'password' => 'my-secret-password',
]);

Include / Exclude Filters

// Only include PHP files
$archiver->compress($source, 'code.zip', [
    'include' => ['*.php'],
]);

// Exclude log files and the vendor directory
$archiver->compress($source, 'project.zip', [
    'exclude' => ['*.log', 'vendor/*'],
]);

// Filters also work on decompress
$archiver->decompress('archive.zip', $output, [
    'include' => ['*.txt'],
]);

Progress Callbacks

$archiver->compress($source, $dest, [
    'progress' => function (string $currentFile, int $processed, int $total): void {
        echo "{$currentFile}: {$processed}/{$total}\n";
    },
]);

Archive Comments (ZIP)

$archiver->compress($source, 'archive.zip', [
    'comment' => 'Release v1.2.0',
]);

Listing Archive Entries

use Temant\Archiver\DTO\ArchiveEntry;

$entries = $archiver->list('/path/to/archive.zip');

foreach ($entries as $entry) {
    echo $entry->path;           // relative path inside archive
    echo $entry->size;           // uncompressed size in bytes
    echo $entry->compressedSize; // compressed size in bytes
    echo $entry->isDirectory;    // bool
    echo $entry->modifiedTime;   // Unix timestamp
    echo $entry->compressionRatio(); // e.g. 0.65
}

Archive Information

use Temant\Archiver\DTO\ArchiveInfo;

$info = $archiver->info('/path/to/archive.zip');

echo $info->format;               // ArchiveFormat::Zip
echo $info->fileCount;            // number of files
echo $info->directoryCount;       // number of directories
echo $info->totalSize;            // total uncompressed size
echo $info->compressedSize;       // total compressed size
echo $info->compressionRatio();   // overall ratio
echo $info->formattedTotalSize(); // e.g. "1.24 MB"

// Search and filter entries
$phpFiles = $info->searchEntries('*.php');
$byExtension = $info->entriesByExtension('json');

Verifying Archives

$isValid = $archiver->verify('/path/to/archive.zip'); // true or false

Format Detection

use Temant\Archiver\Enum\ArchiveFormat;

$format = $archiver->detectFormat('/path/to/archive.tar.gz');
// ArchiveFormat::TarGz

// Check format capabilities
$format->supportsDirectories(); // true
$format->supportsPassword();    // false
$format->supportsCompression(); // true
$format->isDecompressOnly();    // false

Using Specific Drivers

$driver = $archiver->driver(ArchiveFormat::Zip);
$driver->compress($source, $dest, $options);

Registering Custom Drivers

use Temant\Archiver\ArchiverFactory;
use Temant\Archiver\Contract\ArchiverInterface;

$factory = $archiver->factory();
$factory->register(new MyCustomArchiver());

Supported Formats

// All registered formats
$archiver->supportedFormats(); // [ArchiveFormat::Zip, ArchiveFormat::Tar, ...]

Supported Formats

Format Extension(s) Compress Decompress Directories Password Extension Required
ZIP .zip Yes Yes Yes AES-256 ext-zip
TAR .tar Yes Yes Yes No ext-phar
TAR.GZ .tar.gz, .tgz Yes Yes Yes No ext-zlib
TAR.BZ2 .tar.bz2, .tbz2 Yes Yes Yes No ext-bz2
GZIP .gz, .gzip Yes Yes No No ext-zlib
BZIP2 .bz2, .bzip2 Yes Yes No No ext-bz2
RAR .rar No Yes Yes Yes ext-rar

Exception Handling

All exceptions extend ArchiverException (which extends RuntimeException).

Exception When
CompressionException Compression fails (invalid source, write error, unsupported operation)
DecompressionException Decompression fails (invalid archive, password error, write error)
UnsupportedFormatException Unrecognized file extension or missing PHP extension
ArchiverException Base exception for general archive errors
use Temant\Archiver\Exception\CompressionException;
use Temant\Archiver\Exception\UnsupportedFormatException;

try {
    $archiver->compress($source, 'archive.xyz');
} catch (UnsupportedFormatException $e) {
    // Unknown format
} catch (CompressionException $e) {
    // Compression failed
}

API Reference

Archiver (Facade)

Method Description
compress(source, destination, options?) Compress a file or directory
decompress(archive, destination, options?) Decompress an archive
list(archive, options?) List entries without extracting
info(archive) Get archive metadata
verify(archive) Test archive integrity
detectFormat(path) Detect format from file extension
supportedFormats() List all supported formats
driver(format) Get a specific format driver
factory() Access the underlying factory

Options

Option Type Applies To Description
password string ZIP, RAR Encryption password
level CompressionLevel All Compression level enum
include string[] ZIP, TAR Glob patterns to include
exclude string[] ZIP, TAR Glob patterns to exclude
comment string ZIP Archive comment
overwrite bool ZIP Overwrite existing files (default: true)
progress callable All Progress callback

Enums

ArchiveFormat

Cases: Zip, Tar, TarGz, TarBz2, Gz, Bz2, Rar

Methods: fromPath(), extension(), supportsDirectories(), supportsPassword(), supportsCompression(), isDecompressOnly(), label()

CompressionLevel

Cases: None (0), Fastest (1), Fast (3), Normal (5), Good (7), Best (9)

Testing

# Run tests
vendor/bin/phpunit

# Run tests with coverage
vendor/bin/phpunit --coverage-text

# Run static analysis
vendor/bin/phpstan analyse

# Run both
vendor/bin/phpunit && vendor/bin/phpstan analyse

License

MIT License. See LICENSE for details.

temant/zip-archiver 适用场景与选型建议

temant/zip-archiver 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「compression」 「zip」 「archive」 「tar」 「gzip」 「rar」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 temant/zip-archiver 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 temant/zip-archiver 我们能提供哪些服务?
定制开发 / 二次开发

基于 temant/zip-archiver 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-05-08