Skip to content

Commit

Permalink
Refactor Library
Browse files Browse the repository at this point in the history
  • Loading branch information
maennchen committed Nov 25, 2022
1 parent c31a03f commit 67bdd58
Show file tree
Hide file tree
Showing 62 changed files with 2,683 additions and 2,372 deletions.
15 changes: 7 additions & 8 deletions .github/workflows/part_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ["7.4", "8.0", "8.1"]
php: ["8.2"]
os: [ubuntu-latest]
experimental: [false]
include:
- php: nightly
os: ubuntu-latest
experimental: true
- php: "8.1"
- php: "8.2"
os: windows-latest
experimental: false
# Not running on MacOS since it currently doesn't stop
# - php: 8.1
# os: macos-latest
# experimental: false
- php: 8.2
os: macos-latest
experimental: false

steps:
- name: Checkout Code
Expand Down Expand Up @@ -99,7 +98,7 @@ jobs:
id: setup-php
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
php-version: "8.2"
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
Expand Down Expand Up @@ -131,7 +130,7 @@ jobs:
id: setup-php
uses: shivammathur/setup-php@v2
with:
php-version: "8.1"
php-version: "8.2"
- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
Expand Down
4 changes: 2 additions & 2 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
return $config->setRules([
'@PER' => true,
'@PER:risky' => true,
'@PHP81Migration' => true,
'@PHP82Migration' => true,
'@PHPUnit84Migration:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'class_attributes_separation' => true,
Expand Down Expand Up @@ -68,4 +68,4 @@
],
])
->setFinder($finder)
->setRiskyAllowed(true);
->setRiskyAllowed(true);
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
php 8.1.13
php 8.2.0beta3
82 changes: 61 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,17 @@ version.

## Overview

A fast and simple streaming zip file downloader for PHP. Using this library will save you from having to write the Zip to disk. You can directly send it to the user, which is much faster. It can work with S3 buckets or any PSR7 Stream.
A fast and simple streaming zip file downloader for PHP. Using this library will
save you from having to write the Zip to disk. You can directly send it to the
user, which is much faster. It can work with S3 buckets or any PSR7 Stream.

Please see the [LICENSE](LICENSE) file for licensing and warranty information.

## Installation

Simply add a dependency on maennchen/zipstream-php to your project's composer.json file if you use Composer to manage the dependencies of your project. Use following command to add the package to your project's dependencies:
Simply add a dependency on maennchen/zipstream-php to your project's
`composer.json` file if you use Composer to manage the dependencies of your
project. Use following command to add the package to your project's dependencies:

```bash
composer require maennchen/zipstream-php
Expand All @@ -29,45 +33,81 @@ composer require maennchen/zipstream-php
## Usage

For detailed instructions, please check the
[Documentation](https://maennchen.dev/ZipStream-PHP/).

Here's a simple example:
[Documentation](https://maennchen.github.io/ZipStream-PHP/).

```php
// Autoload the dependencies
require 'vendor/autoload.php';

// enable output of HTTP headers
$options = new ZipStream\Option\Archive();
$options->setSendHttpHeaders(true);

// create a new zipstream object
$zip = new ZipStream\ZipStream('example.zip', $options);
$zip = new ZipStream\ZipStream(
fileName: 'example.zip',

// enable output of HTTP headers
sendHttpHeaders: true,
);

// create a file named 'hello.txt'
$zip->addFile('hello.txt', 'This is the contents of hello.txt');
$zip->addFile(
fileName: 'hello.txt',
data: 'This is the contents of hello.txt',
);

// add a file named 'some_image.jpg' from a local file 'path/to/image.jpg'
$zip->addFileFromPath('some_image.jpg', 'path/to/image.jpg');
$zip->addFileFromPath(
fileName: 'some_image.jpg',
path: 'path/to/image.jpg',
);

