简体   繁体   English

将内存中的PHP数组压缩为.tar.gz文件(PHAR?)

[英]Compress in-memory PHP array to a .tar.gz file (PHAR?)

I have a large in-memory array of xml-files as string. 我有大量的xml文件作为字符串存储在内存中。

I'm looking for the most efficient way to create a readable/extractable .tar.gz file from PHP (5.3). 我正在寻找从PHP(5.3)创建可读/可解压缩.tar.gz文件的最有效方法。

Currently i'm using this class . 目前,我正在使用此类

But : 但是:

  • It does not look maintained 它看起来没有维护
  • It requires files (so i'm creating thousands of files from my memory array which are useless a few ms later) 它需要文件(所以我要从我的内存阵列创建成千上万个文件,这些文件在几毫秒后就没用了)

So i'm looking for a efficient way to handle a .tar.gz file, most important point is to be able to easily put files in it with a simple command like : 因此,我正在寻找一种处理.tar.gz文件的有效方法,最重要的一点是能够使用以下简单命令轻松将文件放入其中:

$myArchive->putFileFromString($fileName, $dataString); $ myArchive-> putFileFromString($ fileName,$ dataString);

Looks like it is possible to configure PHAR with TAR & GZ, so it may be a solution? 看起来可以用TAR和GZ配置PHAR,所以这可能是一个解决方案?

$p = $p->convertToExecutable(Phar::TAR, Phar::GZ);

Bonus question, i am not sure if it would be better to build it live (after each xml string generation) to save memory, or just wait this huge object & compress it once. 额外的问题,我不确定是否最好在每个xml字符串生成之后实时构建它以节省内存,或者只是等待这个巨大的对象并压缩一次。 The ratio proc_usage / mem_usage is unclear to me in this case. 在这种情况下,我不清楚proc_usage / mem_usage之比。

Target max usage is 100K XML files. 目标最大使用量为100K XML文件。

Thanks! 谢谢!

It's a bit late, but for future reference : 有点晚了,但是供以后参考:

You can create a tar.gz archive using the PharData class from the the phar extension. 您可以使用phar扩展中的PharData类创建tar.gz归档文件。 It's a bit like the ZipArchive, except that you can add content using the Array interface : 有点像ZipArchive,不同之处在于您可以使用Array接口添加内容:

$archive = new PharData("outfile.tar");
foreach ($xmlfiles as $name => $data) {
    $archive[$name] = $data;
}

// compress to "outfile.tar.gz
$gzipped = $archive->compress(Phar::GZ);

// delete outfile.tar
unset($archive);
unlink("outfile.tar");

If you want to create tgz files, use PEAR's Archive_Tar package. 如果要创建tgz文件,请使用PEAR的Archive_Tar软件包。 It allows you to create tar + gz files on the fly from memory: 它允许您从内存中即时创建tar + gz文件:

require_once 'Archive/Tar.php';
$t = new Archive_Tar('/path/to/dest.tar.gz', 'gz');
foreach ($xmlfiles as $key => $content) {
    $t->addString($key . '.xml', $content);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM