2012-06-29 40 views
23
function uncompress($srcName, $dstName) { 
    $sfp = gzopen($srcName, "rb"); 
    $fp = fopen($dstName, "w"); 

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

Tôi đã thử mã này nhưng điều này không làm việc hãy giúp tôiLàm cách nào để trích xuất hoặc giải nén tệp gzip bằng php?

Cảm ơn bạn

+7

Chúng ta cần nhiều hơn "nó không hoạt động" . Nó làm gì? Bạn nhận được thông báo lỗi nào? – meagar

+0

Mã của bạn có vẻ tốt (nó chunks dữ liệu trong khối 4kb) vì vậy nó nên được ánh sáng trên RAM. Lỗi nào là/bạn đang nhận được? – Lusitanian

+1

Tôi không nhận được gì đơn giản là tôi không giải nén bất kỳ tệp nào của tôi – Farzamtm

Trả lời

50

Hãy thử điều này tìm thấy here

//This input should be from somewhere else, hard-coded in this example 
$file_name = '2013-07-16.dump.gz'; 

// Raising this value may increase performance 
$buffer_size = 4096; // read 4kb at a time 
$out_file_name = str_replace('.gz', '', $file_name); 

// Open our files (in binary mode) 
$file = gzopen($file_name, 'rb'); 
$out_file = fopen($out_file_name, 'wb'); 

// Keep repeating until the end of the input file 
while (!gzeof($file)) { 
    // Read buffer-size bytes 
    // Both fwrite and gzread and binary-safe 
    fwrite($out_file, gzread($file, $buffer_size)); 
} 

// Files are done, close files 
fclose($out_file); 
gzclose($file); 
Các vấn đề liên quan