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

发布于 2024-09-10 08:37:36 字数 38 浏览 3 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怂人 2024-09-17 08:37:36

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 。它不涉及焦油。

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

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.

最冷一天 2024-09-17 08:37:36

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

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

当服务器也吐出 gzip 压缩数据时,也适用于 http 页面。

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

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

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

以往的大感动 2024-09-17 08:37:36

如果您有权访问 system():

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

If you have access to system():

system("gunzip file.sql.gz");
一百个冬季 2024-09-17 08:37:36

使用 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);

Use the functions implemented by the Zlib Compression extension.

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);
桃气十足 2024-09-17 08:37:36

下载解压库
并包含或自动加载unzip

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

Download the Unzip library
and include or autoload the unzip library

$this->load->library('unzip');
安人多梦 2024-09-17 08:37:36

补充@Danial的答案...

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

我同意,在几十MB的文件上使用它是可以的,我想这是最频繁的使用,但是当我们达到数百MB时,最好使用带有 gzopen()gzread()

此外,可以将这 2 个解决方案结合在一起

Complementing @Danial's answer...

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

I agree, to use it on files with a few tens of MB is ok, I imagine this is the most frequent use, but when we reach hundreds of MB it is better to use a solution with gzopen() and gzread().

Furthermore, it is possible to have the 2 solutions together ???? just check the size of the gz file. If it is smaller than 70MB, for example, use file_get_contents(), if it is larger, use gzopen().

if (filesize("foo.xlsx.gz") <= (1024*1024*70)) {
  // file_get_contents() approach...
} else {
  // gzopen() approach...
}

Yes, I know that what matters most is the actual size of the decompressed file, but we can get a good idea since generally the compressed file is 30% smaller than the original file.

One line tip... to compress a file:

file_put_contents('output.gz', gzencode( file_get_contents('input.file'), 9));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文