Skip to content

Commit

Permalink
Merge pull request #6 from wapmorgan/master
Browse files Browse the repository at this point in the history
Add support for lzma2 compression.
  • Loading branch information
CloCkWeRX committed May 25, 2014
2 parents 4b6c906 + 1ab1063 commit 47ea588
Showing 1 changed file with 60 additions and 5 deletions.
65 changes: 60 additions & 5 deletions Archive/Tar.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class Archive_Tar extends PEAR
var $_compress=false;

/**
* @var string Type of compression : 'none', 'gz' or 'bz2'
* @var string Type of compression : 'none', 'gz', 'bz2' or 'lzma2'
*/
var $_compress_type='none';

Expand Down Expand Up @@ -103,8 +103,8 @@ class Archive_Tar extends PEAR
* gzip or bz2 compressed TAR file.
*
* @param string $p_tarname The name of the tar archive to create
* @param string $p_compress can be null, 'gz' or 'bz2'. This
* parameter indicates if gzip or bz2 compression
* @param string $p_compress can be null, 'gz', 'bz2' or 'lzma2'. This
* parameter indicates if gzip, bz2 or lzma2 compression
* is required. For compatibility reason the
* boolean value 'true' means 'gz'.
*
Expand All @@ -128,6 +128,9 @@ function Archive_Tar($p_tarname, $p_compress = null)
} elseif ($data == "BZ") {
$this->_compress = true;
$this->_compress_type = 'bz2';
} elseif (file_get_contents($p_tarname, false, null, 1, 4) == '7zXZ') {
$this->_compress = true;
$this->_compress_type = 'lzma2';
}
}
} else {
Expand All @@ -140,6 +143,9 @@ function Archive_Tar($p_tarname, $p_compress = null)
(substr($p_tarname, -2) == 'bz')) {
$this->_compress = true;
$this->_compress_type = 'bz2';
} else if (substr($p_tarname, -2) == 'xz') {
$this->_compress = true;
$this->_compress_type = 'lzma2';
}
}
} else {
Expand All @@ -149,18 +155,23 @@ function Archive_Tar($p_tarname, $p_compress = null)
} else if ($p_compress == 'bz2') {
$this->_compress = true;
$this->_compress_type = 'bz2';
} else if ($p_compress == 'lzma2') {
$this->_compress = true;
$this->_compress_type = 'lzma2';
} else {
$this->_error("Unsupported compression type '$p_compress'\n".
"Supported types are 'gz' and 'bz2'.\n");
"Supported types are 'gz', 'bz2' and 'lzma2'.\n");
return false;
}
}
$this->_tarname = $p_tarname;
if ($this->_compress) { // assert zlib or bz2 extension support
if ($this->_compress) { // assert zlib or bz2 or xz extension support
if ($this->_compress_type == 'gz')
$extname = 'zlib';
else if ($this->_compress_type == 'bz2')
$extname = 'bz2';
else if ($this->_compress_type == 'lzma2')
$extname = 'xz';

if (!extension_loaded($extname)) {
PEAR::loadExtension($extname);
Expand Down Expand Up @@ -697,6 +708,8 @@ function _openWrite()
$this->_file = @gzopen($this->_tarname, "wb9");
else if ($this->_compress_type == 'bz2' && function_exists('bzopen'))
$this->_file = @bzopen($this->_tarname, "w");
else if ($this->_compress_type == 'lzma2' && function_exists('xzopen'))
$this->_file = @xzopen($this->_tarname, 'w');
else if ($this->_compress_type == 'none')
$this->_file = @fopen($this->_tarname, "wb");
else {
Expand Down Expand Up @@ -752,6 +765,8 @@ function _openRead()
$this->_file = @gzopen($v_filename, "rb");
else if ($this->_compress_type == 'bz2' && function_exists('bzopen'))
$this->_file = @bzopen($v_filename, "r");
else if ($this->_compress_type == 'lzma2' && function_exists('xzopen'))
$this->_file = @xzopen($v_filename, "r");
else if ($this->_compress_type == 'none')
$this->_file = @fopen($v_filename, "rb");
else {
Expand All @@ -778,6 +793,10 @@ function _openReadWrite()
$this->_error('Unable to open bz2 in read/write mode \''
.$this->_tarname.'\' (limitation of bz2 extension)');
return false;
} else if ($this->_compress_type == 'lzma2') {
$this->_error('Unable to open lzma2 in read/write mode \''
.$this->_tarname.'\' (limitation of lzma2 extension)');
return false;
} else if ($this->_compress_type == 'none')
$this->_file = @fopen($this->_tarname, "r+b");
else {
Expand Down Expand Up @@ -805,6 +824,8 @@ function _close()
@gzclose($this->_file);
else if ($this->_compress_type == 'bz2')
@bzclose($this->_file);
else if ($this->_compress_type == 'lzma2')
@xzclose($this->_file);
else if ($this->_compress_type == 'none')
@fclose($this->_file);
else
Expand Down Expand Up @@ -854,6 +875,8 @@ function _writeBlock($p_binary_data, $p_len=null)
@gzputs($this->_file, $p_binary_data);
else if ($this->_compress_type == 'bz2')
@bzwrite($this->_file, $p_binary_data);
else if ($this->_compress_type == 'lzma2')
@xzwrite($this->_file, $p_binary_data);
else if ($this->_compress_type == 'none')
@fputs($this->_file, $p_binary_data);
else
Expand All @@ -864,6 +887,8 @@ function _writeBlock($p_binary_data, $p_len=null)
@gzputs($this->_file, $p_binary_data, $p_len);
else if ($this->_compress_type == 'bz2')
@bzwrite($this->_file, $p_binary_data, $p_len);
else if ($this->_compress_type == 'lzma2')
@xzwrite($this->_file, $p_binary_data, $p_len);
else if ($this->_compress_type == 'none')
@fputs($this->_file, $p_binary_data, $p_len);
else
Expand All @@ -885,6 +910,8 @@ function _readBlock()
$v_block = @gzread($this->_file, 512);
else if ($this->_compress_type == 'bz2')
$v_block = @bzread($this->_file, 512);
else if ($this->_compress_type == 'lzma2')
$v_block = @xzread($this->_file, 512);
else if ($this->_compress_type == 'none')
$v_block = @fread($this->_file, 512);
else
Expand All @@ -909,6 +936,10 @@ function _jumpBlock($p_len=null)
// ----- Replace missing bztell() and bzseek()
for ($i=0; $i<$p_len; $i++)
$this->_readBlock();
} else if ($this->_compress_type == 'lzma2') {
// ----- Replace missing xztell() and xzseek()
for ($i=0; $i<$p_len; $i++)
$this->_readBlock();
} else if ($this->_compress_type == 'none')
@fseek($this->_file, $p_len*512, SEEK_CUR);
else
Expand Down Expand Up @@ -1812,6 +1843,9 @@ function _openAppend()
$v_temp_tar = @gzopen($this->_tarname.".tmp", "rb");
elseif ($this->_compress_type == 'bz2')
$v_temp_tar = @bzopen($this->_tarname.".tmp", "r");
elseif ($this->_compress_type == 'lzma2')
$v_temp_tar = @xzopen($this->_tarname.".tmp", "r");


if ($v_temp_tar == 0) {
$this->_error('Unable to open file \''.$this->_tarname
Expand Down Expand Up @@ -1868,6 +1902,27 @@ function _openAppend()

@bzclose($v_temp_tar);
}
elseif ($this->_compress_type == 'lzma2') {
$end_blocks = 0;

while (strlen($v_buffer = @xzread($v_temp_tar, 512)) > 0) {
if ($v_buffer == ARCHIVE_TAR_END_BLOCK || strlen($v_buffer) == 0) {
$end_blocks++;
// do not copy end blocks, we will re-make them
// after appending
continue;
} elseif ($end_blocks > 0) {
for ($i = 0; $i < $end_blocks; $i++) {
$this->_writeBlock(ARCHIVE_TAR_END_BLOCK);
}
$end_blocks = 0;
}
$v_binary_data = pack("a512", $v_buffer);
$this->_writeBlock($v_binary_data);
}

@xzclose($v_temp_tar);
}

if (!@unlink($this->_tarname.".tmp")) {
$this->_error('Error while deleting temporary file \''
Expand Down

0 comments on commit 47ea588

Please sign in to comment.