1 backup.class.inc | public Backup::compress(BackupFile $file, $format = self::COMPRESSION_GZIP, $delete_original = FALSE) |
Compress a backup file with gzip.
Return value
bool|BackupFile: The newly created compressed file as BackupFile object. FALSE if failure.
File
- core/
includes/ backup/ backup.class.inc, line 276 - Contains the Backup base class.
Class
- Backup
- Base class for creating backups.
Code
public function compress(BackupFile $file, $format = self::COMPRESSION_GZIP, $delete_original = FALSE) {
// Only gzip compression supported currently.
if ($format !== self::COMPRESSION_GZIP) {
return FALSE;
}
// If the file is already compressed, nothing to do here.
if ($file->lastExtension() === 'gz') {
return $file;
}
$success = FALSE;
$compressed_file = clone $file;
$compressed_file->pushExtension('gz');
$source = $file->filePath();
$destination = $compressed_file->filePath();
$level = 9;
if (function_exists('gzopen')) {
$fp_out = gzopen($destination, 'wb' . $level);
$fp_in = fopen($source, 'rb');
if ($fp_out && $fp_in) {
while (!feof($fp_in)) {
gzwrite($fp_out, fread($fp_in, 1024 * 512));
}
$success = TRUE;
}
@fclose($fp_in);
@gzclose($fp_out);
}
if ($success && $delete_original) {
file_unmanaged_delete($source);
}
return $success ? $compressed_file : FALSE;
}