Pop\Archive\Archive::compress PHP Method

compress() public method

Method to compress an archive file with Gzip or Bzip2
public compress ( string $ext = 'gz' ) : Archive
$ext string
return Archive
    public function compress($ext = 'gz')
    {
        if ($ext == 'bz') {
            $ext .= '2';
        }
        switch ($ext) {
            case 'gz':
                $newArchive = Compress\Gzip::compress($this->fullpath);
                break;
            case 'tgz':
                $tmpArchive = Compress\Gzip::compress($this->fullpath);
                $newArchive = str_replace('.tar.gz', '.tgz', $tmpArchive);
                rename($tmpArchive, $newArchive);
                break;
            case 'bz2':
                $newArchive = Compress\Bzip2::compress($this->fullpath);
                break;
            case 'tbz':
                $tmpArchive = Compress\Bzip2::compress($this->fullpath);
                $newArchive = str_replace('.tar.bz2', '.tbz', $tmpArchive);
                rename($tmpArchive, $newArchive);
                break;
            case 'tbz2':
                $tmpArchive = Compress\Bzip2::compress($this->fullpath);
                $newArchive = str_replace('.tar.bz2', '.tbz2', $tmpArchive);
                rename($tmpArchive, $newArchive);
                break;
            default:
                $newArchive = $this->fullpath;
        }
        if (file_exists($this->fullpath)) {
            unlink($this->fullpath);
        }
        self::__construct($newArchive);
        return $this;
    }

Usage Example

Example #1
0
<?php

require_once '../../bootstrap.php';
use Pop\Archive\Archive;
try {
    // Create a new TAR archive and add some files to it
    // (Make sure the '../tmp' folder is writable)
    $archive = new Archive('../tmp/test.tar');
    $archive->addFiles('../assets');
    // Display the new archive file size
    echo $archive->getBasename() . ': file size => ' . $archive->getSize() . '<br /> ' . PHP_EOL;
    // Compress the archive (Gzip by default)
    $archive->compress('bz');
    // Display the newly compressed archive file size
    echo $archive->getBasename() . ': compressed file size => ' . $archive->getSize() . '<br /> ' . PHP_EOL;
    echo 'Done.';
} catch (\Exception $e) {
    echo $e->getMessage() . PHP_EOL . PHP_EOL;
}
All Usage Examples Of Pop\Archive\Archive::compress