PHP code example of genkgo / archive-stream

1. Go to this page and download the library: Download genkgo/archive-stream 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/ */

    

genkgo / archive-stream example snippets



use Genkgo\ArchiveStream\Archive;
use Genkgo\ArchiveStream\CallbackContents;
use Genkgo\ArchiveStream\CallbackStringContent;
use Genkgo\ArchiveStream\EmptyDirectory;
use Genkgo\ArchiveStream\FileContent;
use Genkgo\ArchiveStream\Psr7Stream;
use Genkgo\ArchiveStream\StringContent;
use Genkgo\ArchiveStream\TarGzReader;
use Genkgo\ArchiveStream\TarReader;
use Genkgo\ArchiveStream\ZipReader;

$archive = (new Archive())
    ->withContent(new CallbackStringContent('callback.txt', function () {
        return 'data';
    }))
    ->withContent(new StringContent('string.txt', 'data'))
    ->withContent(new FileContent('file.txt', 'local/file/name.txt'))
    ->withContent(new EmptyDirectory('directory'))
    ->withContents([new StringContent('string2.txt', 'data')])
    ->withContents(new CallbackContents(fn () => yield new StringContent('string3.txt', 'data')));

$response = $response->withBody(
    new Psr7Stream(new ZipReader($archive))
);

// or for tar files

$response = $response->withBody(
    new Psr7Stream(new TarReader($archive))
);

// or for tar.gz files

$response = $response->withBody(
    new Psr7Stream(new TarGzReader(new TarReader($archive)))
);

use Symfony\Component\HttpFoundation\StreamedResponse;

$stream = new Psr7Stream(new ZipReader($archive));

$response = new StreamedResponse(function () use ($stream) {
    while ($stream->eof() === false) {
        echo $stream->read($blockSize = 1048576);
    }
}, 200, [
    'Content-type' => 'application/zip',
    'Content-Disposition' => 'attachment; filename="file.zip"',
    'Content-Transfer-Encoding' => 'binary',
]);