Solution: Lot's o' Zipped Files

by John H
3 minutes

As none of you know I was faced with a drive full of zip files and the need to unzip them all and link to the files. The zipped files all contained the same files: basically FileName.zip contained an HTML file called Getd.html and an images folder named "images". There were roughly 1500 zip files that needed to be unzipped and renamed. Solution: The zip file themselves were uniquely named so if I could name the html file the zip name then I'd be good. Additionally they all shared the same Images so I could have one images folder to share for the group of html files. My options for unzipping the files was either to do it locally with a unix script - or to use PHP to unzip and rename the files. I opted for PHP. Unzipping the files The first thing I had to upload the files into a directory on the server. After that I started a php file in the same directory called zip.php. The first set of functions would look in the directory for any files with the zip extension. This was achieved with the glob(); function. $files = glob(".zip"); ZipArchive At this point I have an array called $files that can be run through a foreach loop.
foreach($files as $file) { Using the loop I can then unzip them and place them into a folder. To unzip the files I used the PHP built in ZipArchive Object. This object has many features that can be accessed and manipulated in php. The first thing to do is declare the object $zip = new ZipArchive; Next use the object's open function to create a Zip resource and pass it my array values through the $file variable. $zip = new ZipArchive; $res = $zip->open($file); The next step is to use $zip->extractTo() function to put the $zip resource into a directory This is accomplished like this, and I conclude the script by closing the $zip object. if ($res === TRUE) {
$zip->extractTo("Directory"); $zip->close();
This worked. Here is the complete code here $files = glob("
.zip"); foreach($files as $file) { $zip = new ZipArchive; $res = $zip->open($file); if ($res === TRUE) { $zip->extractTo("Directory"); $zip->close();
} }

This handled putting the zip file contents into a directory called "Directory".

To rename the files I used PHP's rename(); function. This worked flawlessly.

rename('Directory/fileToRename.html', 'newFileName.html');

Related Articles

JavaScript Concatenation and Multiple Line Variables

JavaScript has some idiosyncrasies that make me want to pull my hair out sometimes. The worst part...

John H John H
2 minutes

Amazon Linux additional SSH users

https://blog.e-zest.com/how-to-add-ssh-users-in-amazon-linux/...

John H John H
~1 minute

PopUp Video On Visitor's First Visit

I've been working on a pop up window that will play a video the first time a user visits a...

John H John H
~1 minute