Is it possible to pack files in chunks to avoid memory exhaustion when using zip.lib.php for file compression in PHP?

When using zip.lib.php for file compression in PHP, memory exhaustion can occur when trying to compress large files. One way to avoid this issue is to pack files in chunks instead of trying to compress the entire file at once. By breaking the file into smaller chunks, you can compress each chunk individually and then combine them into a single zip file.

<?php
$zip = new ZipArchive();
$zipFileName = 'compressed_files.zip';
$zip->open($zipFileName, ZipArchive::CREATE);

$files = ['file1.txt', 'file2.txt', 'file3.txt']; // List of files to compress
$chunkSize = 1024 * 1024; // 1MB chunk size

foreach ($files as $file) {
    $fileSize = filesize($file);
    $chunks = ceil($fileSize / $chunkSize);

    for ($i = 0; $i < $chunks; $i++) {
        $chunkData = file_get_contents($file, false, null, $i * $chunkSize, $chunkSize);
        $zip->addFromString(basename($file) . '_' . $i, $chunkData);
    }
}

$zip->close();
?>