reading zip file contents

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
say i have a zip file - - with a file - somefile.txt - in it,
whose contents are as follows:

this is a test

when i try running the following script on the above zip file, i get
everything i should be getting, save for the contents of somefile.txt
(example output follows the script):

   $filename = '';
    if (($temp = zip_open($filename))) {
       while ($entry = zip_read($temp))
          if (preg_match('/\.txt$/',zip_entry_name($entry))) {
             print "file name = '".zip_entry_name($entry)."'\n";
             print "file size = '".zip_entry_filesize($entry)."'\n";
             print "contents = '".zip_entry_read($entry)."'\n";


the output is as follows:

file name = 'somefile.txt'
file size = '14'
contents = ''

the output i would like and expect is as follows:

file name = 'somefile.txt'
file size = '14'
contents = 'this is a test'

any ideas as to what the problem is?  could it be a bug in php?  any
help would be appreciated - thanks!

Re: reading zip file contents

yawnmoth wrote:
Quoted text here. Click to load it

You will first have to open the entry before you can read it:

if (zip_entry_open($temp, $entry)) {
    print "contents = '".zip_entry_read($entry, 1024)."'\n";


Site Timeline