簡體   English   中英

如何使用 PHP 解壓縮 .gz 文件?

[英]How can I unzip a .gz file with PHP?

我正在使用 CodeIgniter,但不知道如何解壓縮文件!

PHP 本身有許多處理 gzip 文件的函數。

如果你想創建一個新的、未壓縮的文件,它會是這樣的。

注意:這不會首先檢查目標文件是否存在,不會刪除輸入文件,也不會進行任何錯誤檢查。 在生產代碼中使用它之前,您確實應該修復這些問題。

// This input should be from somewhere else, hard-coded in this example
$file_name = 'file.txt.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);

注意:這處理 gzip 。 它不處理焦油。

gzopen 工作量太大了。 這更直觀:

$zipped = file_get_contents("foo.gz");
$unzipped = gzdecode($zipped);

當服務器也吐出 gzipped 數據時,它也適用於 http 頁面。

如果您有權訪問 system():

system("gunzip file.sql.gz");

使用由Zlib 壓縮擴展實現的函數。

此代碼段顯示了如何使用擴展中提供的一些功能:

// open file for reading
$zp = gzopen($filename, "r");

// read 3 char
echo gzread($zp, 3);

// output until end of the file and close it.
gzpassthru($zp);
gzclose($zp);

下載解壓縮庫並包含或autoload unzip

$this->load->library('unzip');

我正在使用CodeIgniter,卻不知道如何解壓縮文件!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM