diff --git a/_test/tests/inc/io_deletefromfile.test.php b/_test/tests/inc/io_deletefromfile.test.php
new file mode 100644
index 0000000000000000000000000000000000000000..361c82214e9a3979223a871b01eb9466ca78d9c6
--- /dev/null
+++ b/_test/tests/inc/io_deletefromfile.test.php
@@ -0,0 +1,42 @@
+<?php
+
+class io_deletefromfile_test extends DokuWikiTest {
+
+    /*
+     * dependency for tests needing zlib extension to pass
+     */
+    public function test_ext_zlib() {
+        if (!extension_loaded('zlib')) {
+            $this->markTestSkipped('skipping all zlib tests.  Need zlib extension');
+        }
+    }
+
+    /*
+     * dependency for tests needing zlib extension to pass
+     */
+    public function test_ext_bz2() {
+        if (!extension_loaded('bz2')) {
+            $this->markTestSkipped('skipping all bzip2 tests.  Need bz2 extension');
+        }
+    }
+
+    function _write($file){
+        $contents = "The\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012";
+        io_saveFile($file, $contents);
+        $this->assertTrue(io_deleteFromFile($file, "Delete\012"));
+        $this->assertEquals("The\012Delete01\012Delete02\012DeleteX\012Test\012", io_readFile($file));
+        $this->assertTrue(io_deleteFromFile($file, "#Delete\\d+\012#", true));
+        $this->assertEquals("The\012DeleteX\012Test\012", io_readFile($file));
+    }
+
+    function test_delete(){
+        $this->_write(TMP_DIR.'/test.txt');
+    }
+
+//    /**
+//     * @depends test_ext_zlib
+//     */
+//    function test_gzwrite(){
+//    }
+
+}
diff --git a/_test/tests/inc/io_savefile.test.php b/_test/tests/inc/io_savefile.test.php
new file mode 100644
index 0000000000000000000000000000000000000000..4a4d4671d2d750b8197ba83a30e5164db1bf2c51
--- /dev/null
+++ b/_test/tests/inc/io_savefile.test.php
@@ -0,0 +1,49 @@
+<?php
+
+class io_savefile_test extends DokuWikiTest {
+
+    /*
+     * dependency for tests needing zlib extension to pass
+     */
+    public function test_ext_zlib() {
+        if (!extension_loaded('zlib')) {
+            $this->markTestSkipped('skipping all zlib tests.  Need zlib extension');
+        }
+    }
+
+    /*
+     * dependency for tests needing zlib extension to pass
+     */
+    public function test_ext_bz2() {
+        if (!extension_loaded('bz2')) {
+            $this->markTestSkipped('skipping all bzip2 tests.  Need bz2 extension');
+        }
+    }
+
+    function _write($file){
+        $contents = "The\012Write\012Test\012";
+        $this->assertTrue(io_saveFile($file, $contents));
+        $this->assertEquals($contents, io_readFile($file));
+        $this->assertTrue(io_saveFile($file, $contents, true));
+        $this->assertEquals($contents.$contents, io_readFile($file));
+    }
+
+    function test_write(){
+        $this->_write(TMP_DIR.'/test.txt');
+    }
+
+    /**
+     * @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');
+    }
+
+}
diff --git a/inc/io.php b/inc/io.php
index 0636a4b62503c76a7fd76b15f458acdcdc5e1f7d..8846b7e56b1cf90f2ebd2e98374eeadf06942859 100644
--- a/inc/io.php
+++ b/inc/io.php
@@ -223,7 +223,16 @@ function io_saveFile($file,$content,$append=false){
         gzwrite($fh, $content);
         gzclose($fh);
     }else if(substr($file,-4) == '.bz2'){
-        $fh = @bzopen($file,$mode{0});
+        if($append) {
+            $bzcontent = bzfile($file);
+            if($bzcontent === false) {
+                msg("Writing $file failed", -1);
+                io_unlock($file);
+                return false;
+            }
+            $content = $bzcontent.$content;
+        }
+        $fh = @bzopen($file,'w');
         if(!$fh){
             msg("Writing $file failed", -1);
             io_unlock($file);