// finish the zip stream
$zip->finish();
```

## Upgrade to version 3.0.0

### General

- Minimum PHP Version: `8.2`
- Only 64bit Architecture is supported.
- The class `ZipStream\Option\Method` has been replaced with the enum `ZipStream\CompressionMethod`.
- Most clases have been flagged as `@internal` and should not be used from the outside.
If you're using internal resources to extend this library, please open an issue so that a clean
interface can be added & published.
The externally available classes & enums are:
- `ZipStream\CompressionMethod`
- `ZipStream\Exception*`
- `ZipStream\ZipStream`

### Archive Options

- The class `ZipStream\Option\Archive` has been replaced in favor of named arguments in the
`ZipStream\ZipStream` constuctor.
- The archive options `largeFileSize` & `largeFileMethod` has been removed. If you want different
`compressionMethods` based on the file size, you'll have to implement this yourself.
- The archive option `httpHeaderCallback` changed the type from `callable` to `Closure`.
- The archive option `zeroHeader` has been replaced with the option `defaultEnableZeroHeader`
and can be overridden for every file.
- The archive option `statFiles` was removed since the library no longer checks filesizes this way.
- The archive option `deflateLevel` has been replaced with the option `defaultDeflateLevel`
and can be overridden for every file.
- The first argument (`name`) of the `ZipStream\ZipStream` constuctor has been replaced with
the named argument `outputName`.

### File Options

- The class `ZipStream\Option\File` has been replaced in favor of named arguments in the
`ZipStream\ZipStream->addFile*` functions.
- The file option `method` has been renamed to `compressionMethod`.
- The file option `time` has been renamed to `lastModificationDateTime`.
- The file option `size` has been renamed to `maxSize`.

## Upgrade to version 2.0.0

- Only the self opened streams will be closed (#139)
If you were relying on ZipStream to close streams that the library didn't open,
you'll need to close them yourself now.
https://github.com/maennchen/ZipStream-PHP/tree/2.0.0#upgrade-to-version-200

## Upgrade to version 1.0.0

- All options parameters to all function have been moved from an `array` to structured option objects. See [the wiki](https://github.com/maennchen/ZipStream-PHP/wiki/Available-options) for examples.
- The whole library has been refactored. The minimal PHP requirement has been raised to PHP 7.1.

## Usage with Symfony and S3

You can find example code on [the wiki](https://github.com/maennchen/ZipStream-PHP/wiki/Symfony-example).
https://github.com/maennchen/ZipStream-PHP/tree/2.0.0#upgrade-to-version-100

## Contributing

Expand Down
32 changes: 18 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,35 +22,39 @@
}
],
"require": {
"php": "^7.4 || ^8.0",
"php-64bit": "^8.2",
"symfony/polyfill-mbstring": "^1.0",
"psr/http-message": "^1.0",
"myclabs/php-enum": "^1.5"
"psr/http-message": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^8.5.8 || ^9.4.2",
"guzzlehttp/guzzle": "^6.5.3 || ^7.2.0",
"phpunit/phpunit": "^9.5",
"guzzlehttp/guzzle": "^7.5",
"ext-zip": "*",
"mikey179/vfsstream": "^1.6",
"vimeo/psalm": "^4.1",
"php-coveralls/php-coveralls": "^2.4",
"friendsofphp/php-cs-fixer": "^3.9"
"php-coveralls/php-coveralls": "^2.5",
"friendsofphp/php-cs-fixer": "^3.13",
"vimeo/psalm": "^4.30"
},
"autoload": {
"psr-4": {
"ZipStream\\": "src/"
}
},
"autoload-dev": {
"psr-4": { "ZipStream\\Test\\": "test/" }
},
"scripts": {
"format": "php-cs-fixer fix",
"format": "PHP_CS_FIXER_IGNORE_ENV=true php-cs-fixer fix",
"test": "composer run test:unit && composer run test:formatted && composer run test:lint",
"test:unit": "phpunit --coverage-clover=coverage.clover.xml",
"test:unit:slow": "composer run test:unit -- --group slow",
"test:unit:fast": "composer run test:unit -- --exclude-group slow",
"test:formatted": "composer run format -- --dry-run --stop-on-violation --using-cache=no",
"test:lint": "psalm --stats --show-info --find-unused-psalm-suppress",
"coverage:report": "php-coveralls --coverage_clover=coverage.clover.xml --json_path=coveralls-upload.json -v",
"install:tools": "phive install --trust-gpg-keys 0x67F861C3D889C656",
"docs:generate": "tools/phpdocumentor"
},
"autoload": {
"psr-4": {
"ZipStream\\": "src/"
}
},
"archive": {
"exclude": [
"/composer.lock",
Expand Down
64 changes: 35 additions & 29 deletions guides/ContentLength.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@ The following workaround adds an approximated header:

.. code-block:: php
class Zip
{
/** @var string */
private $name;
use ZipStream\CompressionMethod;
use ZipStream\ZipStream;
class Zip
{
private $files = [];
public function __construct($name)
{
$this->name = $name;
}
public function __construct(
private readonly string $name
) { }
public function addFile($name, $data)
{
public function addFile(
string $name,
string $data,
): void {
$this->files[] = ['type' => 'addFile', 'name' => $name, 'data' => $data];
}
public function addFileFromPath($name, $path)
{
public function addFileFromPath(
string $name,
string $path,
): void {
$this->files[] = ['type' => 'addFileFromPath', 'name' => $name, 'path' => $path];
}
public function getEstimate()
{
public function getEstimate(): int {
$estimate = 22;
foreach ($this->files as $file) {
$estimate += 76 + 2 * strlen($file['name']);
Expand All @@ -48,24 +50,28 @@ The following workaround adds an approximated header:
public function finish()
{
header('Content-Length: ' . $this->getEstimate());
$options = new \ZipStream\Option\Archive();
$options->setSendHttpHeaders(true);
$options->setEnableZip64(false);
$options->setDeflateLevel(-1);
$zip = new \ZipStream\ZipStream($this->name, $options);
$zip = new ZipStream(
outputName: $this->name,
SendHttpHeaders: true,
enableZip64: false,
defaultCompressionMethod: CompressionMethod::STORE,
);
$fileOptions = new \ZipStream\Option\File();
$fileOptions->setMethod(\ZipStream\Option\Method::STORE());
foreach ($this->files as $file) {
if ($file['type'] === 'addFile') {
$zip->addFile($file['name'], $file['data'], $fileOptions);
}
if ($file['type'] === 'addFileFromPath') {
$zip->addFileFromPath($file['name'], $file['path'], $fileOptions);
}
if ($file['type'] === 'addFile') {
$zip->addFile(
fileName: $file['name'],
data: $file['data'],
);
}
if ($file['type'] === 'addFileFromPath') {
$zip->addFileFromPath(
fileName: $file['name'],
path: $file['path'],
);
}
}
$zip->finish();
exit;
}
}
Expand All @@ -76,4 +82,4 @@ It only works with the following constraints:

Thanks to
`partiellkorrekt <https://github.com/maennchen/ZipStream-PHP/issues/89#issuecomment-1047949274>`_
for this workaround.
for this workaround.
8 changes: 5 additions & 3 deletions guides/FlySystem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ default one, and pass it to Flysystem ``putStream`` method.
$zipStream->addFile('test.txt', 'text');
$zipStream->finish();
// Store File (see Flysystem documentation, and all its framework integration)
$adapter = new Local(__DIR__.'/path/to/folder'); // Can be any adapter (AWS, Google, Ftp, etc.)
// Store File
// (see Flysystem documentation, and all its framework integration)
// Can be any adapter (AWS, Google, Ftp, etc.)
$adapter = new Local(__DIR__.'/path/to/folder');
$filesystem = new Filesystem($adapter);
$filesystem->putStream('test.zip', $tempStream)
// Close Stream
fclose($tempStream);
fclose($tempStream);

0 comments on commit 67bdd58

Please sign in to comment.