Check file sizes of content inside ZIP archive
Asked Answered
A

1

6

I need to open a ZIP archive, check the file size of each file inside the archive and return an array with the numeric values (sizes). I don't want the archive to be extracted to check the file sizes, possibly.

I tried a lot myself, but no ZIP function seems to have a feature like that and I couldn't think of any combination to write the function myself.

Abagael answered 19/8, 2012 at 12:59 Comment(3)
ZipArchive::statIndex does not work for you?Orest
ZipArchive::statIndex() is what you're looking for, if you just need to collect information about the archived files.Grave
Well, this must have slipped away somehow, but thanks, will try to get a function out of it.Abagael
P
9

I know this question is pretty old. I hope this answer could help someone.
The following code cycles up all the files inside the test2.zip file and prints its name and the sizes in bytes.

<?php
$zip = new ZipArchive;
$res = $zip->open('test2.zip');
if ($res) {
    $i=0;
    while(!empty($zip->statIndex($i)['name']))
    {
       echo "Filename: ".$zip->statIndex($i)['name']." | Size: ".$zip->statIndex($i)['size']." bytes<br>";
       $i++;
    }
}
?>

OUTPUT :

Filename: main.php | Size: 44 bytes
Filename: test.php | Size: 385 bytes
Filename: test2.json | Size: 35 bytes
Filename: Token.txt | Size: 5 bytes
Filename: trans.png | Size: 95442 bytes
Filename: url_xml.xml | Size: 399 bytes
Paragraphia answered 5/10, 2013 at 14:5 Comment(1)
You can also use ZipArchive::statName() if you know the exact path inside the ZIP archive.Vanessa

© 2022 - 2024 — McMap. All rights reserved.