php-extended / php-http-client-zip
符合psr-18规范的中间件客户端,用于处理zip压缩和解压缩
7.0.6
2024-07-31 13:45 UTC
Requires
- php: >=8.0
- ext-zip: *
- psr/http-client: ^1
- psr/http-factory: ^1
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-31 12:04:17 UTC
README
符合psr-18规范的中间件客户端,用于处理zip压缩和解压缩。
安装
此库的安装通过composer进行,所有类的自动加载均通过其自动加载器完成。
- 从其网站下载
composer.phar
。 - 然后运行以下命令将此库作为依赖项安装
php composer.phar php-extended/php-http-client-zip ^7
基本用法
此库旨在作为http请求和响应的中间件,并在请求通过时记录事件。可以使用以下方式使用它
/* @var $client Psr\Http\Message\ClientInterface */ // psr-18
/* @var $request Psr\Http\Message\RequestInterface */ // psr-7
$client = new ZipClient($client);
$response = $client->sendRequest($request);
/* @var $response Psr\Http\Message\ResponseInterface */
此库处理请求中添加 Accept-Header
和根据响应的 Content-Encoding
头解码。
此库还可以解压缩接收到的类似zip的文件。为了使其工作
- 首先,请求必须包含一个指向有效文件路径的
X-Php-Download-File
头。如果在此位置已存在文件,则将覆盖它。 - 其次,http客户端必须支持
X-Php-Download-File
头,以在指定位置写入下载的文件,并从该文件创建一个StreamInterface
。
如果给定的 X-Php-Download-File
响应是兼容zip的文件,它将被解压缩到与当前文件相同的目录中,但使用一个虚构的名称。所有文件都将连接到这个位置。
此客户端将设置 X-Php-Uncompressed-File
头,其中包含新解压文件的路径,并将指向下载文件的现有 StreamInterface
替换为指向解压文件的新 StreamInterface
。
下载的文件将不会被删除,删除下载文件和解压文件是调用库的工作,一旦工作完成。
$client = new ZipClient($client);
$request = $request->withAddedHeader('X-Php-Download-File' => '/tmp/file.gz');
$response = $client->sendRequest($response);
$stream = $response->getBody();
/** @var $stream \Psr\Http\Message\StreamInterface */
// the stream points to the /tmp/{$sha1} file that is unzipped
// the exact location of the file can be uncovered with :
$filePath = $response->getHeader('X-Php-Uncompressed-File');
许可证
MIT (见许可证文件)。