3

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

4

5 に答える 5

2

一般的に単一ファイルに使用されるgzipは、 ZIPと同じファイル形式ではありません。両方の形式で同じ圧縮アルゴリズム(DEFLATE)が使用されていますが、ヘッダーは完全に異なるため、PHPのZip関数はファイルを認識しません。

代わりに、compress.zlib:// ラッパーを使用してgzip圧縮されたファイルを開くことができます。その後、通常のストリーム関数を使用してファイルを読み取ることができます。

$handle = fopen("compress.zlib://$filename", 'r');

ただし、いくつかの制限があります。たとえば、gzip圧縮されたファイルを読み取り/書き込みモードで開くことはできず、シークが遅くなる可能性があります。必要に応じて、一時的に非圧縮のコピーを作成することで、これらを回避できます。

copy("compress.zlib://$filename", $tmpfname);
于 2013-01-22T06:42:17.737 に答える
1

あなたはそれをzipファイルと呼び続けます。zipファイルではありません(.zip)。これはgzipファイルです(.gz)。別の形式。

gzdecode()gzipファイルを解凍するために使用できます。

于 2013-01-22T06:42:42.310 に答える
1

モジュール.gzの機能を介してファイルを操作できます。zlib


あなたのコメントへの返信:

ファイルには複数のファイルが含まれていないため、これを行う必要はありませwhile($zip_entry = zip_read($zip))ん。ファイルは、圧縮された単一gzのファイルです。残念ながら、これは、ファイルが1つしかないため、どちらに.gzも相当するものがないことも意味します。zip_entry_name

.tar.gztarball(連結されてgzipで圧縮されてファイルを形成したファイルのアーカイブ)を読み取ろうとしているZlib場合は、それを処理するように設計されていないため、適していません。PharData代わりに次のようなものが必要になります。

$phar = new PharData($filename);
foreach ($phar as $phar_stream) {
  $file_data = file_get_contents($phar_stream);
  // process $file_data how you like
}

これらが単一ファイルgzファイルの場合は、通常のファイルシステム関数のgzバージョンでファイルを読み取ることができます。

を使用gzreadして、現在実行している方法でファイルのチャンクを処理するzip_entry_readことも、すべての行を。を使用して配列に読み込むこともできますgzfile。ファイル全体を取得する関数がありreadgzfileますが、残念ながら、ファイルを実行するのではなく、出力をクライアントに直接ダンプするように見えます。出力バッファリングを使用してその出力をキャプチャすることもできますが、他のオプションを考えると面倒なようです。

于 2013-01-22T05:32:40.807 に答える
1

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.

于 2013-02-01T15:41:46.800 に答える
1

Try this....

if(isset($_FILES['zipfile'])) 
{

    $filename = $_FILES['zipfile']['name'];
    $source = $_FILES['zipfile']['tmp_name'];
    $type = $_FILES['zipfile']['type'];

    /*$name = explode('.zip', $filename); # $name[0] returns the name of the file. $name[1] returns the extension (zip)

    ; */ # Where the file will be saved. I.E. 'extracted/myFile-02151985/'
    $target = PHYSICAL_PATH."upload/";
    // Ensures that the correct file type was chosen.
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach($accepted_types as $mime_type)
    {
        if($mime_type == $type)
        {
            $okay = true;
            break;
        } 
    }

    $okay = strtolower($name[1]) == 'zip' ? true : false;

    if(!$okay) 
    {
        $msg="Please choose a zip file, dummy!";
    }
    $saved_file_location = $target.$filename;
    if(move_uploaded_file($source, $target . $filename))
    {
        global $target;
        global $unique_folder;
        $zip = new ZipArchive();
            $x = $zip->open($saved_file_location);
            if ($x === true) 
            {
                $folder=$zip->extractTo($target);
                for ( $i=0; $i < $zip->numFiles; $i++ ) 
                { 
                    echo $entry = $zip->getNameIndex($i); 
                    $filename=explode('/',$entry);
                    print_r($filename);
                    if($filename[1] != '')
                    { 
                        $filename_folder=$filename[0];
                        $filename = $filename[1]; //

                        $name = explode('.zip', $filename_folder); # $name[0] returns the name of the file. $name[1] returns the extension (zip)
                        //echo 'sfkdf'.$name[0];
                        $target = PHYSICAL_PATH."upload/";

                        $sql = "select `id` from `tracks` order by `id` desc";
                            $list = mysql_query($sql);
                            $list_num = mysql_num_rows($list);
                            if($list_num > 0)
                            {

                            $res = mysql_fetch_array($list);
                            $new_id=$res['id'];
                            } else {
                            $new_id=1;
                            }
                        $check_for_jpg = strpos($filename, '.mp3');

                        if ($check_for_jpg === false ){
                            echo 'Check file format.';
                            $source=$target . $name[0].'/'.$filename;
                            unlink($source);
                        } else {
                            $srch = array(' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','/','*','+','~','`','=');
                            $rep = array('_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_');
                            $name_song[$i]= str_replace($srch,$rep,$filename);
                            $title_name=explode('.',$filename);

                            $new_id=$new_id+1;
                            $new_filename=$new_id.'-'.$name_song[$i];
                            mysql_query("insert into `tracks` set track='".addslashes(stripslashes($new_filename))."',title='".addslashes(stripslashes($title_name[0]))."',tape_id='".$id_last."',addDate=NOW(),status=1 ");
                            $source=$target . $name[0].'/'.$filename; $target_move=$target.$new_filename;
                            if(rename($source,$target_move)) { unlink($source); } else { echo 'not';}
                            echo 'folder zip';
                            echo '<li>' . $new_filename . '</li>';
                        }
                        rmdir($target . $name[0]);
                    }
                    else { echo $target = PHYSICAL_PATH."upload/";
                        $sql = "select `id` from `test_table` order by `id` desc";

                        $list = mysql_query($sql);
                        $list_num = mysql_num_rows($list);
                        if($list_num > 0) {
                        //and, instead of using "while":
                        $res = mysql_fetch_array($list); // will return the highest "id" number
                        echo $new_id=$res['id'];
                        } else {
                        $new_id=1;
                        }

                        $check_for_jpg = strpos($entry, '.mp3');

                        if ($check_for_jpg === false ){
                            echo 'Check file format.';
                            $source=$target .$entry;
                            unlink($source);
                        } else {
                            $srch = array(' ','--','&quot;','!','@','#','$','%','^','&','*','(',')','_','+','{','}','|',':','"','<','>','?','[',']','\\',';',"'",',','/','*','+','~','`','=');
                            $rep = array('_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_','_');
                            $name_song[$i]= str_replace($srch,$rep,$entry);
                            $title_name=explode('.',$entry);
                            $new_id=$new_id+1;
                            $new_filename=$new_id.'-'.$name_song[$i];
                            mysql_query("insert into `tracks` set track='".addslashes(stripslashes($new_filename))."',title='".addslashes(stripslashes($title_name[0]))."',tape_id='".$id_last."',status=1 ");
                            $source=$target .$entry; $target_move=$target.$new_filename;
                            if(rename($source,$target_move)) { unlink($source); } else { echo 'not';}
                            echo 'only zip';
                            echo '<li>' . $new_filename . '</li>';
                        }

                    }
                }
                $zip->close();
                unlink($saved_file_location); #deletes the zip file. We no longer need it.
            } else {
                die("There was a problem. Please try again!");
            }

    } else {
        $msg="There was a problem";
    }
}
于 2013-02-07T13:50:09.550 に答える