diff --git a/_test/tests/inc/io_getSizeFile.test.php b/_test/tests/inc/io_getSizeFile.test.php new file mode 100644 index 0000000000000000000000000000000000000000..3456342ba5d6b17a11e265479a6cbfcac022705d --- /dev/null +++ b/_test/tests/inc/io_getSizeFile.test.php @@ -0,0 +1,62 @@ +<?php + +class io_getSizeFile_test extends DokuWikiTest { + + /* + * dependency for tests needing zlib extension to pass + */ + public function test_ext_zlib() { + if (!DOKU_HAS_GZIP) { + $this->markTestSkipped('skipping all zlib tests. Need zlib extension'); + } + } + + /* + * dependency for tests needing zlib extension to pass + */ + public function test_ext_bz2() { + if (!DOKU_HAS_BZIP) { + $this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension'); + } + } + + function test_plain(){ + // since git converts line endings, we can't check in this test file but have to create it ourselves + $plain = TMP_DIR.'/test.txt'; + file_put_contents($plain, "The\015\012Test\015\012"); + + $this->assertEquals(11, io_getSizeFile($plain)); + $this->assertEquals(0, io_getSizeFile(__DIR__.'/io_readfile/nope.txt')); + $plain_mb = TMP_DIR.'/test.txt'; + io_saveFile($plain_mb, "string with utf-8 chars åèö - doo-bee doo-bee dooh\012"); + $this->assertEquals(54, io_getSizeFile($plain_mb)); + } + + /** + * @depends test_ext_zlib + */ + function test_gzfiles(){ + $this->assertEquals(11, io_getSizeFile(__DIR__.'/io_readfile/test.txt.gz')); + $this->assertEquals(0, io_getSizeFile(__DIR__.'/io_readfile/nope.txt.gz')); + $this->assertEquals(11, io_getSizeFile(__DIR__.'/io_readfile/corrupt.txt.gz')); + $gz_mb = TMP_DIR.'/test.txt.gz'; + io_saveFile($gz_mb, "string with utf-8 chars åèö - doo-bee doo-bee dooh\012"); + $this->assertEquals(54, io_getSizeFile($gz_mb)); + } + + /** + * @depends test_ext_bz2 + */ + function test_bzfiles(){ + + $this->assertEquals(11, io_getSizeFile(__DIR__.'/io_readfile/test.txt.bz2')); + $this->assertEquals(0, io_getSizeFile(__DIR__.'/io_readfile/nope.txt.bz2')); + $this->assertEquals(0, io_getSizeFile(__DIR__.'/io_readfile/corrupt.txt.bz2')); + $this->assertEquals(9720, io_getSizeFile(__DIR__.'/io_readfile/large.txt.bz2')); + $this->assertEquals(17780, io_getSizeFile(__DIR__.'/io_readfile/long.txt.bz2')); + $bz_mb = TMP_DIR.'/test.txt.bz2'; + io_saveFile($bz_mb, "string with utf-8 chars åèö - doo-bee doo-bee dooh\012"); + $this->assertEquals(54, io_getSizeFile($bz_mb)); + } + +} diff --git a/inc/io.php b/inc/io.php index 066030c89a5dc339bb62861df7f23cab17c7df5f..bb0d557cde9e9d5de45f3c997324de7725e2d6f8 100644 --- a/inc/io.php +++ b/inc/io.php @@ -776,3 +776,52 @@ function io_grep($file,$pattern,$max=0,$backref=false){ return $matches; } + +/** + * Get size of contents of a file, for a compressed file the uncompressed size + * Warning: reading uncompressed size of content of bz-files requires uncompressing + * + * @author Gerrit Uitslag <klapinklapin@gmail.com> + * + * @param string $file filename path to file + * @return int size of file + */ +function io_getSizeFile($file) { + if (!file_exists($file)) return 0; + + if(substr($file,-3) == '.gz'){ + if(!DOKU_HAS_GZIP) return 0; + + $fp = @fopen($file, "rb"); + if($fp === false) return 0; + + fseek($fp, -4, SEEK_END); + $buffer = fread($fp, 4); + fclose($fp); + $array = unpack("V", $buffer); + $uncompressedsize = end($array); + }else if(substr($file,-4) == '.bz2'){ + if(!DOKU_HAS_BZIP) return 0; + + $bz = bzopen($file,"r"); + if($bz === false) return 0; + + $uncompressedsize = 0; + while (!feof($bz)) { + //8192 seems to be the maximum buffersize? + $buffer = bzread($bz,8192); + if(($buffer === false) || (bzerrno($bz) !== 0)) { + return 0; + } + if (UTF8_MBSTRING) { + $uncompressedsize += mb_strlen($buffer, '8bit'); + } else { + $uncompressedsize += strlen($buffer); + } + } + }else{ + $uncompressedsize = filesize($file); + } + + return $uncompressedsize; + }