From c2490eaadc47587496e47cbc64e061bfcd0c0643 Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Wed, 12 Aug 2015 15:21:54 +0200 Subject: [PATCH] correctly skip in bzip streams. closes #9 --- src/Tar.php | 7 ++++++- tests/tar.test.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Tar.php b/src/Tar.php index d28b65e..e29c7d5 100644 --- a/src/Tar.php +++ b/src/Tar.php @@ -431,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); } diff --git a/tests/tar.test.php b/tests/tar.test.php index c5a3296..3474c1d 100644 --- a/tests/tar.test.php +++ b/tests/tar.test.php @@ -155,6 +155,37 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase } } + /** + * Create an archive and unpack it again + */ + public function test_dogfood() { + foreach ($this->extensions as $ext) { + $input = glob(dirname(__FILE__) . '/../src/*'); + $archive = sys_get_temp_dir() . '/dwtartest' . md5(time()) . '.' . $ext; + $extract = sys_get_temp_dir() . '/dwtartest' . md5(time() + 1); + + $tar = new Tar(); + $tar->create($archive); + foreach($input as $path) { + $file = basename($path); + $tar->addFile($path, $file); + } + $tar->close(); + $this->assertFileExists($archive); + + $tar = new Tar(); + $tar->open($archive); + $tar->extract($extract, '', '/FileInfo\\.php/', '/.*\\.php/'); + + $this->assertFileExists("$extract/Tar.php"); + $this->assertFileExists("$extract/Zip.php"); + $this->assertFileNotExists("$extract/FileInfo.php"); + + self::rdelete($extract); + unlink($archive); + } + } + /** * Extract the prebuilt tar files */