【问题标题】:What File Format Does PharData::extractTo Extract Files As?PharData::extractTo 将文件提取为什么文件格式?
【发布时间】:2013-04-06 22:29:23
【问题描述】:

我正在使用 PHP PharData 类的 extractTo 方法来检查 phar 文件的内容并遇到一些策略结果。我已经达到了我的字节级侦探工作的极限,希望这里的人能够帮助我解决这个问题。

细节如下,但一般来说:当我使用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 文件夹中。

我正在提取的存档can be found here

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

但是,如果我使用 quicklook、vicat 等其他工具,我会看到二进制数据。我在尝试通过文件内容ack/grep 时注意到了这一点,但没有得到预期的结果。

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

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

并使用十六进制编辑器检查文件确认文件以 BZ 标头开头

但是,尝试使用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 文件,尽管它在文件的中间会被这个

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.

【问题讨论】:

  • 你看到php.net/manual/en/phardata.extractto.php下面的评论了吗?它说的是旧 Mac 系统上的 pax 存档格式。
  • 我看到了,但我不确定我是否遵循它的应用方式(如果有的话)。似乎是说一些较旧的 Mac OS 版本的 PHP 会生成 pax 样式的 phar 存档,但 extractTo 仅支持取消存档 ustar 变体。由于我没有生成存档,并且存档确实提取正确,因此我不确定它是否相关。 (我的意思是,我真的不确定:我以前没有研究过 phar 的东西)
  • 抱歉,我弄错了 - 在第一次阅读后,我理解为某些系统(并提到您正在使用的 Mac OS)将 phar 提取到 pax 档案中......

标签: php hex bzip2 phar


【解决方案1】:

使用extractTo,文件以与存档相同的格式存储!这可以是以下之一:无、gzip、bzip2

虽然您当然可以以这种格式保存它们,然后尝试以某种方式提取它,但我建议如下: 将 phar 转换为 未压缩 phar 并提取该存档!

方法如下:

<?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

这将为您提供所有未压缩的文件!

【讨论】:

    【解决方案2】:

    这是一个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 将以下内容输出到标准错误:

    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 命令报告该文件是一个有效的 bzip2 文件,块大小为 400k:

    $ 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 文件的程序是导致此问题的原因。

    【讨论】:

    • 侦探工作+1,可能是最终的最佳答案。我深入研究了 PHP 的 phar 创建代码并发现了无数问题,虽然我不能指出这些问题是导致这些奇怪的 bzfiles 的原因,但我认为这是罪魁祸首。
    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多