I am developing a script using PHP zip archive class. My intention is to identify if the zip file contain a folder with the same name as the zip file name itself. If not then it will create a folder name as the zipfile name during unzipping. I have tried so far as follows: example: The zip file name is: TEST.zip my script will check if the TEST.zip file contains a folder "TEST"
$zip = new \ZipArchive();
$zip->open('TEST.zip');
if($zip->locateName('/TEST') !== false) {
// directory exists
}
Can anybody help me to find out how to do it with php ZIP::ARCHIVE class ?
I think you only had the slash in the wrong end:
$zip = new \ZipArchive();
$zip->open('TEST.zip');
if ($zip->locateName('TEST/') !== false) {
// directory exists
}
It's worth noting that it won't find TEST if it is a file rather than a directory—that's what the trailing slash accomplishes.
P.S. To list all names as seen by ZipArchive:
$i = 0;
while (($name = $zip->getNameIndex($i)) !== false) {
printf("%d: %s\n", $i, $name);
$i++;
}
0: empty-dir/
1: file
2: dir-with-contents/
3: dir-with-contents/test.txt