diff --git a/README.md b/README.md index d858803..f18764b 100644 --- a/README.md +++ b/README.md @@ -54,12 +54,13 @@ $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 diff --git a/src/Tar.php b/src/Tar.php index ef532a3..58bf446 100644 --- a/src/Tar.php +++ b/src/Tar.php @@ -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())) { diff --git a/tests/tar.test.php b/tests/tar.test.php index 6b44b0f..a43edea 100644 --- a/tests/tar.test.php +++ b/tests/tar.test.php @@ -101,7 +101,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase $tdir = ltrim($dir, '/'); $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); - $tar->create($tmp, Tar::COMPRESS_NONE); + $tar->create($tmp); $tar->AddFile("$dir/testdata1.txt"); $tar->AddFile("$dir/foobar/testdata2.txt", 'noway/testdata2.txt'); $tar->addData('another/testdata3.txt', 'testcontent3'); @@ -332,11 +332,12 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase public function test_createlongfile() { $tar = new Tar(); + $tar->setCompression(0); $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $path = '0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789.txt'; - $tar->create($tmp, Tar::COMPRESS_NONE); + $tar->create($tmp); $tar->addData($path, 'testcontent1'); $tar->close(); @@ -354,6 +355,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase public function test_createlongpathustar() { $tar = new Tar(); + $tar->setCompression(0); $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $path = ''; @@ -362,7 +364,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase } $path = rtrim($path, '/'); - $tar->create($tmp, Tar::COMPRESS_NONE); + $tar->create($tmp); $tar->addData("$path/test.txt", 'testcontent1'); $tar->close(); @@ -382,6 +384,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase public function test_createlongpathgnu() { $tar = new Tar(); + $tar->setCompression(0); $tmp = tempnam(sys_get_temp_dir(), 'dwtartest'); $path = ''; @@ -390,7 +393,7 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase } $path = rtrim($path, '/'); - $tar->create($tmp, Tar::COMPRESS_NONE); + $tar->create($tmp); $tar->addData("$path/test.txt", 'testcontent1'); $tar->close(); @@ -437,9 +440,10 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase { $dir = dirname(__FILE__).'/tar'; $tar = new Tar(); + $tar->setCompression(0); $tar->create(); $tar->addFile("$dir/zero.txt", 'zero.txt'); - $file = $tar->getArchive(Tar::COMPRESS_NONE); + $file = $tar->getArchive(); $this->assertEquals(512 * 3, strlen($file)); // 1 header block + 2 footer blocks } @@ -447,9 +451,10 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase public function test_zerodata() { $tar = new Tar(); + $tar->setCompression(0); $tar->create(); $tar->addData('zero.txt', ''); - $file = $tar->getArchive(Tar::COMPRESS_NONE); + $file = $tar->getArchive(); $this->assertEquals(512 * 3, strlen($file)); // 1 header block + 2 footer blocks } @@ -461,9 +466,10 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase { $dir = dirname(__FILE__).'/tar'; $tar = new Tar(); + $tar->setCompression(0); $tar->create(); $tar->addFile("$dir/block.txt", 'block.txt'); - $file = $tar->getArchive(Tar::COMPRESS_NONE); + $file = $tar->getArchive(); $this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks } @@ -471,9 +477,10 @@ class Tar_TestCase extends PHPUnit_Framework_TestCase public function test_blockdata() { $tar = new Tar(); + $tar->setCompression(0); $tar->create(); $tar->addData('block.txt', str_pad('', 512, 'x')); - $file = $tar->getArchive(Tar::COMPRESS_NONE); + $file = $tar->getArchive(); $this->assertEquals(512 * 4, strlen($file)); // 1 header block + data block + 2 footer blocks }