UnifiedArchive icon indicating copy to clipboard operation
UnifiedArchive copied to clipboard

one file driver: add memory efficient chunk read/write unpacking of gzip files

Open iasjennen opened this issue 1 year ago • 4 comments

Previously the whole unpacked file was loaded into memory. This implementation allows to unpack gz files in a streamed way.

iasjennen avatar Feb 14 '25 09:02 iasjennen

btw, the above was tested successfully. possible implementation for the other two one-file-drivers (untested):

bzip:

    /**
     * @param string $targetPath
     */
    public function streamToFile($targetPath)
    {
        $sfp = bzopen($this->fileName, 'rb');
        $fp = fopen($targetPath, "w");
        while (!feof($sfp)) {
            $chunk = bzread($sfp, 8192);
            if($chunk === 0 || $chunk === false)
                throw new ArchiveExtractionException('Cannot read bzip chunk');
            if(fwrite($fp, $chunk, strlen($chunk)) === false)
                throw new ArchiveExtractionException('Cannot write bzip chunk');
        }
        bzclose($sfp);
        fclose($fp);
    }

lzma:

    /**
     * @param string $targetPath
     */
    public function streamToFile($targetPath)
    {
        $sfp = xzopen($this->fileName, 'rb');
        $fp = fopen($targetPath, "w");
        while (!feof($sfp)) {
            $chunk = xzread($sfp, 8192);
            if($chunk === 0 || $chunk === false)
                throw new ArchiveExtractionException('Cannot read xz chunk');
            if(fwrite($fp, $chunk, strlen($chunk)) === false)
                throw new ArchiveExtractionException('Cannot write xz chunk');
        }
        xzclose($sfp);
        fclose($fp);
    }

iasjennen avatar Feb 14 '25 09:02 iasjennen

@wapmorgan please review when you can.

iasjennen avatar Mar 08 '25 21:03 iasjennen

@wapmorgan any update on the above?

iasjennen avatar Jun 16 '25 07:06 iasjennen

(untested):

somebody can test and create MR!

wapmorgan avatar Jun 18 '25 12:06 wapmorgan