Pop\Compress\Bzip2::compress PHP Method

compress() public static method

Static method to compress data
public static compress ( string $data, integer $block = 9 ) : mixed
$data string
$block integer
return mixed
    public static function compress($data, $block = 9)
    {
        // Compress the file
        if (file_exists($data)) {
            $fullpath = realpath($data);
            $data = file_get_contents($data);
            // Create the new Bzip2 file resource, write data and close it
            $bzResource = bzopen($fullpath . '.bz2', 'w');
            bzwrite($bzResource, $data, strlen($data));
            bzclose($bzResource);
            return $fullpath . '.bz2';
            // Else, compress the string
        } else {
            return bzcompress($data, $block);
        }
    }

Usage Example

Esempio n. 1
0
 public function testBzip2WithTarFile()
 {
     $tar = false;
     $includePath = explode(PATH_SEPARATOR, get_include_path());
     foreach ($includePath as $path) {
         if (file_exists($path . DIRECTORY_SEPARATOR . 'Archive' . DIRECTORY_SEPARATOR . 'Tar.php')) {
             $tar = true;
         }
     }
     if ($tar && function_exists('bzopen')) {
         $a = new Archive(__DIR__ . '/../tmp/test.tar');
         $a->addFiles(__DIR__ . '/../tmp');
         $compressed = Bzip2::compress(__DIR__ . '/../tmp/test.tar');
         copy($compressed, __DIR__ . '/../tmp/test.tbz2');
         copy($compressed, __DIR__ . '/../tmp/test.tbz');
         $this->fileExists(__DIR__ . '/../tmp/test.tar');
         $this->fileExists(__DIR__ . '/../tmp/test.tar.bz2');
         $this->fileExists(__DIR__ . '/../tmp/test.tbz2');
         $this->fileExists(__DIR__ . '/../tmp/test.tbz');
         if (file_exists(__DIR__ . '/../tmp/test.tar')) {
             unlink(__DIR__ . '/../tmp/test.tar');
         }
         // Test *.tar.bz2
         $decompressed = Bzip2::decompress(__DIR__ . '/../tmp/test.tar.bz2');
         $this->fileExists(__DIR__ . '/../tmp/test.tar');
         if (file_exists(__DIR__ . '/../tmp/test.tar')) {
             unlink(__DIR__ . '/../tmp/test.tar');
         }
         if (file_exists(__DIR__ . '/../tmp/test.tar.bz2')) {
             unlink(__DIR__ . '/../tmp/test.tar.bz2');
         }
         // Test *.tbz2
         $decompressed = Bzip2::decompress(__DIR__ . '/../tmp/test.tbz2');
         $this->fileExists(__DIR__ . '/../tmp/test.tar');
         if (file_exists(__DIR__ . '/../tmp/test.tar')) {
             unlink(__DIR__ . '/../tmp/test.tar');
         }
         if (file_exists(__DIR__ . '/../tmp/test.tbz2')) {
             unlink(__DIR__ . '/../tmp/test.tbz2');
         }
         // Test *.tbz
         $decompressed = Bzip2::decompress(__DIR__ . '/../tmp/test.tbz');
         $this->fileExists(__DIR__ . '/../tmp/test.tar');
         if (file_exists(__DIR__ . '/../tmp/test.tar')) {
             unlink(__DIR__ . '/../tmp/test.tar');
         }
         if (file_exists(__DIR__ . '/../tmp/test.tbz')) {
             unlink(__DIR__ . '/../tmp/test.tbz');
         }
     }
 }
All Usage Examples Of Pop\Compress\Bzip2::compress