如何使用php解压txt.gz文件并存储到数据库中

如何使用php解压txt.gz文件并存储到数据库中,php,mysql,gzip,unzip,datafeed,Php,Mysql,Gzip,Unzip,Datafeed,我的zip文件名是Product_Catalog.txt.gz。此zip文件包含一个txt文件 如何提取并存储到数据库中 我已经在zip文件中解压并存储到我的数据库中。但是我不能理解.gz格式。因此,请建议 我的邮政编码在这里 if (realpath($destinationname."/".$filename)){ if ($zip = zip_open(realpath($directory."/".$filename))) { while

我的zip文件名是Product_Catalog.txt.gz。此zip文件包含一个txt文件

如何提取并存储到数据库中

我已经在zip文件中解压并存储到我的数据库中。但是我不能理解.gz格式。因此,请建议

我的邮政编码在这里

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);
if(realpath($destinationname./“$filename)){
如果($zip=zip_open(realpath($directory./“$filename)))
{
而($zip\u entry=zip\u read($zip))){
$zipfilename=zip\u条目\u名称($zip\u条目);
$zipfilename。“
”; $tmpfname=tempnam(“/tmp”,”); $handle=fopen($tmpfname,“w+”); 而($data=zip\u entry\u read($zip\u entry,50000000000)){ fwrite($handle,$data); }//当$data结束时结束 fseek($handle,0); 如果($可分离资产); $table=strtok($zipfilename,“-”); $sql=“如果不存在,则创建表”`$TABLE`LIKE`cjfeeds`”; mysql_查询($sql);

如何将此代码转换为txt.gz

您可以通过的函数与
.gz
文件交互


回应你的评论:

由于
gz
文件中不包含多个文件,因此不需要执行
while($zip\u entry=zip\u read($zip))
,因为
gz
文件只是一个已压缩的文件。不幸的是,这也意味着没有与
zip\u entry\u name
等效的文件,因为只有一个文件

如果您试图读取tarball(一个文件归档,这些文件已连接在一起并压缩成
.tar.gz
文件),那么
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\u entry\u read
处理文件块一样,也可以使用
gzfile
将所有行读取到数组中。有一个
readgzfile
函数可以获取整个文件,但不幸的是,它似乎只是将输出直接转储到客户端,而不是文件。您可以使用输出缓冲来捕获该输出,但考虑到其他选项似乎太麻烦了。

通常用于单个文件,它的文件格式与。虽然相同的压缩算法()如果在两种格式中都使用,则头文件是完全不同的,因此PHP的Zip函数将无法识别您的文件

相反,您可以使用
compress.zlib://
打开gzip压缩文件,然后使用普通流函数读取文件

$handle = fopen("compress.zlib://$filename", 'r');
但是,也有一些限制;例如,无法以读写模式打开gzip文件,并且查找速度可能较慢。如有必要,您可以通过制作临时未压缩副本来解决这些问题:

copy("compress.zlib://$filename", $tmpfname);

你一直称它为zip文件。它不是zip文件(
.zip
)。它是gzip文件(
.gz
)。格式不同

您可以使用解压缩gzip文件。

您不需要解压缩

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;";
您可以使用此代码。此代码对您非常有帮助。

试试这个

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";
    }
}
if(isset($\u文件['zipfile']))
{
$filename=$_文件['zipfile']['name'];
$source=$_文件['zipfile']['tmp_名称'];
$type=$\u文件['zipfile']['type'];
/*$name=explode('.zip',$filename);#$name[0]返回文件名。$name[1]返回扩展名(zip)
*/#保存文件的位置。即“extracted/myFile-02151985/”
$target=物理路径。“上传/”;
//确保选择了正确的文件类型。
$accepted_types=array('application/zip','application/x-zip-compressed','multipart/x-zip','application/x-compressed');
foreach($mime\u类型接受为$mime\u类型)
{
if($mime_type==$type)
{
$okay=正确;
打破
} 
}
$okay=strtolower($name[1])==“zip”?true:false;
如果(!$OK)
{
$msg=“请选择一个zip文件,dummy!”;
}
$saved_file_location=$target.$filename;
如果(移动上传的文件($source,$target.$filename))
{
全球美元目标;
全局$unique_文件夹;
$zip=新的ZipArchive();
$x=$zip->open($saved\u file\u location);
如果($x==true)
{
$folder=$zip->extractTo($target);
对于($i=0;$i<$zip->numFiles;$i++)
{ 
echo$entry=$zip->getNameIndex($i);
$filename=explode(“/”,$entry);
打印(文件名);
如果($filename[1]!='')
{ 
$filename_folder=$filename[0];
$filename=$filename[1]//
$name=explode('.zip',$filename_folder);#$name[0]返回文件名。$name[1]返回扩展名(zip)
//回显'sfkdf'。$name[0];
$target=物理路径。“上传/”;
$sql=“通过`id`desc`从`tracks`顺序中选择`id`”;
$list=mysql\u查询($sql);
$list\u num=mysql\u num\u行($list);
如果($list_num>0)
{
$res=mysql\u fetch\u数组($list);
$new_id=$res['id'];
}否则{
$new_id=1;
}
$check_for_jpg=strpos($filename,.mp3');
if($check_for_jpg==false){
echo“检查文件格式”;
$source=$ta