2013-04-14 155 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 

這會給你解壓縮的所有文件!