在过去的两年里,当我尝试确定文件的大小时,特别是 gz zip 压缩文件的大小时,我断断续续地遇到了一个奇怪的问题。我已经找到了解决方法,但它们并不理想。问题是 gzseek() 似乎总是寻找大约。无论未压缩文件的大小如何,文件大小均为 2.14GB。测试时,我通过 1) 解压缩并保存为文本,2) 使用 gzread() 读取 1MB 直到文件末尾来确定未压缩文件的大小。假设未压缩的文件大小为 13MB。
使用 gzseek() 和 gztell() 测试代码。这将使句柄前进 1mb / 1000000 字节,但始终持续到大约。2.14GB,无论未压缩文件大小如何:
//GZ file is opened ....
gzseek($Handle, 0, SEEK_SET);
while (true) {
//Seek through file advancing offset with 1000000 bytes each time
$Eof = gzseek($Handle, 1000000, SEEK_CUR); //0 or -1 if passed eof
//This will dump the handle position incrementing 1000000 bytes at the time but continue until
//approx. 2.14 GB even through file is 13MB uncompressed
var_dump(gztell($Handle));
//When the handle (via gztell() ) shows 2.14GB, the gzseek() returns -1 which means it
//has reached / gone pas end of file
if ( $Eof !== -1 ) {
//This will only be true once the gztell() shows approx. 2.14GB
break;
}
}
现在,如果使用 gzread() ,它将正常工作,句柄将前进 1mb/1000000 字节,直到 13mb。例如:
while ( !gzeof($Handle) ) {
$Data = gzread($Handle, 1000000);
}
在过去的几年里,我对此进行了大量研究,但从未找到测量 gz 文件大小的有效解决方案,也没有任何关于为什么不能使用 gzseek 完成此操作的报告,我觉得这有点奇怪。要么 gzseek 不起作用,我希望找到报告,要么我真的在这里遗漏了一些东西。
POPMUISE