Search code examples
phpmysqlgzipunzipdatafeed

how to unzip txt.gz file and store into database using php


My zip file name is Product_Catalog.txt.gz. this zip file contain one txt file.

how can i extract and store into my database

i have already done in zip file unzip and store into my database. but i can't understand .gz format. so please advise

my zip file code is here

if (realpath($destinationname."/".$filename)){
    if ($zip = zip_open(realpath($directory."/".$filename)))
        {
            while (($zip_entry = zip_read($zip))){
            $zipfilename=zip_entry_name($zip_entry  );
            $zipfilename."<br>";
            $tmpfname = tempnam("/tmp", "");
            $handle = fopen($tmpfname, "w+");
            while($data = zip_entry_read($zip_entry,50000000000)){
            fwrite($handle,$data);
            }// end while $data
            fseek($handle,0);
            if ($separatetables);
            $table=strtok($zipfilename,"-");
            $sql="CREATE TABLE IF NOT EXISTS `$table` LIKE `cjfeeds`";
             mysql_query($sql);

how can i convert this code into txt.gz


Solution

  • You don't extract.

    if ($rrpath){
         if ($zip = gzopen($rrpath, "rb"))
            {   
                $filenamen = $_SESSION['files_list'][$i];       
                $zipfilename = str_replace('.gz', '', $_SESSION['files_list'][$i]); 
                $tmpfname = tempnam("/home/demoosiz/tmp", "");      
                $handle = fopen($tmpfname, "w+");
                ini_set("max_execution_time",3000000000000);
                while($data = gzread($zip, 4096)){
                fwrite($handle,$data);
                }// end while $data
                fseek($handle,0);
                if ($separatetables);
                $table=strtok($zipfilename,"-");                   
               // $sqly = "TRUNCATE TABLE `cjfeeds`";
                //mysql_query($sqly);                           
                 // I'm not too sure if this is windows specific
                $tmpfile=addslashes($tmpfname);
                //if the script times out uncomment the next line 
                 ini_set("max_execution_time",3000000000000);
                $sql22 ="LOAD DATA LOCAL INFILE '$tmpfile' REPLACE  INTO TABLE `cjfeeds` FIELDS TERMINATED BY '$fieldseparator' IGNORE 1 LINES;";
    

    You can use this code. This code is very helpful for you.