Skip to content

About

Pure-PHP implementation to read and write TAR and ZIP archives

Topics

Resources

Stars

107 stars

Watchers

6 watching

Forks

Latest commit

 

History

133 Commits

Folders and files

Repository files navigation

PHPArchive - Pure PHP ZIP and TAR handling

This library allows to handle new ZIP and TAR archives without the need for any special PHP extensions (gz and bzip are needed for compression). It can create new files or extract existing ones.

To keep things simple, the modification (adding or removing files) of existing archives is not supported.

Install

Use composer:

php composer.phar require splitbrain/php-archive

Usage

The usage for the Zip and Tar classes are basically the same. Here are some examples for working with TARs to get you started.

Check the API docs for more info.

require_once 'vendor/autoload.php';
use splitbrain\PHPArchive\Tar;

// To list the contents of an existing TAR archive, open() it and use
// contents() on it:
$tar = new Tar();
$tar->open('myfile.tgz');
$toc = $tar->contents();
print_r($toc); // array of FileInfo objects

// To extract the contents of an existing TAR archive, open() it and use
// extract() on it:
$tar = new Tar();
$tar->open('myfile.tgz');
$tar->extract('/tmp');

// To create a new TAR archive directly on the filesystem (low memory
// requirements), create() it:
$tar = new Tar();
$tar->create('myfile.tgz');
$tar->addFile(...);
$tar->addData(...);
...
$tar->close();

// To create a TAR archive directly in memory, create() it, add*()
// files and then either save() or getArchive() it:
$tar = new Tar();
$tar->setCompression(9, Archive::COMPRESS_BZIP);
$tar->create();
$tar->addFile(...);
$tar->addData(...);
...
$tar->save('myfile.tbz'); // compresses and saves it
echo $tar->getArchive(); // compresses and returns it

Differences between Tar and Zip: Tars are compressed as a whole, while Zips compress each file individually. Therefore you can call setCompression before each addFile() and addData() function call.

The FileInfo class can be used to specify additional info like ownership or permissions when adding a file to an archive.

File name encoding

File names you pass to addFile(), addData() or a FileInfo object are expected to be UTF-8 encoded.

Zip archives record which encoding a name uses, so names read from a Zip are always returned as UTF-8. Names written outside the ASCII range are stored as UTF-8 and flagged as such, which is what current archivers expect.

Tar archives have no field for that. Names are written exactly as you pass them and returned exactly as they are stored. Anything written by a current tool uses UTF-8, but an older archive may use any encoding without saying so, and such names are returned unchanged. Use mb_check_encoding() if you need to know, and convert them yourself if you know what encoding they use.

Extracting untrusted archives

A small archive can expand to a very large size or to very many files. By default, extract() has no limits. Set limits before you extract archives from an untrusted source:

$zip = new Zip();
$zip->setExtractionSizeLimit(100 * 1024 * 1024); // bytes of all extracted files
$zip->setExtractionFileLimit(1000);              // number of extracted files and directories
$zip->open('upload.zip');
$zip->extract('/tmp/upload');

Before each entry, extract() compares the size declared in the archive with the limit. When an entry would exceed a limit, it throws an ArchiveLimitException. Files extracted up to that point stay on disk.

For Tar archives, the size limit also applies to all data that extract() reads from the uncompressed archive, including headers and skipped entries. Since Tar needs to decompress the file as a whole, this is the only way to limit the time to decompress a Tar archive.

About

Pure-PHP implementation to read and write TAR and ZIP archives

Topics

Resources

Stars

107 stars

Watchers

6 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages