如何使用 PHP 解压缩 .gz 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3293121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:15:39  来源:igfitidea点击:

How can I unzip a .gz file with PHP?

phpcodeigniterunzip

提问by Shamoon

I'm using CodeIgniter and I can't figure out how to unzip files!

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

采纳答案by Sarfraz

Download the Unzip libraryand include or autoloadthe unziplibrary

下载解压库和包括或autoloadunzip图书馆

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

回答by Powerlord

PHP itself has a number of functions for dealing with gzip files.

PHP 本身有许多处理 gzip 文件的函数。

If you want to create a new, uncompressed file, it would be something like this.

如果你想创建一个新的、未压缩的文件,它会是这样的。

Note: This doesn't check if the target file exists first, doesn't delete the input file, or do any error checking. You really should fix those before using this in production code.

注意:这不会首先检查目标文件是否存在,不会删除输入文件,也不会进行任何错误检查。在生产代码中使用它之前,您确实应该修复这些问题。

// 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);

Note: This deals with gzip only. It doesn't deal with tar.

注意:这处理 gzip 。它不处理焦油。

回答by Danial

gzopen is way too much work. This is more intuitive:

gzopen 工作量太大了。这更直观:

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

works on http pages when the server is spitting out gzipped data also.

当服务器也吐出 gzipped 数据时,它也适用于 http 页面。

回答by bizauto

If you have access to system():

如果您有权访问 system():

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

回答by kiamlaluno

Use the functions implemented by the Zlib Compressionextension.

使用由Zlib 压缩扩展实现的函数。

This snippet shows how to use some of the functions made available from the extension:

此代码段显示了如何使用扩展中提供的一些功能:

// 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);

回答by McNab

Use the extractTofunction from PharData.

使用extractTo来自PharData函数

$sourceis the .gz archive file you want to unzip and $destDirthe directory you want to extract them to.

$source是要解压缩的 .gz 存档文件以及要解压缩到$destDir的目录。

$phar = new PharData($source);
$phar->extractTo($destDir,null,true); // extract all files, overwrites existing files