pclzip eats up a lot of memory while compressing a folder

The problem is that when the directory is compressed on the server, for some reason, there is a memory leak. On LAN on Vertrigo, this fragment works with 128 MB of memory, and even 256 MB is not enough for the server during compression. It compresses two directories from the install folder and one file, then for some reason creates a very large temporary file (about 500 – 900 megabytes) and crashes with an out of memory error.

The archive itself eventually comes out to be about 55m, it does not squeeze out about 1 megabyte somewhere (another 1 folder and a dozen files). What’s the problem, I can’t figure it out…

require_once('pclzip.lib.php');
$catalog = "install";    
@unlink($catalog.".zip"); 
$archive = new PclZip($catalog.".zip");
$openDIR = opendir($catalog);
while ($scan = readdir($openDIR))
{
    if($scan == '.' || $scan == '..' ) continue;
    $list = $archive->add($catalog."/".$scan, PCLZIP_OPT_REMOVE_PATH, $catalog);     
}

Answer 1, authority 100%

As an option, I can suggest getting rid of the cycle, usually they love memory 😉
Moreover, pclzip has the ability to immediately point to the folder that needs to be compressed, and it will do everything inside itself.

$archive->create($catalog."/".$scan, PCLZIP_OPT_REMOVE_ALL_PATH);