Skip to content

Instantly share code, notes, and snippets.

@jlgrall
Created July 20, 2016 09:29
Show Gist options
  • Save jlgrall/1523948cd023dbd79681a9aa838c5fbc to your computer and use it in GitHub Desktop.
Save jlgrall/1523948cd023dbd79681a9aa838c5fbc to your computer and use it in GitHub Desktop.
Test case for inconsistent PHPZipMerge merged zip
<?php
// Put this file in a subdirectory inside the PHPZipMerge repository, then execute it.
error_reporting(E_ALL);
ini_set('open_basedir', dirname(__DIR__)); // Security: restrict file access to parent directory.
require __DIR__.'/../vendor/autoload.php';
$srcName = 'src.zip';
$src = __DIR__.'/'.$srcName;
if(file_exists($src)) unlink($src);
$mergedName = 'merged.zip';
$merged = __DIR__.'/'.$mergedName;
if(file_exists($merged)) unlink($merged);
// Creating the source src.zip
$zip = new \ZipArchive();
$res = $zip->open($src, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
if($res !== true) {
// List of error codes at: http://php.net/manual/en/class.ziparchive.php#108601
throw new \Exception("Opening $srcName error code: $res");
}
$zip->addFromString('hello.txt', 'Hello test !');
$res = $zip->close();
if($res !== true) {
throw new \Exception("Zip could not be saved correctly: $src");
}
// Merging src.zip into a new file merged.zip
$zipMerge = new \ZipMerge\Zip\File\ZipMergeToFile($merged);
$zipMerge->appendZip($src);
$zipMerge->finalize();
// Checking consistency of merged.zip
$zipCheck = new \ZipArchive();
$res = $zipCheck->open($merged, \ZipArchive::CHECKCONS);
if ($res !== true) {
// List of error codes at: http://php.net/manual/en/class.ziparchive.php#108601
if($res === 21) throw new \Exception("Checking $mergedName error code: $res (ZipArchive::ER_INCONS => 'Zip archive inconsistent')");
else throw new \Exception("Checking $mergedName error code: $res");
}
// If we reach here, no error happened :)
echo "Test success, no error encountered." . PHP_EOL;
@jlgrall
Copy link
Author

jlgrall commented Jul 20, 2016

Test case for: Grandt/PHPZipMerge#7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment