首页 > php > PHP:使用passthru仅在mac os x上传输zip的问题

PHP:使用passthru仅在mac os x上传输zip的问题 (PHP: Problem using passthru to stream a zip on mac os x only)

问题

我试图通过使用Unix的zip命令和PHP的passthru函数来组合一个zip流式解决方案,但我遇到了麻烦。

脚本看起来像这样:

<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachement; filename=myfile.zip");
passthru("zip -r -0 - /stuff/to/zip/");
exit();
?>

zip命令工作正常,浏览器接收输出并保存为zip文件。然后可以在Windows和Unix上提取zip,但在Mac OS X上,内置提取器(BOMArchiveHelper)无法提取文件。在OS X上使用其他应用程序工作正常。

如果zip受密码保护(不由应用程序处理),BOMArchiveHelper给出的错误与它给出的相同。我使用了某种类型的zip分析器程序,它表明zip存档中的一些文件被标记为受密码保护。就像我说的那样,其他提取应用程序显然没有注意到这一点。

当拉近拉链时,我发现PHP文件生成的文件比服务器上的zip命令直接生成的文件大几个字节。似乎带有passthru的流进程会在文件中添加一些可能导致BOMArchiveHelper出现问题的内容。

为了测试这个,我使用passthru来流式传输我已经在服务器上创建的zip:passthru(“cat stuff.zip”)这与BOMArchiveHelper配合得很好。

所以这个问题似乎存在于passthru函数通过zip命令获取动态生成的二进制数据并将其传递给浏览器的过程中。

我试图消除所有可以生成额外字节的源(将zip命令设置为quiet等),但添加的数据仍然存在。流式zip的二进制差异和预生成的zip表明额外的数据分散在整个zip中,而不仅仅是在结尾或开头。

任何人都有线索,或者之前看过这个问题并决定不可能解决?

注意:由于其他人已经遇到并在我面前很好地描述了这个问题而没有任何答案我只是在这里复制/粘贴他的信息,并确保他的所有测试都有效地失败了,我的任何一个都没有通过......

显然,让这个工作的唯一方法是让人们使用解压缩或者足够的扩展...

解决方法

如果您使用的是nginx,请查看http://wiki.nginx.org/NginxNgxZip

问题

I'm trying to put together a zip streaming solution through the use of Unix's zip command and PHP's passthru function, but I've hit a snag.

The script looks something like this:

<?php
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachement; filename=myfile.zip");
passthru("zip -r -0 - /stuff/to/zip/");
exit();
?>

The zip command works OK and the output is received by the browser and saved as a zip file. The zip can then be extracted fine on Windows and Unix, but on Mac OS X the build in extractor (BOMArchiveHelper) can't extract the file. Using other applications on OS X works fine though.

The error given by BOMArchiveHelper is the same it gives if a zip is password protected (not handled by the application). I used some kind of zip analyzer program and it indicated that some of the files in the zip archive were flagged as password protected. Like I said though, no other extraction application pays attention to that apparently.

When examening the zip closer I found that the one generated by the PHP files is a few bytes larger than one generated directly by the zip command on the server. It seems that the stream process with passthru adds something to the file that probably causes the problems with BOMArchiveHelper.

To test this, I used passthru to stream a zip I had already created on the server: passthru("cat stuff.zip") That worked fine with BOMArchiveHelper.

So the problem seems to lie somewhere in the process where the passthru function takes the binary data generated on the fly by the zip command and passes that to the browser.

I've tried to eliminate all the sources where the extra bytes could be generated (setting zip command to quiet and so on), but the added data still remains. A binary diff of the streamed zip and a pre generated zip shows that the extra data is scattered all over the zip, not just in the end or the beginning.

Anyone have a clue, or seen this problem before and decided it's impossible to solve?

NB: Since someone else has already encountered and very well described this issue before me without any answer I just copied/paste his message here and made sure that all his tests did effectively fail and neither any of mine passed ...

Apparently the only way to get this to work would be to ask people to use either unzip or suffitexpander ...

解决方法

If you are using nginx then take a look at http://wiki.nginx.org/NginxNgxZip

相似信息