Search code examples
phpfileziparchive

How to get compressed and uncompressed size for each file in zip archive with php?


I gets zip archive content with ZipArchive class in PHP. I need to display compressed and uncompressed size for each file in it. How to do this?

        $zip = new ZipArchive;
        $zip->open('download/' . $results[0]['filename']);
        $i = 0;
        while($name = $zip->getNameIndex($i)) {
            $files[] = array(
                'filename' => $name,
                'compressed_size' => ???,
                'uncompressed_size' => ???);
            $i++;
        }
        $zip->close();

Solution

  • If you read the docu https://www.php.net/manual/en/class.ziparchive.php you will find the function statIndex() (https://www.php.net/manual/en/ziparchive.statindex.php). Use it instead of function getNameIndex().