SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Downloading and unpackaging a gzip file

    I'm getting myself a little confused about this.

    Here is how I am doing it for a ZIP file:

    Download cURL function:

    PHP Code:
    function grab_file($url$new_file) {

        
    //get file
        
    $ch curl_init();
        
    $fp fopen("zip/$new_file""w");

        
    $options = array(CURLOPT_URL => $urlCURLOPT_HEADER => 0CURLOPT_FAILONERROR =>
            
    1CURLOPT_AUTOREFERER => 1CURLOPT_BINARYTRANSFER => 1CURLOPT_RETURNTRANSFER =>
            
    1CURLOPT_FOLLOWLOCATION => 1CURLOPT_TIMEOUT => 5CURLOPT_ENCODING => 1,
            
    CURLOPT_FILE => $fp);

        
    curl_setopt_array($ch$options);
        
    $file curl_exec($ch);
        
    curl_close($ch);
        
    fclose($fp);

        if (!
    $file) {

            return 
    false;

        } else {

            return 
    true;

        }

    The unzip function:

    PHP Code:
    function get_zip($data) {

        
    $zip = new ZipArchive();
        
    $filename $data;

        if (
    $zip->open($filenameZIPARCHIVE::CREATE || ZIPARCHIVE::OVERWRITE)) {

            
    $zip->extractTo('feeds');
            
    $zip->close();

            return 
    true;

        } else {

            return 
    false;

        }

    I'm getting myself confused about the Zlib functions http://www.php.net/manual/en/ref.zlib.php

    Unlike zip, these functions read it to memory, right?

    So would I have to create some code like below and then create a XML or CVS file out of the PHP array?

    PHP Code:
       $zd gzopen($gz_file"r");

        
    $data gzread($zd1000000);
        
    gzclose($zd);

        if (
    $data !== FALSE) {

        return 
    $data;

        } else {

        return 
    FALSE
    Let everyday be Christmas

  2. #2
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've found a solution.

    This downloads and unzips the file:

    PHP Code:
    $new_file uniqid();
    $sfp gzopen($url"rb");
     
    $fp fopen("feeds/$new_file.csv""w");

    while (
    $string gzread($sfp4096)) {
                    
    fwrite($fp$stringstrlen($string));
    }
    gzclose($sfp);
    fclose($fp); 
    Let everyday be Christmas

  3. #3
    SitePoint Addict Divisive Cotton's Avatar
    Join Date
    Jun 2008
    Location
    Andy lives in London, UK
    Posts
    393
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    actually, still not quite right.

    Instead of using a random string I would like to use the name of the file basename

    The problem is is that the url is a redirect (ie: htp://datafeed.api.productserve.com/datafeed/download/apikey/1a5de2d28b6bbc860d98b3d69bc69aec/mid/736/columns/aw_deep_link,aw_image_url,aw_product_id,aw_thumb_url,brand_id,commission_group,mpn,parent_product_id,pre_order,web_offer/format/xml/compression/gzip/) so the file name isn't apparent until after it has been downloaded...
    Let everyday be Christmas


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •