Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Created February 4, 2014 14:15
Show Gist options
  • Save iolloyd/8804365 to your computer and use it in GitHub Desktop.
Save iolloyd/8804365 to your computer and use it in GitHub Desktop.
Rename the files in a zip
<?php
$zipFile = $argv[1];
$newName = $argv[2];
if (!($zipFile && $newName)) {
die('You need to specify a zip file to rename and a new filename');
}
$ext = pathinfo($zipFile, PATHINFO_EXTENSION);
if ($ext != 'zip') {
die('You must provide the name of a zip file');
}
$zip = new ZipArchive;
if ($zip->open($zipFile) === true) {
mkdir("./tmp-$newName");
for($i = 0; $i < $zip->numFiles; $i++) {
$filename = $zip->getNameIndex($i);
$fileinfo = pathinfo($filename);
$newFile = sprintf("./tmp-%s/%s-%s.%s", $newName, $newName, $i, $fileinfo['extension']);
copy("zip://".$zipFile."#".$filename, $newFile);
}
$zip->close();
exec("zip -r $newName.zip tmp-$newName");
exec("rm -rf tmp-$newName");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment