Skip to content
Snippets Groups Projects
Commit 530d6729 authored by Andreas Gohr's avatar Andreas Gohr
Browse files

updated php-archive to 1.0.7 fixes #1296

parent 0ef04790
No related branches found
No related tags found
No related merge requests found
...@@ -69,3 +69,5 @@ vendor/*/*/test/* ...@@ -69,3 +69,5 @@ vendor/*/*/test/*
vendor/*/*/doc/* vendor/*/*/doc/*
vendor/*/*/docs/* vendor/*/*/docs/*
vendor/*/*/contrib/* vendor/*/*/contrib/*
vendor/splitbrain/php-archive/apigen.neon
vendor/splitbrain/php-archive/generate-api.sh
...@@ -54,16 +54,16 @@ ...@@ -54,16 +54,16 @@
}, },
{ {
"name": "splitbrain/php-archive", "name": "splitbrain/php-archive",
"version": "1.0.4", "version": "1.0.7",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/splitbrain/php-archive.git", "url": "https://github.com/splitbrain/php-archive.git",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68" "reference": "c075304b44c4aadff0718af445e86bf730f331ff"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/splitbrain/php-archive/zipball/6572e78ef9d064eeb5c74d4ffe61b473a4996b68", "url": "https://api.github.com/repos/splitbrain/php-archive/zipball/c075304b44c4aadff0718af445e86bf730f331ff",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68", "reference": "c075304b44c4aadff0718af445e86bf730f331ff",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -97,7 +97,7 @@ ...@@ -97,7 +97,7 @@
"unzip", "unzip",
"zip" "zip"
], ],
"time": "2015-07-24 11:36:49" "time": "2015-08-12 13:24:34"
} }
], ],
"packages-dev": [], "packages-dev": [],
......
...@@ -351,7 +351,7 @@ class ClassLoader ...@@ -351,7 +351,7 @@ class ClassLoader
foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) { foreach ($this->prefixLengthsPsr4[$first] as $prefix => $length) {
if (0 === strpos($class, $prefix)) { if (0 === strpos($class, $prefix)) {
foreach ($this->prefixDirsPsr4[$prefix] as $dir) { 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; return $file;
} }
} }
...@@ -361,7 +361,7 @@ class ClassLoader ...@@ -361,7 +361,7 @@ class ClassLoader
// PSR-4 fallback dirs // PSR-4 fallback dirs
foreach ($this->fallbackDirsPsr4 as $dir) { foreach ($this->fallbackDirsPsr4 as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file; return $file;
} }
} }
...@@ -380,7 +380,7 @@ class ClassLoader ...@@ -380,7 +380,7 @@ class ClassLoader
foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
if (0 === strpos($class, $prefix)) { if (0 === strpos($class, $prefix)) {
foreach ($dirs as $dir) { foreach ($dirs as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file; return $file;
} }
} }
...@@ -390,7 +390,7 @@ class ClassLoader ...@@ -390,7 +390,7 @@ class ClassLoader
// PSR-0 fallback dirs // PSR-0 fallback dirs
foreach ($this->fallbackDirsPsr0 as $dir) { foreach ($this->fallbackDirsPsr0 as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
return $file; return $file;
} }
} }
......
...@@ -49,17 +49,17 @@ ...@@ -49,17 +49,17 @@
}, },
{ {
"name": "splitbrain/php-archive", "name": "splitbrain/php-archive",
"version": "1.0.4", "version": "1.0.7",
"version_normalized": "1.0.4.0", "version_normalized": "1.0.7.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/splitbrain/php-archive.git", "url": "https://github.com/splitbrain/php-archive.git",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68" "reference": "c075304b44c4aadff0718af445e86bf730f331ff"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/splitbrain/php-archive/zipball/6572e78ef9d064eeb5c74d4ffe61b473a4996b68", "url": "https://api.github.com/repos/splitbrain/php-archive/zipball/c075304b44c4aadff0718af445e86bf730f331ff",
"reference": "6572e78ef9d064eeb5c74d4ffe61b473a4996b68", "reference": "c075304b44c4aadff0718af445e86bf730f331ff",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -68,7 +68,7 @@ ...@@ -68,7 +68,7 @@
"require-dev": { "require-dev": {
"phpunit/phpunit": "4.5.*" "phpunit/phpunit": "4.5.*"
}, },
"time": "2015-07-24 11:36:49", "time": "2015-08-12 13:24:34",
"type": "library", "type": "library",
"installation-source": "dist", "installation-source": "dist",
"autoload": { "autoload": {
......
...@@ -3,5 +3,6 @@ ...@@ -3,5 +3,6 @@
composer.phar composer.phar
vendor/ vendor/
composer.lock composer.lock
apigen.phar
docs/
...@@ -19,8 +19,11 @@ Usage ...@@ -19,8 +19,11 @@ Usage
----- -----
The usage for the Zip and Tar classes are basically the same. Here are some 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 examples for working with TARs to get you started.
comments for more info
Check the [API docs](https://splitbrain.github.io/php-archive/) for more
info.
```php ```php
require_once 'vendor/autoload.php'; require_once 'vendor/autoload.php';
...@@ -51,16 +54,17 @@ $tar->close(); ...@@ -51,16 +54,17 @@ $tar->close();
// To create a TAR archive directly in memory, create() it, add*() // To create a TAR archive directly in memory, create() it, add*()
// files and then either save() or getArchive() it: // files and then either save() or getArchive() it:
$tar = new Tar(); $tar = new Tar();
$tar->setCompression(9, Archive::COMPRESS_BZIP);
$tar->create(); $tar->create();
$tar->addFile(...); $tar->addFile(...);
$tar->addData(...); $tar->addData(...);
... ...
$tar->save('myfile.tgz'); // compresses and saves it $tar->save('myfile.tbz'); // compresses and saves it
echo $tar->getArchive(Archive::COMPRESS_GZIP); // compresses and returns 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 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. 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 The FileInfo class can be used to specify additional info like ownership or permissions when adding a file to
an archive. an archive.
\ No newline at end of file
...@@ -126,3 +126,7 @@ class ArchiveIOException extends \Exception ...@@ -126,3 +126,7 @@ class ArchiveIOException extends \Exception
class ArchiveIllegalCompressionException extends \Exception class ArchiveIllegalCompressionException extends \Exception
{ {
} }
class ArchiveCorruptedException extends \Exception
{
}
...@@ -61,6 +61,7 @@ class FileInfo ...@@ -61,6 +61,7 @@ class FileInfo
$file->setMode(fileperms($path)); $file->setMode(fileperms($path));
$file->setOwner(fileowner($path)); $file->setOwner(fileowner($path));
$file->setGroup(filegroup($path)); $file->setGroup(filegroup($path));
$file->setSize(filesize($path));
$file->setUid($stat['uid']); $file->setUid($stat['uid']);
$file->setGid($stat['gid']); $file->setGid($stat['gid']);
$file->setMtime($stat['mtime']); $file->setMtime($stat['mtime']);
......
...@@ -36,6 +36,8 @@ class Tar extends Archive ...@@ -36,6 +36,8 @@ class Tar extends Archive
$this->compressioncheck($type); $this->compressioncheck($type);
$this->comptype = $type; $this->comptype = $type;
$this->complevel = $level; $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 ...@@ -366,7 +368,7 @@ class Tar extends Archive
public function save($file) public function save($file)
{ {
if ($this->comptype === Archive::COMPRESS_AUTO) { 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())) { if (!file_put_contents($file, $this->getArchive())) {
...@@ -429,7 +431,12 @@ class Tar extends Archive ...@@ -429,7 +431,12 @@ class Tar extends Archive
@gzseek($this->fh, $bytes, SEEK_CUR); @gzseek($this->fh, $bytes, SEEK_CUR);
} elseif ($this->comptype === Archive::COMPRESS_BZIP) { } elseif ($this->comptype === Archive::COMPRESS_BZIP) {
// there is no seek in bzip2, we simply read on // 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 { } else {
@fseek($this->fh, $bytes, SEEK_CUR); @fseek($this->fh, $bytes, SEEK_CUR);
} }
...@@ -513,15 +520,19 @@ class Tar extends Archive ...@@ -513,15 +520,19 @@ class Tar extends Archive
/** /**
* Decode the given tar file header * Decode the given tar file header
* *
* @param string $block a 512 byte block containign the header data * @param string $block a 512 byte block containing the header data
* @return array|bool * @return array|false returns false when this was a null block
* @throws ArchiveCorruptedException
*/ */
protected function parseHeader($block) protected function parseHeader($block)
{ {
if (!$block || strlen($block) != 512) { 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++) { for ($i = 0, $chks = 0; $i < 148; $i++) {
$chks += ord($block[$i]); $chks += ord($block[$i]);
} }
...@@ -535,12 +546,12 @@ class Tar extends Archive ...@@ -535,12 +546,12 @@ class Tar extends Archive
$block $block
); );
if (!$header) { if (!$header) {
return false; throw new ArchiveCorruptedException('Failed to parse header');
} }
$return['checksum'] = OctDec(trim($header['checksum'])); $return['checksum'] = OctDec(trim($header['checksum']));
if ($return['checksum'] != $chks) { if ($return['checksum'] != $chks) {
return false; throw new ArchiveCorruptedException('Header does not match it\'s checksum');
} }
$return['filename'] = trim($header['filename']); $return['filename'] = trim($header['filename']);
...@@ -613,7 +624,9 @@ class Tar extends Archive ...@@ -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 * 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 ...@@ -622,14 +635,25 @@ class Tar extends Archive
*/ */
public function filetype($file) 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); $file = strtolower($file);
if (substr($file, -3) == '.gz' || substr($file, -4) == '.tgz') { 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') { } elseif (substr($file, -4) == '.bz2' || substr($file, -4) == '.tbz') {
$comptype = Archive::COMPRESS_BZIP; return Archive::COMPRESS_BZIP;
} else {
$comptype = Archive::COMPRESS_NONE;
} }
return $comptype;
return Archive::COMPRESS_NONE;
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment