2013-04-14 81 views
5

我正在使用PHP PharData类的extractTo方法来检查phar文件的内容并运行一些平稳的结果。我已经达到了我的字节级侦探工作的极限,并希望这里有人能够帮我解决这个问题。什么文件格式PharData :: extractTo将文件解压缩为?

详情如下,但一般来说:当我使用PharData::extractTo提取存档文件时,我得到的文件是bzip变量,但bzip2命令不喜欢它们。这是正常的phar行为,还是与特定档案有关? (或者我使用的PHP/OS组合)。有没有一种方法可以将纯文本文件从phar归档文件中取出 - 或者应该将纯文本作为默认文件,并且我正在查看奇怪的系统行为?

具体而言,当我运行命令

$phar = new Phar('n98-magerun.phar'); 
$phar->extractTo('/tmp/n98-magerun'); 

在我的OS 10.6.8,基于Intel的Mac使用内置的PHP 5.3.6,存档成功提取到/ tmp/N98-magerun夹。

enter image description here

我解压can be found here存档。

如果我打开BBEdit中提取的任何文本文件,我会看到正确的内容。

enter image description here

然而,如果我使用其他工具,如快速查找,vi,或cat,我看到的二进制数据。我在通过文件内容尝试ack/grep时注意到了这一点,并且我没有得到我期望的结果。

enter image description here

如果我使用file命令上的文件,它的报告说,这是一个bzip文件。

$ file MIT-LICENSE.txt 
MIT-LICENSE.txt: bzip2 compressed data, block size = 400k 

,并检查与十六进制编辑器文件确认文件开始用BZ

enter image description here

但是,试图与bzip2结果的文件解压缩以下错误

$ bzip2 -d MIT-LICENSE.txt 
bzip2: Can't guess original name for MIT-LICENSE.txt -- using MIT-LICENSE.txt.out 

bzip2: Compressed file ends unexpectedly; 
    perhaps it is corrupted? *Possible* reason follows. 
bzip2: No such file or directory 
    Input file = MIT-LICENSE.txt, output file = MIT-LICENSE.txt.out 

It is possible that the compressed file(s) have become corrupted. 
You can use the -tvv option to test integrity of such files. 

You can use the `bzip2recover' program to attempt to recover 
data from undamaged sections of corrupted files. 

bzip2: Deleting output file MIT-LICENSE.txt.out, if it exists. 

和我可以bzcat文件成功,虽然它在中期barf该文件与此

bzcat: Compressed file ends unexpectedly; 
    perhaps it is corrupted? *Possible* reason follows. 
bzcat: Undefined error: 0 
    Input file = MIT-LICENSE.txt, output file = (stdout) 

It is possible that the compressed file(s) have become corrupted. 
You can use the -tvv option to test integrity of such files. 

You can use the `bzip2recover' program to attempt to recover 
data from undamaged sections of corrupted files. 
+0

你见过下面的评论http://php.net/manual/en/phardata.extractto.php吗?它介绍了旧版Mac系统上的'pax'存档格式。 –

+0

我看到了这一点,但我不确定我是否遵循了它的适用方式(如果有的话)。这似乎是说一些较旧的Mac OS版本的PHP生成pax样式的phar归档文件,但'extractTo'只支持取消归档ustar变体。由于我没有生成档案,并且档案没有正确提取,所以我不确定它是否相关。 (我的意思是,我真的不知道:我之前没有在phar的东西上砍过) –

+0

对不起,我错了 - 第一次阅读后,我将它理解为一些系统(并提到Mac OS,正在使用)提取phar到pax档案... –

回答

1

DLE这是一个bzip2文件,但将其解压,你需要使用--stdout(或-c)选项(见下文)。

您需要--stdout选项的原因是该文件不以.bz2扩展名结尾,这将允许bunzip2确定要解压缩到的结果文件名。

$ bunzip2 --stdout MIT-LICENSE.txt 2>/dev/null 
Copyright (c) 2012 netz98 new media GmbH 

http://www.netz98.de 

Permission is hereby granted, free of charge, to any person obtaining 
a copy of this software and associated documentation files (the 
"Software"), to deal in the Software without restriction, including 
without limitation the rights to use, copy, modify, merge, publish, 
distribute, sublicense, and/or sell copies of the Software, and to 
permit persons to whom the Software is furnished to do so, subject to 
the following conditions: 

The above copyright notice and this permission notice shall be 
included in all copies or substantial portions of the Software. 

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE 

我不知道为什么bunzip2被outputing以下标准错误:

bzip2: Compressed file ends unexpectedly; 
     perhaps it is corrupted? *Possible* reason follows. 
bzip2: Success 
     Input file = MIT-LICENSE.txt, output file = (stdout) 

It is possible that the compressed file(s) have become corrupted. 
You can use the -tvv option to test integrity of such files. 

You can use the `bzip2recover' program to attempt to recover 
data from undamaged sections of corrupted files. 

由于file命令报告文件是400K的块大小的有效bzip2的文件:

$ file MIT-LICENSE.txt 
MIT-LICENSE.txt: bzip2 compressed data, block size = 400k 

我试过将-4选项加到bunzip2,但它仍然抱怨:

$ bunzip2 -d -4 -vvvvv -c MIT-LICENSE.txt >/dev/null 
    MIT-LICENSE.txt: 
    [1: huff+mtf rt+rld {0x2010d4b9, 0x2010d4b9}] 
    combined CRCs: stored = 0x2010d4b9, computed = 0x2010d4b9 
    [1: huff+mtf 
bunzip2: Compressed file ends unexpectedly; 
     perhaps it is corrupted? *Possible* reason follows. 
bunzip2: Success 
     Input file = MIT-LICENSE.txt, output file = (stdout) 

It is possible that the compressed file(s) have become corrupted. 
You can use the -tvv option to test integrity of such files. 

You can use the `bzip2recover' program to attempt to recover 
data from undamaged sections of corrupted files. 

所以我的猜测是创建这些bzip2文件的程序是造成这个问题的原因。

+0

+1为侦探工作,并且可能最终的最佳答案。我已经深入研究了PHP的'phar'创建代码,发现了很多问题,虽然我不能指出它们是造成这些奇怪的'bz'文件的原因,但我指出我是伪劣的实施者。 –

0

使用extractTo这些文件的存储格式与它们在存档中的格式相同!这可能是一个:没有,gzip的,bzip2的

虽然你当然可以以这种格式保存,然后尝试以某种方式提取出来,我建议如下: 转换的药业为未压缩药业和提取物存档!

方法如下:

<?php 
$phar = new Phar('Someclass.phar'); 
$phar2 = $phar->convertToExecutable (Phar::TAR,Phar::NONE); //convert to an uncompressed tar archive 
$phar2->extractTo('/some/path/'); // extract all files 

这会给你解压缩的所有文件!