October 22, 2024
Chicago 12, Melborne City, USA
PHP

Preserve symlinks when creating zip file with php ZipArchive


I am building archive with ZipArchive from directory which contains symbolic links which I want to preserve. Example structure:

-- file1
-- file2
-- directory/file3 --> ../file1

When I execute the code I have below, directory/file3 is present in the archive but instead of being symlink, it has the contents of file. Although it works, there are large amount of links that I want to preserve to keep archive size smaller.

    $zip = new ZipArchive();
    $zip->open($zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE)

    foreach($filesToZip as $name => $file) {
        if (!$file->isDir()) {
            $filePath = $file->getPathname();
            $relativePath = substr($filePath, strlen($rootPath) + 1);
            $zip->addFile($filePath, $relativePath);
        }
    }

    $zip->close();

What would you recommend as solution?

I also tried using file->getRealPath() which just makes the problem worse since it adds the file with its absolute path into the archive.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video