updated php-archive to 1.0.7 fixes #1296

This commit is contained in:
Andreas Gohr 2015-08-13 10:40:35 +02:00
parent 0ef04790d1
commit 530d672995
9 changed files with 70 additions and 34 deletions

2
.gitignore vendored
View File

@ -69,3 +69,5 @@ vendor/*/*/test/*
vendor/*/*/doc/*
vendor/*/*/docs/*
vendor/*/*/contrib/*
vendor/splitbrain/php-archive/apigen.neon
vendor/splitbrain/php-archive/generate-api.sh

10
composer.lock generated
View File

@ -54,16 +54,16 @@
},
{
"name": "splitbrain/php-archive",
"version": "1.0.4",
"version": "1.0.7",
"source": {
"type": "git",
"url": "https://github.com/splitbrain/php-archive.git",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68"
"reference": "c075304b44c4aadff0718af445e86bf730f331ff"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/splitbrain/php-archive/zipball/6572e78ef9d064eeb5c74d4ffe61b473a4996b68",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68",
"url": "https://api.github.com/repos/splitbrain/php-archive/zipball/c075304b44c4aadff0718af445e86bf730f331ff",
"reference": "c075304b44c4aadff0718af445e86bf730f331ff",
"shasum": ""
},
"require": {
@ -97,7 +97,7 @@
"unzip",
"zip"
],
"time": "2015-07-24 11:36:49"
"time": "2015-08-12 13:24:34"
}
],
"packages-dev": [],

View File

@ -351,7 +351,7 @@ class ClassLoader
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
}
@ -361,7 +361,7 @@ class ClassLoader
// PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
@ -380,7 +380,7 @@ class ClassLoader
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}
@ -390,7 +390,7 @@ class ClassLoader
// PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file;
}
}

View File

@ -49,17 +49,17 @@
},
{
"name": "splitbrain/php-archive",
"version": "1.0.4",
"version_normalized": "1.0.4.0",
"version": "1.0.7",
"version_normalized": "1.0.7.0",
"source": {
"type": "git",
"url": "https://github.com/splitbrain/php-archive.git",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68"
"reference": "c075304b44c4aadff0718af445e86bf730f331ff"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/splitbrain/php-archive/zipball/6572e78ef9d064eeb5c74d4ffe61b473a4996b68",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68",
"url": "https://api.github.com/repos/splitbrain/php-archive/zipball/c075304b44c4aadff0718af445e86bf730f331ff",
"reference": "c075304b44c4aadff0718af445e86bf730f331ff",
"shasum": ""
},
"require": {
@ -68,7 +68,7 @@
"require-dev": {
"phpunit/phpunit": "4.5.*"
},
"time": "2015-07-24 11:36:49",
"time": "2015-08-12 13:24:34",
"type": "library",
"installation-source": "dist",
"autoload": {

View File

@ -3,5 +3,6 @@
composer.phar
vendor/
composer.lock
apigen.phar
docs/

View File

@ -19,8 +19,11 @@ 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 source code
comments for more info
examples for working with TARs to get you started.
Check the [API docs](https://splitbrain.github.io/php-archive/) for more
info.
```php
require_once 'vendor/autoload.php';
@ -51,16 +54,17 @@ $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.tgz'); // compresses and saves it
echo $tar->getArchive(Archive::COMPRESS_GZIP); // compresses and returns it
$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.
an archive.

View File

@ -126,3 +126,7 @@ class ArchiveIOException extends \Exception
class ArchiveIllegalCompressionException extends \Exception
{
}
class ArchiveCorruptedException extends \Exception
{
}

View File

@ -61,6 +61,7 @@ class FileInfo
$file->setMode(fileperms($path));
$file->setOwner(fileowner($path));
$file->setGroup(filegroup($path));
$file->setSize(filesize($path));
$file->setUid($stat['uid']);
$file->setGid($stat['gid']);
$file->setMtime($stat['mtime']);

View File

@ -36,6 +36,8 @@ class Tar extends Archive
$this->compressioncheck($type);
$this->comptype = $type;
$this->complevel = $level;
if($level == 0) $this->comptype = Archive::COMPRESS_NONE;
if($type == Archive::COMPRESS_NONE) $this->complevel = 0;
}
/**
@ -366,7 +368,7 @@ class Tar extends Archive
public function save($file)
{
if ($this->comptype === Archive::COMPRESS_AUTO) {
$this->setCompression($this->filetype($this->complevel, $file));
$this->setCompression($this->complevel, $this->filetype($file));
}
if (!file_put_contents($file, $this->getArchive())) {
@ -429,7 +431,12 @@ class Tar extends Archive
@gzseek($this->fh, $bytes, SEEK_CUR);
} elseif ($this->comptype === Archive::COMPRESS_BZIP) {
// there is no seek in bzip2, we simply read on
@bzread($this->fh, $bytes);
// bzread allows to read a max of 8kb at once
while($bytes) {
$toread = min(8192, $bytes);
@bzread($this->fh, $toread);
$bytes -= $toread;
}
} else {
@fseek($this->fh, $bytes, SEEK_CUR);
}
@ -513,15 +520,19 @@ class Tar extends Archive
/**
* Decode the given tar file header
*
* @param string $block a 512 byte block containign the header data
* @return array|bool
* @param string $block a 512 byte block containing the header data
* @return array|false returns false when this was a null block
* @throws ArchiveCorruptedException
*/
protected function parseHeader($block)
{
if (!$block || strlen($block) != 512) {
return false;
throw new ArchiveCorruptedException('Unexpected length of header');
}
// null byte blocks are ignored
if(trim($block) === '') return false;
for ($i = 0, $chks = 0; $i < 148; $i++) {
$chks += ord($block[$i]);
}
@ -535,12 +546,12 @@ class Tar extends Archive
$block
);
if (!$header) {
return false;
throw new ArchiveCorruptedException('Failed to parse header');
}
$return['checksum'] = OctDec(trim($header['checksum']));
if ($return['checksum'] != $chks) {
return false;
throw new ArchiveCorruptedException('Header does not match it\'s checksum');
}
$return['filename'] = trim($header['filename']);
@ -613,7 +624,9 @@ class Tar extends Archive
}
/**
* Guesses the wanted compression from the given filename extension
* Guesses the wanted compression from the given file
*
* Uses magic bytes for existing files, the file extension otherwise
*
* You don't need to call this yourself. It's used when you pass Archive::COMPRESS_AUTO somewhere
*
@ -622,14 +635,25 @@ class Tar extends Archive
*/
public function filetype($file)
{
// for existing files, try to read the magic bytes
if(file_exists($file) && is_readable($file) && filesize($file) > 5) {
$fh = fopen($file, 'rb');
if(!$fh) return false;
$magic = fread($fh, 5);
fclose($fh);
if(strpos($magic, "\x42\x5a") === 0) return Archive::COMPRESS_BZIP;
if(strpos($magic, "\x1f\x8b") === 0) return Archive::COMPRESS_GZIP;
}
// otherwise rely on file name
$file = strtolower($file);
if (substr($file, -3) == '.gz' || substr($file, -4) == '.tgz') {
$comptype = Archive::COMPRESS_GZIP;
return Archive::COMPRESS_GZIP;
} elseif (substr($file, -4) == '.bz2' || substr($file, -4) == '.tbz') {
$comptype = Archive::COMPRESS_BZIP;
} else {
$comptype = Archive::COMPRESS_NONE;
return Archive::COMPRESS_BZIP;
}
return $comptype;
return Archive::COMPRESS_NONE;
}
}