Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP & Zip Files

Status
Not open for further replies.

YoungManRiver

IS-IT--Management
Feb 9, 2004
220
US
All,

Looking in PHP manual there is an "addfile", but no "pullfile" routine in the zip library. The extractTo, extracts all the files and I need to selectively extract files from the zip.

Does anyone have a sample/example of how to do this?

YMR
 
jpadie,

Tried ur solution, but doesn't work. Here is my code:
Code:
  function open_zip($doc_str) {
    $pth_lin = getcwd();
    $ful_fil = $pth_lin.'\\'.$doc_str;
    $zp = new ZipArchive;
    $zip = zip_open($ful_fil);
    if ($zip) {
       while ($zip_entry = zip_read($zip)) {
         $zp_fls[] = zip_entry_name($zip_entry);
       }
       asort($zp_fls);
       $k=0;
       $zp->open($ful_fil);
       foreach ($zp_fls as $vv) {
         $k++;
         echo "Name:    " . $vv . "<br>";
         $zp_nfile = $pth_lin.'\\'.$vv;
         $cmd = "unzip $zp_nfile";
         exec (escapeshellcmd($cmd));
         if ($k >= 3) {
            break;
         }
       }
    }
    zip_close($zip);
  }
It is supposed to pull the first three entries in the zip file, but doesn't pull anything, yet. This code has ur solution in it. U have any idea why it is not working?

Thanks!

YMR

 
the term "doesn't work" is not helpful in diagnosing the problem. the code i posted should work on all *nix systems that has unzip installed and appropriate permissions set on the cwd.

you are using a class in your sample above, but you do not provide us with the code of the class. the way you have integrated my code to yours does not seem correct to me.

why not go back to basics. Create a zip archive with a few files in it. run my code against the zip file and see what happens. remember, of course, that the $file variable that you pass to the code must be the full path to the file.

Code:
function unZip($file){
 if (is_file($file)){
 $command = "unzip $file";
 exec (escapeshellcmd($command));
 } else {
 die("file does not exist");
 }
}
 
jpadie,

The reason it "doesn't work" is your assumption. You assumed I wanted to extract a .zip file. Reading carefully you would have understood that worked already, without a jump to the command shell and just running "extractTo".

Not so! I must extract the first three files contained in the .zip file. If you look at the code I put up it becomes apparent that the command, you gave me, can not work as there is no such file on the HD, because "file" exists only inside of the .zip file.

So now what? Back to the original Q, how do I extract explicit files from a .zip file?

YMR
 
this is not complex

you just extract the contents of the whole zip file to a directory, take those files that you want and delete the rest. this, to me, is the quickest and cleanest route. least coding, least room for error and least trusting of user input.

basic rule: never never never trust user submitted data. By inference you are trusting potentially binary files that are uploaded by users and then you are also trusting that the particular program that they use to create these binary files does so in a way that will always guarantee the order that the compressed files are stored in the archive. I would not be so trusting. even in an intranet scenario.
 
jpadie,

Ho, Ho NOOOO!

Ur trying to think ahead of me. All this code is for a process that runs files by the quarter keeps shells of the program files in a .zip and moves them to another .zip when completed. That is why it must be explicit extract.

I know ur trying to be helpful and I appreciate it, but the target file folder is too full of files to easily find the newly extracted files, so do not think it practical to add to that complexity with your proposed method. Something like over 100 .zip files. Extracted files are .doc, which have to be manually edited. English composition, editing sort of thing, that can't be automated. Only automation available is to:

1. zip all files by quarter,
2. keep them until needed,
3. run this process to extract needed files,
4. edit,
5. zip into completed zip file.

Otherwise this directory would have over 5,000 file and it would be impossible to get work done. Biggest errors would be wrong files would be processed and that would be a disaster.

I could however, using your idea as a work around:

1.create a temp dir under the existing folder,
2.extract all files there,
3.copy the needed files back to the parent dir,
4.erase the remaining files and delete the temp dir.

Seems like a lot of work to do something that should be an integrated feature of this tool set.

Going to go up on PHP site an look to see it there is an update or beta that has this feature. If not then will put in the suggestion for it, since it doesn't seem to exist.

YMR
 
i maintain that it is not good practice to trust user input, whether this be the position of files in an archive or anything else.

as for "a lot of work", i'd say it's 3-5 extra lines of code, depending how you do it.

do not rely on native php zip manipulation - it's never been good. you would be better off looking in the pear repository.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top