Hey PHP experts, Looking for some help here... Basically I have many .zip files on a folder, I need a script that will unzip all this files and then rename the extracted files using the original .zip filename. ex. If I have the zip file "0060ab38ffa3.zip" I need the script to extract that file, the extracted file has a different filename (it's an mp4 file) ie. funny-video.mp4, I need the extracted mp4 file to be renamed "0060ab38ffa3.mp4" (the .zip filename) I need the extracted renamed file to be on the same folder and the .zip file will not be deleted. I have more than 900 files to be unzip and renamed so I'm looking for an automated way of doing it. Extracting the files is easy, its the renaming part that's giving me trouble. So far I have this script.. <?php if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if(exec("unzip $file",$result)){ echo "$file<br/>"; } else { echo 'Error unzipping.Check the file!'; } } } closedir($handle); } ?> Code (markup): Any thoughts? Thanks
Try this: function unzip_rename($file){ if(is_file($file) && extension_loaded('zip')){ $z = new ZipArchive; $z->open($file); $numFiles = $z->numFiles; $index = $numFiles - 1; while($index>=0){ $n = $z->getNameIndex($index); $pinfo = pathinfo($file); $fname = $pinfo['filename']; $z->extractTo('.',$n); rename($n,$fname.$index.".mp4"); $index--; } } } Code (markup):
Technoslab thanks for the code. I tested it and it doesn't seem to be working, I did a bit of research and it looks like I need to enable the zip extension on my server, so I will figure out how to do that and test the code again. Thanks again for your help.
Hey Technoslab, I have enabled zip on my server and the code works perfect! Thank you! Just one thing though, the extracted filename seems to all have zero at the end ex. if the zip filename is 0060ab38ffa3.zip the extracted filename will be 0060ab38ffa30.mp4 Any idea why? Here's my current code: <?php function unzip_rename($file){ if(is_file($file) && extension_loaded('zip')){ $z = new ZipArchive; $z->open($file); $numFiles = $z->numFiles; $index = $numFiles - 1; while($index >= 0){ $n = $z->getNameIndex($index); $pinfo = pathinfo($file); $fname = $pinfo['filename']; $z->extractTo('.',$n); rename($n,$fname.$index.".mp4"); $index--; } } } if ($handle = opendir('.')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { unzip_rename($file); echo "$file<br/>"; } } closedir($handle); } ?> Code (markup): Thanks again!
I added that intentionally. Suppose that you've an archive named a.zip and it contains 5 files, then all files in archive would be extracted like a0.mp4,a1.mp4,a2.mp4 and so on. If you don't want that, you can try this function : function unzip_rename($file){ if(is_file($file) && extension_loaded('zip')){ $z = new ZipArchive; $z->open($file); $numFiles = $z->numFiles; $index = $numFiles - 1; $n = $z->getNameIndex(0); $pinfo = pathinfo($file); $fname = $pinfo['filename']; $z->extractTo('.',$n); rename($n,$fname.".mp4"); } } Code (markup):
Oh I see Yeah the second function is what I wanted but the first one is definitely useful as well if you have multiple files inside an archive. Thanks for your help, really appreciate it.
Just a thought here - since almost all mp4 files are about as tightly compressed as they can get, zipping them usually leads to slightly larger files. Why is someone zipping compressed files? (The method is probably "storage", which tells you to not compress the file. Look in Winzip or some other program that tells you what the compression method is.)