Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add files to an existing ZIP file

Tags:

php

EDIT: The question is answered below. If you want to ZIP a directory/folder like what I did, see this: How to zip a whole folder using PHP

I have an application with a timer that automatically download a ZIP file from my server.

But the ZIP file is being changed every day.

When someone use the application, the application user will get a "550 File unavailable" error because the ZIP file was removed and added again (that's because the application timer is executed every 900 milliseconds).

So instead of removing the ZIP file and creating it again with the new data, how to add the new data without re-creating the ZIP file?

Currently I use this:

$zip = new ZipArchive;
// Get real path for our folder
$rootPath = realpath('../files_to_be_in_zip');

// Initialize archive object
$zip = new ZipArchive();
$zip->open('../zouch.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
    }
}

// Zip archive will be created only after closing object
$zip->close();

And this code get the contents of the files_to_be_in_zip folder and re-create the "zouch.zip" file with it.

And yes, I know the new data fullpath... it is $recentlyCreatedFile

EDIT: I found this code on http://php.net/manual/en/ziparchive.addfile.php

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->addFile('/path/to/index.txt', 'newname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>

But I want to create a directory in the existing ZIP too.

Any help?

Thanks!

like image 349
Mario Avatar asked Mar 09 '23 05:03

Mario


1 Answers

When you open the zip, you are specifying for it to either be newly created or overwritten in your second parameter. Removing that second parameter should make your script work as it is. Below is your code with the required edits already implemented.

$zip = new ZipArchive;
// Get real path for our folder
$rootPath = realpath('../files_to_be_in_zip');

$zip->open('../zouch.zip');

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
  new RecursiveDirectoryIterator($rootPath),
  RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file){
  // Skip directories (they would be added automatically)
  if (!$file->isDir()){
    // Get real and relative path for current file
    $filePath = $file->getRealPath();
    $relativePath = substr($filePath, strlen($rootPath) + 1);

    // Add current file to archive
    $zip->addFile($filePath, $relativePath);
  }
}

// Zip archive will be created only after closing object
$zip->close();

However, if you have data that are already in the ZIP file but will need to be REPLACED in the future then you have to use ZipArchive::OVERWRITE

$zip->open('../zouch.zip', ZipArchive::OVERWRITE);
like image 64
coderodour Avatar answered Mar 16 '23 04:03

coderodour