i want list files inside archive, without extraction.
the types of archives interested in:
- .7z (7-zip)
- .rar (winrar)
- .tar (posix, e.g. gnu tar).
- .zip (iso standard, e.g. winzip)
for .zip files, have been able achieve this:
<?php $za = new ziparchive(); $za->open('thezip.zip'); ($i = 0; $i < $za->numfiles; $i++) { $stat = $za->statindex($i); print_r(basename($stat['name']) . php_eol); } ?>
however, have not managed same .7z files. haven’t tested .rar , .tar, need them well.
this has come before (for various reasons this , this , the 1 broken links in answer).
generally prevailing opinion @ moment create wrapper (either diy or use a library) relies on having 7-zip binary (executable) accessible on server , wrap calls binary using exec()
, rather pure php solution.
since 7zip format supports variety of compression algorithms, i'm assuming want pure php implementation of reading/decompressing lzma format. while there lzma sdks available c, c++, c# , java , has made php extension lzma2 (and fork lzma) yet though there has been interest on 7-zip forums quite while, no 1 seems have ported on comprehensive pecl extension or pure php yet.
depending on needs & motivation, leaves with:
- add 7-zip binary server, , use wrapper library, own or someone else's
- install , use unofficial pecl extension
- bravely port lzma sdk php (and contribute open source!)
for other formats can php documentation examples , details on usage:
- .rar has own official pecl extension
- .tar can extracted phar pecl extention (also see so examples)
- .zip has official pecl extension
- .gz has official pecl exension
- and couple of other formats
since of these involve pecl extensions, if you're limited webhost in way , need pure php solutions this, might easier shift more amenable webhost.
to attempt protect against zip bombs, can @ compression ratios suggested this answer (packed size divided unpacked size , treat on threshold invalid), although zip bomb talked answer 1 of linked questions indicate can ineffective against multi-layered zip bombs. need @ whether or not files you're listing archives well, ensuring you're not doing kind of recursive extraction , treat archives contain archives invalid.
for completeness, usage examples official pecl extensions:
rar:
<?php // open archive file $archive = rararchive::open('archive.rar'); // make sure it's valid if ($archive === false) return; // retrieve list of entries in archive $entries = $archive->getentries(); // make sure entry list valid if ($entries === false) return; // example output of entry count echo "found ".count($entries)." entries.\n"; // loop on entries foreach ($entries $e) { echo $e->getname()."\n"; } // close archive file $archive->close(); ?>
tar:
<?php // open archive file try { $archive = new phardata('archive.tar'); } // make sure it's valid catch (unexpectedvalueexception $e) { return; } // make sure entry list valid if ($archive->count() === 0) return; // example output of entry count echo "found ".$archive->count()." entries.\n"; // loop on entries (phardata list of entries in archive) foreach ($archive $entry) { echo $entry."\n"; } // no need close phardata ?>
zip (adapted op's question here):
<?php // open archive file $archive = new ziparchive; $valid = $archive->open('archive.zip'); // make sure it's valid (if not ziparchive::open() returns various error codes) if ($valid !== true) return; // make sure entry list valid if ($archive->numfiles === 0) return; // example output of entry count echo "found ".$archive->numfiles." entries.\n"; // loop on entries ($i = 0; $i < $archive->numfiles; $i++) { $e = $archive->statindex($i); echo $e['name']."\n"; } // close archive file (redundant called automatically @ end of script) $archive->close(); ?>
gz:
since gz (gnu zlib) compression mechanism rather archive format, different in php. if open .gz
file (rather treating .tar
) gzopen()
, reads transparently decompressed. since commonly .tar.gz
, can treat .tar
above (also see this answer on question). or can extract tar phardata::decompress()
in this answer on question.
Comments
Post a Comment