Created
February 4, 2014 14:15
-
-
Save iolloyd/8804365 to your computer and use it in GitHub Desktop.
Rename the files in a zip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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