php-zip icon indicating copy to clipboard operation
php-zip copied to clipboard

use ZipFile::saveAsStream with non-seekable stream

Open connorhu opened this issue 5 years ago • 0 comments

Description
With symfony this would a pretty easy way to stream large zip files on the fly:

        $finder = new Finder();
        $finder
            ->files()
            ->in('path');

        $zipFile = new ZipFile();
        $zipFile->addFromFinder($finder, $options = [
            ZipOptions::COMPRESSION_METHOD => ZipCompressionMethod::DEFLATED,
        ]);
        
        $response = new StreamedResponse(function() use ($zipFile) {
            $outputStream = fopen('php://output', 'wb');
            $zipFile->saveAsStream($outputStream);
        });
        
        $response->headers->set('Content-Type', 'application/zip');
        $disposition = HeaderUtils::makeDisposition(
            HeaderUtils::DISPOSITION_ATTACHMENT,
            'file.zip'
        );
        $response->headers->set('Content-Disposition', $disposition);

        return $response;

But the problem is the saveAsStream() only works with seekable streams. (btw you should check stream_get_meta_data of $handle in saveAsStream method)

Is there any workaround to save large zip into a non-seekable stream?

connorhu avatar Nov 19 '20 14:11 connorhu