This maybe a silly question to most, but I have the following folder, which I want to zip.
FolderI
Folder1
Folder2
Folder3
..
..
FolderN
Now, zipping up FolderI
into a zipFile.zip
is pretty easy because of so many resources online, specially here! But, I want to create zipFile.zip
, containing Folder1...FolderN
, such that when the file is unzipped, it would directly populate the current directory with Folder1...FolderN
,instead of creating FolderI
, which has Folder1...FolderN
in it.
Some of the things I've been trying are:
$zip = new ZipArchive();
// open archive
if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("FolderI/"));
// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
$zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}
// close and save archive
$zip->close();
This was picked up from compress/archive folder using php script and I tried to change it around. Didn't work though - The FolderI is still zipped. But, when I unzip the my-archive.zip
, I get a FolderI
and then Folder1..FolderN
inside it. I also saw code from @Alix somewhere here, which did the same thing. But, didn't really follow much.
Any kind of help would be great. If not the code, at least point me to the right direction.
Thanks
****Anyone here knows how to do this? ****
@Mods and Admins: Is the solution too simple or PHP can't do this at all?