/    Sign up×
Community /Pin to ProfileBookmark

Read text file contents from zip file.

Hi all

I’m using the following code to read the contents of a zip file:

[code=php]
<?php

$zip = zip_open(“test.zip”);

if ($zip) {

while ($zip_entry = zip_read($zip)) {
echo “Name: ” . zip_entry_name($zip_entry) . “n”;
echo “Actual Filesize: ” . zip_entry_filesize($zip_entry) . “n”;
echo “Compressed Size: ” . zip_entry_compressedsize($zip_entry) . “n”;
echo “Compression Method: ” . zip_entry_compressionmethod($zip_entry) . “n”;

if (zip_entry_open($zip, $zip_entry, “r”)) {
echo “File Contents:n”;
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
echo “$bufn”;

zip_entry_close($zip_entry);
}
echo “n”;

}

zip_close($zip);

}

?>
[/code]

This script works fine and prints this:

[CODE]
Name: example 1.mp3
Actual Filesize: 1204777
Compressed Size: 1094660
Compression Method: deflated
File Contents: ID3)TT2Louggggtyf MTEN iTunes v4.9COMhengiTunNORM 00000C56 00000C7D 00006619 00
Name: example 1.wav
Actual Filesize: 5292068
Compressed Size: 4934316
Compression Method: deflated
File Contents: RIFFÀPWAVEfmt D¬±dataø¿Pûÿþÿûÿþÿýÿþÿþÿúÿýÿ÷ÿþÿúÿ
Name: racer.txt
Actual Filesize: 5292068
Compressed Size: 4934316
Compression Method: deflated
File Contents: racer text saved in file.
Name: descr.txt
Actual Filesize: 5292068
Compressed Size: 4934316
Compression Method: deflated
File Contents: descr text saved in file.
[/CODE]

This is where I get stuck….

I don’t need all this info, all I need is the following as $variables:

  • 1. a list of the files in the zip file.

  • 2. the file contents of racer.txt

  • 3. the file contents of descr.txt
  • like this:

    [CODE]
    Zip contents:
    example 1.mp3
    example 1.wav
    racer.txt
    descr.txt
    ——————-
    racer text:
    racer text saved in file.
    ——————-
    descr text:
    descr text saved in file.
    ——————-
    [/CODE]

    Can this be done?

    Any ideas?

    Cheers
    Chris

    to post a comment
    PHP

    4 Comments(s)

    Copy linkTweet thisAlerts:
    @emin3mJul 24.2005 — try this

    iev not tested it :rolleyes:

    [code=php]<?php

    $zip = zip_open("test.zip");

    if ($zip) {

    while ($zip_entry = zip_read($zip)) {
    echo "Name: " . zip_entry_name($zip_entry) . "n";

    }
    zip_close($zip);
    }
    echo "-----------------";

    $zip = zip_open("test.zip");
    if($zip){
    while ($zip_entry = zip_read($zip)) {
    if(substr(zip_entry_name($zip_entry) , -3, 3)=="txt")){
    echo substr(zip_entry_name($zip_entry) , 0, -3)." text:n";

    if (zip_entry_open($zip, $zip_entry, "r")) {

    $buf = zip_entry_read($zip_entry,

    zip_entry_filesize($zip_entry));
    echo "$bufn-----------------";

    zip_entry_close($zip_entry);
    }



    }


    }
    zip_close($zip);
    }


    ?> [/code]
    Copy linkTweet thisAlerts:
    @cybercampbellauthorJul 24.2005 — Cheers ?
    Copy linkTweet thisAlerts:
    @cybercampbellauthorJul 24.2005 — That worked fine but I need them as variables so can do things with the info.

    I like to use them like something similar to the following:
    [code=php]
    <?php
    //Do the zip stuff here....

    //stuff here
    echo $zip_contents;
    //stuff here
    echo $racer_text;
    //stuff here
    echo $descr_text;
    ?>
    [/code]


    Cheers

    Chris
    Copy linkTweet thisAlerts:
    @emin3mJul 25.2005 — nop cant do that

    u can use array variable for displaying file conntents

    as we cant initiliaze variable names dynamically

    can do with $zip_contents
    [code=php]
    <?php

    $zip = zip_open("test.zip");
    $zip_contents="Zip Contents:n";
    if ($zip) {

    while ($zip_entry = zip_read($zip)) {
    $zip_contents.= zip_entry_name($zip_entry) . "n";

    }
    zip_close($zip);
    }
    echo $zip_contents;
    echo "-----------------";

    $zip = zip_open("test.zip");
    if($zip){
    while ($zip_entry = zip_read($zip)) {
    if(substr(zip_entry_name($zip_entry) , -3, 3)=="txt")){
    echo substr(zip_entry_name($zip_entry) , 0, -3)." text:n";

    if (zip_entry_open($zip, $zip_entry, "r")) {

    $buf = zip_entry_read($zip_entry,

    zip_entry_filesize($zip_entry));
    echo "$bufn-----------------";

    zip_entry_close($zip_entry);
    }



    }


    }
    zip_close($zip);
    }


    ?>
    [/code]
    ×

    Success!

    Help @cybercampbell spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...