Skip to content

Commit

Permalink
Deleting lines works with BZ2 files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrown committed May 7, 2015
1 parent 3690758 commit cfb71e3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 9 deletions.
18 changes: 13 additions & 5 deletions _test/tests/inc/io_deletefromfile.test.php
Expand Up @@ -33,10 +33,18 @@ function test_delete(){
$this->_write(TMP_DIR.'/test.txt');
}

// /**
// * @depends test_ext_zlib
// */
// function test_gzwrite(){
// }
/**
* @depends test_ext_zlib
*/
function test_gzwrite(){
$this->_write(TMP_DIR.'/test.txt.gz');
}

/**
* @depends test_ext_bz2
*/
function test_bzwrite(){
$this->_write(TMP_DIR.'/test.txt.bz2');
}

}
7 changes: 6 additions & 1 deletion _test/tests/inc/io_readfile.test.php
Expand Up @@ -48,6 +48,11 @@ function test_bzfiles(){
$this->assertEquals("The\015\012Test\015\012", io_readFile(__DIR__.'/io_readfile/test.txt.bz2', false));
$this->assertEquals(false, io_readFile(__DIR__.'/io_readfile/nope.txt.bz2'));
$this->assertEquals(false, io_readFile(__DIR__.'/io_readfile/corrupt.txt.bz2'));
// internal bzfile function
$this->assertEquals(array("The\015\012","Test\015\012"), bzfile(__DIR__.'/io_readfile/test.txt.bz2', true));
$this->assertEquals(array_fill(0, 120, str_repeat('a', 80)."\012"), bzfile(__DIR__.'/io_readfile/large.txt.bz2', true));
$line = str_repeat('a', 8888)."\012";
$this->assertEquals(array($line,"\012",$line,"!"), bzfile(__DIR__.'/io_readfile/long.txt.bz2', true));
}

}
}
Binary file added _test/tests/inc/io_readfile/large.txt.bz2
Binary file not shown.
Binary file added _test/tests/inc/io_readfile/long.txt.bz2
Binary file not shown.
31 changes: 28 additions & 3 deletions inc/io.php
Expand Up @@ -127,22 +127,36 @@ function io_readFile($file,$clean=true){
* @author Andreas Gohr <andi@splitbrain.org>
*
* @param string $file filename
* @return string|bool content or false on error
* @param bool $array return array of lines
* @return string|array|bool content or false on error
*/
function bzfile($file){
function bzfile($file, $array=false) {
$bz = bzopen($file,"r");
if($bz === false) return false;

if($array) $lines = array();
$str = '';
while (!feof($bz)){
while (!feof($bz)) {
//8192 seems to be the maximum buffersize?
$buffer = bzread($bz,8192);
if(($buffer === false) || (bzerrno($bz) !== 0)) {
return false;
}
$str = $str . $buffer;
if($array) {
$pos = strpos($str, "\n");
while($pos !== false) {
$lines[] = substr($str, 0, $pos+1);
$str = substr($str, $pos+1);
$pos = strpos($str, "\n");
}
}
}
bzclose($bz);
if($array) {
if($str !== '') $lines[] = $str;
return $lines;
}
return $str;
}

Expand Down Expand Up @@ -280,6 +294,8 @@ function io_deleteFromFile($file,$badline,$regex=false){
// load into array
if(substr($file,-3) == '.gz'){
$lines = gzfile($file);
}else if(substr($file,-4) == '.bz2'){
$lines = bzfile($file, true);
}else{
$lines = file($file);
}
Expand All @@ -306,6 +322,15 @@ function io_deleteFromFile($file,$badline,$regex=false){
}
gzwrite($fh, $content);
gzclose($fh);
}else if(substr($file,-4) == '.bz2'){
$fh = @bzopen($file,'w');
if(!$fh){
msg("Removing content from $file failed",-1);
io_unlock($file);
return false;
}
bzwrite($fh, $content);
bzclose($fh);
}else{
$fh = @fopen($file,'wb');
if(!$fh){
Expand Down

0 comments on commit cfb71e3

Please sign in to comment.