LoginSignup
17
19

More than 5 years have passed since last update.

PHP上でtarアーカイブファイルを作成

Last updated at Posted at 2014-04-02

Pharを使えばPHP上でシステムのコマンド叩いたりしなくてもtarアーカイブファイルを作成できる。

data.txt をアーカイブするサンプル。中身はLorem Ipsumを71KB分。gzip圧縮を用いる。

<?php

$tarPath = __DIR__ . '/data.tar';

$phar = new PharData($tarPath);

// -rw-r--r-- 1 fivestar fivestar  71K Apr  2 17:16 data.txt
$phar->addFile('data.txt');

$phar->compress(Phar::GZ, '.tgz');

// *.tar と *.tgz ができるので、 .tar は消す
unlink($tarPath);

アーカイブファイルを展開した結果。

% tar xvf data.tgz
data.txt

% ls -l
total 96K
-rw-r--r-- 1 fivestar fivestar 20K Apr  2 17:25 data.tgz
-rw-r--r-- 1 fivestar fivestar 71K Apr  2 17:25 data.txt
17
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
19