Skip to content
Snippets Groups Projects
Commit cfb71e37 authored by Patrick Brown's avatar Patrick Brown
Browse files

Deleting lines works with BZ2 files.

parent 36907582
No related branches found
No related tags found
No related merge requests found
......@@ -33,10 +33,18 @@ class io_deletefromfile_test extends DokuWikiTest {
$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');
}
}
......@@ -48,6 +48,11 @@ class io_readfile_test extends DokuWikiTest {
$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));
}
}
\ No newline at end of file
}
File added
File added
......@@ -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;
}
......@@ -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);
}
......@@ -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){
......
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