首页 > 解决方案 > 从 php popen 压缩和从命令行不同的结果

问题描述

我正在尝试使用 php 从 apache 提供整个目录的下载 我使用这个问题的答案Zip Stream in PHP

但是,当我比较(使用 cksum)下载的 zip 和从命令行使用相同命令创建的 zip 时,结果是不同的。

这是我从 PHP 所做的:

<?php
$dir = "/path/to/myfolder";
$ficzip = basename($name) . ".zip";
header('Content-Transfer-Encoding: binary');
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="' . $ficzip. '"');
chdir( $dir );
$stream = popen( "/usr/bin/zip -qr -1 - .", "r" );
if( $stream )
{
    fpassthru( $stream );
    fclose( $stream );
}
?>

这是我从命令行执行的操作:

cd "/path/to/myfolder"
/usr/bin/zip -qr -1 - . > /tmp/myfolder.zip

我希望两个 cksum 相同,但它们不是。我错过了什么?怎么做才能使它们相同?

编辑

这是两个文件的 zipinfo 之间的差异(在左侧下载的文件和右侧的命令行文件上)。

7,9c7,9
<   Zip archive file size:                 248145030 (000000000ECA6486h)
<   Actual end-cent-dir record offset:     248145008 (000000000ECA6470h)
<   Expected end-cent-dir record offset:   248145008 (000000000ECA6470h)
---
>   Zip archive file size:                 248144998 (000000000ECA6466h)
>   Actual end-cent-dir record offset:     248144976 (000000000ECA6450h)
>   Expected end-cent-dir record offset:   248144976 (000000000ECA6450h)
16c16
<   is 248144779 (000000000ECA638Bh).
---
>   is 248144747 (000000000ECA636Bh).
31c31
<   compression sub-type (deflation):               normal
---
>   compression sub-type (deflation):               fast
33c33
<   extended local header:                          yes
---
>   extended local header:                          no
59,60d58
<   There are an extra 16 bytes preceding this file.
< 
63,64c61,62
<   offset of local header from start of archive:   248143809
<                                                   (000000000ECA5FC1h) bytes
---
>   offset of local header from start of archive:   248143793
>                                                   (000000000ECA5FB1h) bytes
70c68
<   compression sub-type (deflation):               normal
---
>   compression sub-type (deflation):               fast
72c70
<   extended local header:                          yes
---
>   extended local header:                          no

编辑2

用无压缩替换-1最快压缩-Z store可以减少差异,但仍然不行。

7,9c7,9
<   Zip archive file size:                 249693306 (000000000EE2047Ah)
<   Actual end-cent-dir record offset:     249693284 (000000000EE20464h)
<   Expected end-cent-dir record offset:   249693284 (000000000EE20464h)
---
>   Zip archive file size:                 249693274 (000000000EE2045Ah)
>   Actual end-cent-dir record offset:     249693252 (000000000EE20444h)
>   Expected end-cent-dir record offset:   249693252 (000000000EE20444h)
16c16
<   is 249693055 (000000000EE2037Fh).
---
>   is 249693023 (000000000EE2035Fh).
32c32
<   extended local header:                          yes
---
>   extended local header:                          no
58,59d57
<   There are an extra 16 bytes preceding this file.
< 
62,63c60,61
<   offset of local header from start of archive:   249690256
<                                                   (000000000EE1F890h) bytes
---
>   offset of local header from start of archive:   249690240
>                                                   (000000000EE1F880h) bytes
70c68
<   extended local header:                          yes
---
>   extended local header:                          no

解决方案

这是我用来获得相同结果的命令。我将问题保持打开状态,以便有人可以解释命令的 popen 和终端执行之间的区别。

/usr/bin/zip -qr -0 -fd -fz- - .

标签: phpzip

解决方案


推荐阅读