Skip to content
Snippets Groups Projects
Commit f549be3d authored by Gerrit Uitslag's avatar Gerrit Uitslag
Browse files

io_getSizeFile returns uncompressed size of given file

A bz2-file doesn't contain information about the size of its
uncompressed content. Therefore it requires reading the whole file to
obtain the filesize.
parent 655ddc1d
No related branches found
No related tags found
No related merge requests found
<?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));
}
}
......@@ -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;
}
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