PHP code example of goletter / hyperf-archive

1. Go to this page and download the library: Download goletter/hyperf-archive library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

goletter / hyperf-archive example snippets


return [
    'temp_dir' => BASE_PATH . '/runtime/archive',
    'max_files' => 500,
    'max_total_size' => 500 * 1024 * 1024,
    'temp_ttl' => 3600,
    'compression_level' => -1,
];

use Goletter\Archive\Archive;

return Archive::make()
    ->addFile('/data/uploads/a.pdf')
    ->addFile('/data/uploads/b.jpg', 'images/b.jpg')
    ->addFromStorage('s3', 'exports/report.xlsx', 'report.xlsx')
    ->addContents('hello', 'readme.txt')
    ->download('bundle.zip');

return archive()
    ->addFile('/path/a.pdf')
    ->download('files.zip');

// ZIP 内保留顶层目录名:project-docs/a.pdf, project-docs/sub/b.jpg
return Archive::make()
    ->addDirectory('/data/uploads/project-docs')
    ->download('project-docs.zip');

// 自定义 ZIP 内顶层目录名
return Archive::make()
    ->addDirectory('/data/uploads/project-docs', 'exports')
    ->download('exports.zip');

// 不保留顶层目录,直接按相对路径写入 ZIP 根目录
return Archive::make()
    ->addDirectory('/data/uploads/project-docs', '')
    ->download('flat.zip');

// 包含隐藏文件(默认会跳过 .git、.DS_Store 等)
return Archive::make()
    ->addDirectory('/data/uploads/project-docs', null, true)
    ->download('all.zip');

return $this->archiver->download([
    ['dir' => '/data/uploads/project-docs'],
    ['dir' => '/data/uploads/assets', 'prefix' => 'assets'],
], 'bundle.zip');

use Goletter\Archive\Contract\ArchiverInterface;
use Hyperf\Di\Annotation\Inject;

#[Inject]
protected ArchiverInterface $archiver;

// 直接下载
return $this->archiver->download([
    '/path/a.pdf',
    ['path' => '/path/b.jpg', 'name' => 'photos/b.jpg'],
    ['disk' => 's3', 'path' => 'docs/c.pdf', 'name' => 'c.pdf'],
], 'export.zip');

// 仅生成临时 ZIP 路径
$path = $this->archiver->create($entries, 'export.zip');

// 打包后上传到磁盘
$remote = $this->archiver->upload($entries, 'exports/bundle.zip', 's3');

// 打包后返回下载地址(推荐)
$result = Archive::make()
    ->addDirectory('/data/uploads/project-docs')
    ->store(disk: 'oss');

return [
    'download_url' => $result->url,
    'path' => $result->path,
    'disk' => $result->disk,
];

// 指定远端路径
$result = Archive::make()
    ->addEntries($files)
    ->store('exports/bundle.zip', 'oss');

// 返回 JSON 给前端
return $result->toArray();
// ['path' => 'exports/bundle.zip', 'url' => 'https://...', 'disk' => 'oss', 'expires_at' => null]

return [
    'default_disk' => 'oss',
    'upload_path' => 'archives/{Ymd}/{uuid}.zip',
    'disks' => [
        'local' => [
            'url' => env('APP_URL') . '/uploads/{path}',
        ],
        'oss' => [
            'url' => 'https://your-bucket.oss-cn-hangzhou.aliyuncs.com/{path}',
        ],
    ],
];

use Goletter\Archive\Archive;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: '/files')]
class FileController
{
    #[PostMapping('download-zip')]
    public function downloadZip(): ResponseInterface
    {
        // 先按业务做权限校验,再收集可访问文件路径
        $files = $this->fileService->getAccessibleFiles($this->request->input('ids', []));

        return Archive::make()
            ->addEntries($files)
            ->download('files_' . date('YmdHis') . '.zip');
    }
}

[
    ['path' => '/local/a.pdf'],
    ['path' => '/local/b.jpg', 'name' => 'images/b.jpg'],
    ['disk' => 'oss', 'path' => 'remote/c.png', 'name' => 'c.png'],
    ['contents' => 'meta', 'name' => 'info.txt'],
]
bash
composer rf.php vendor:publish goletter/hyperf-archive