Ne-Lexa / php-zip

PhpZip is a php-library for extended work with ZIP-archives.
MIT License
491 stars 60 forks source link

use ZipFile::saveAsStream with non-seekable stream #67

Open connorhu opened 3 years ago

connorhu commented 3 years ago

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?