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:

function grab_file($url, $new_file) {

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

    $options = array(CURLOPT_URL => $url, CURLOPT_HEADER => 0, CURLOPT_FAILONERROR =>
        1, CURLOPT_AUTOREFERER => 1, CURLOPT_BINARYTRANSFER => 1, CURLOPT_RETURNTRANSFER =>
        1, CURLOPT_FOLLOWLOCATION => 1, CURLOPT_TIMEOUT => 5, CURLOPT_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:

function get_zip($data) {

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

    if ($zip->open($filename, ZIPARCHIVE::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?

   $zd = gzopen($gz_file, "r");

    $data = gzread($zd, 1000000);
    gzclose($zd);

    if ($data !== FALSE) {

    return $data;

    } else {

    return FALSE;

I’ve found a solution.

This downloads and unzips the file:

$new_file = uniqid();
$sfp = gzopen($url, "rb");
 $fp = fopen("feeds/$new_file.csv", "w");

while ($string = gzread($sfp, 4096)) {
                fwrite($fp, $string, strlen($string));
}
gzclose($sfp);
fclose($fp);

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…