# phar API update

- Thread: https://externals.io/message/36948
- Original archive: https://news-web.php.net/php.internals/36948
- Started by Gregory Beaver on 2008-04-11T13:55:41+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #36948 — Gregory Beaver — 2008-04-11T13:55:41+00:00

URL: https://externals.io/message/36948#36948

Hi,

A quick update on Phar's API for those who are keeping score:

1) Phar->isWritable() now works properly and does what Liz was hoping:
tells you whether you can actually modify the phar archive by looking at
the archive's file perms as well as phar.readonly
2) PharFileInfo->getContent() added to retrieve file contents like
$phar['a.txt']->getContent()
3) addFile/addFromString/addEmptyDir added to match ext/zip API
4) fixed compression API.  Here are the new methods:

bool Phar->isCompressed([int compression]) // takes Phar::GZ, Phar::BZ2
or no parameter to check for any whole-archive compression
Phar|PharData Phar->compress(int compression[, string ext]) // takes
Phar::GZ/Phar::BZ2 and optional file extension to override default renaming
Phar|PharData Phar->decompress([, string ext]) // takes optional file
extension to override default renaming
bool Phar->compressFiles(int compression) // takes Phar::GZ/Phar::BZ2
and compressed all files within the archive
bool Phar->decompressFiles() // decompress all files within the archive
Phar Phar->convertToExecutable([int format[, int compression[, string
ext]]])
PharData Phar->convertToData([int format[, int compression[, string ext]]])
// takes Phar::TAR, Phar::ZIP, or Phar::PHAR as format
// takes Phar::GZ, Phar::BZ2 as compression
// optional file extension to override default renaming

PharFileInfo->isCompressed([int compression]) // takes Phar::GZ,
Phar::BZ2 or no parameter to check for any compression of file within
the archive
PharFileInfo->compress(int compression) // Phar::GZ/Phar::BZ2
PharFileInfo->decompress()

Here's code example of the new API:

<?php
$phar = new Phar('blah.phar');
$tar = $phar->convertToData(Phar::TAR); // creates blah.tar
$tgz = $tar->compress(Phar::GZ); // creates blah.tar.gz
$phargz = $tar->convertToExecutable(Phar::PHAR, Phar::GZ, '.gz.phar');
// creates blah.gz.phar (default would be blah.phar.gz)

var_dump($tgz->isCompressed(), $tgz->isCompressed(Phar::GZ),
$tgz->isCompressed(Phar::BZ2)); // true, true, false

$phar['a.txt'] = 'hi';
echo $phar['a.txt']->getContent(); // "hi"

$phar->compressFiles(Phar::GZ); // internal files compressed with zlib
compression
var_dump($phar['a.txt']->isCompressed(),
$phar['a.txt']->isCompressed(Phar::GZ),
$phar['a.txt']->isCompressed(Phar::BZ2)); // true, true, false
$phar['a.txt']->decompress(); // now this specific file is not
compressed within the archive

$phar->decompressFiles(); // internal files decompressed
$phar['a.txt']->compress(Phar::BZ2); // now this specific file is
compressed with bzip2 compression

$tar2 = $tgz->decompress('.2.tar'); // creates blah.2.tar
$phartar2 = $tar2->convertToExecutable(); // create blah.2.phar.tar
?>

Thanks,
Greg
