[lang-ref] ( file_io_with_gzip ) ( php )
<?php
public function testFileIoWithGzip(): void
{
// gzopen(), gzwrite(), gzread()
$lines = ['test', 'test', 'test'];
$text = implode("\n", $lines);
$p = $this->tmpDir . DIRECTORY_SEPARATOR . 'a.txt.gz';
$fp = gzopen($p, 'wb9');
$this->assertNotFalse($fp);
gzwrite($fp, $text);
gzclose($fp);
$fp = gzopen($p, 'rb');
$this->assertNotFalse($fp);
$r = '';
while (!gzeof($fp)) {
$chunk = gzread($fp, 8192);
if ($chunk === false) {
gzclose($fp);
$this->fail('Failed to read gzip file.');
}
$r .= $chunk;
}
gzclose($fp);
$this->assertSame($text, $r);
}
private function removeDirectory(string $dir): void
{
if (!is_dir($dir)) {
return;
}
$items = scandir($dir);
if ($items === false) {
return;
}
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
$this->removeDirectory($path);
} else {
unlink($path);
}
}
rmdir($dir);
}