/** * @expectedException \Brainbits\Transcoder\Exception\DecodeFailedException */ public function testDecodeThrowsErrorOnEmptyResult() { $testString = ''; $encodedString = bzcompress($testString); $result = $this->decoder->decode($encodedString); $this->assertSame($testString, $result); }
/** * Pack file by BZIP2 compressor. * * @param string $source * @param string $destination * @return string */ public function pack($source, $destination) { $data = $this->_readFile($source); $bzData = bzcompress($data, 9); $this->_writeFile($destination, $bzData); return $destination; }
public function create($paths, $filename = FALSE) { $archive = new Archive('tar'); foreach ($paths as $set) { $archive->add($set[0], $set[1]); } $gzfile = bzcompress($archive->create()); if ($filename == FALSE) { return $gzfile; } if (substr($filename, -8) !== '.tar.bz2') { // Append tar extension $filename .= '.tar.bz2'; } // Create the file in binary write mode $file = fopen($filename, 'wb'); // Lock the file flock($file, LOCK_EX); // Write the tar file $return = fwrite($file, $gzfile); // Unlock the file flock($file, LOCK_UN); // Close the file fclose($file); return (bool) $return; }
public function render($maxUrls = NULL, $maxMegabytes = NULL, $compression = NULL) { if ($maxUrls && !count($this) > $maxUrls) { throw new OutOfBoundsException('Sitemap has more than ' . $maxUrls . ' URLs and needs to be spitted'); } $tree = new XML_Node('urlset'); $tree->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); foreach ($this->urls as $url) { $node = new XML_Node('url'); $node->addChild(new XML_Node('loc', $url->getLocation())); if ($datetime = $url->getDatetime()) { $node->addChild(new XML_Node('lastmod', $datetime)); } $tree->addChild($node); } $builder = new XML_Builder(); $xml = $builder->build($tree); if ($compression) { if (!in_array($compression, $this->compressions)) { throw new OutOfRangeException('Available compression types are ' . join(", ", $this->compressions)); } switch ($compression) { case 'bz': $xml = bzcompress($xml); break; case 'gz': $xml = gzencode($xml); break; } } if ($maxMegabytes && strlen($xml) > $maxMegabytes * 1024 * 1024) { throw new OutOfBoundsException('Rendered sitemap is to large (max: ' . $maxMegabytes . ' MB)'); } return $xml; }
function server() { list($dr, $nod) = split_right('/', $_GET['table'], 1); $main = msql_read($dr, $nod, ''); //p($main); if ($main) { $dscrp = flux_xml($main); } $host = $_SERVER['HTTP_HOST']; //$dscrp=str_replace('users/','http://'.$host.'/users/',$dscrp); //$dscrp=str_replace('img/','http://'.$host.'/img/',$dscrp); $xml = '<' . '?xml version="1.0" encoding="utf-8" ?' . '>' . "\n"; //iso-8859-1// $xml .= '<rss version="2.0">' . "\n"; $xml .= '<channel>' . "\n"; $xml .= '<title>http://' . $host . '/msql/' . $_GET['table'] . '</title>' . "\n"; $xml .= '<link>http://' . $host . '/</link>' . "\n"; $xml .= '<description>' . count($main) . ' entries</description>' . "\n"; $xml .= $dscrp; $xml .= '</channel>' . "\n"; $xml .= '</rss>' . "\n"; //$xml.='</xml>'."\n"; if ($_GET['bz2']) { return bzcompress($xml); } if ($_GET["b64"]) { return base64_encode($xml); } return utf8_encode($xml); }
public function insertPageForMenuItem($item) { $faker = Faker\Factory::create(); $this->insert('page', ['title' => $item['title'], 'path' => $item['path'], 'status' => TRUE, 'body' => base64_encode(bzcompress($faker->text)), 'type' => PageTypes::PAGE_TYPE]); $pageId = $this->getAdapter()->getConnection()->lastInsertId(); return $pageId; }
/** * Encode a string from the values that have been generated * * @param text string The string to be encoded * * @return string */ public static function encrypt($text) { $config = Config::settings('encrypt'); $base64key = $config['key']; $base64ivector = $config['iv']; return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, base64_decode($base64key), bzcompress(trim($text)), MCRYPT_MODE_CBC, base64_decode($base64ivector))); }
function write_to_file($filename, $stream_id, $content) { global $GLOBAL_CRON; $file_ext = substr($filename, strrpos($filename, '.') + 1); switch ($file_ext) { case 'gz': $string = gzencode($content, 9); break; case 'bz2': $string = bzcompress($content, 9); break; default: $string = $content; break; } flush(); if (!$GLOBAL_CRON) { echo $string; } else { echo "file " . $filename . " " . strlen($string) . "\n"; } if ($stream_id) { return fwrite($stream_id, $string); } }
/** * @inheritDoc */ public function encode($data) { $data = bzcompress($data, 9); if (!$data) { throw new EncodeFailedException("bzcompress returned no data."); } return $data; }
/** * {@inheritdoc} */ public function compress($data, $length) { $output = @bzcompress(substr($data, 0, $length)); if (!is_string($output)) { throw new InvalidInputDataException('The compression input data is invalid.', $output); } return $output; }
/** // You can use this class like that. $test = new bzip2; $test->makeBzip2('./','./toto.bzip2'); var_export($test->infosBzip2('./toto.bzip2')); $test->extractBzip2('./toto.bzip2', './new/'); **/ function makeBzip2($src, $dest = false) { $Bzip2 = bzcompress(strpos(chr(0), $src) ? file_get_contents($src) : $src, 6); if (empty($dest)) { return $Bzip2; } elseif (file_put_contents($dest, $Bzip2)) { return $dest; } return false; }
public function compress($data = '', $blockSize = 4, $workFactor = 0) { if (!is_scalar($data)) { return Error::set('Error', 'valueParameter', '1.(data)'); } if (!is_numeric($blockSize) || !is_numeric($workFactor)) { return Error::set('Error', 'numericParameter', '2.(blockSize) & 3.(workFactor)'); } return bzcompress($data, $blockSize, $workFactor); }
function compress($str, $lvl) { global $compression_handler; switch ($compression_handler) { case 'no': return $str; case 'bzip2': return bzcompress($str, $lvl); default: return gzcompress($str, $lvl); } }
function CacheMakeEncode($cacheFileName = null, $cacheContent = null, $cacheDirectory = null) { $content = base64_encode(bzcompress(serialize($cacheContent), 9)); $cont = "<?php"; $cont .= "\r\n"; $cont .= "\$content='" . $content . "';"; $cont .= "?>"; $fpindex = @fopen($cacheDirectory . $cacheFileName, "w+"); $fw = @fwrite($fpindex, $cont); @fclose($fpindex); return $fw; }
/** * Export data to a download file. * * BROWSER BUG: * IE has problems with the use of attachment; needs atachment (someone at MS can't spell) or none. * however ns under version 6 accepts this also. * NS 6+ has problems with the absense of attachment; and the misspelling of attachment; * at present ie 5 on mac gives wrong filename and NS 6+ gives wrong filename. * * @todo Currently supports only csv/excel mimetypes. * * @param string $data The content * @param string $fileName Filename for the download * @param string $type The type (csv / excel / xml) * @param string $ext Extension of the file * @param string $compression Compression method (bzip / gzip) */ public function export($data, $fileName, $type, $ext = '', $compression = '') { ob_end_clean(); if ($compression == 'bzip') { $mime_type = 'application/x-bzip'; $ext = 'bz2'; } elseif ($compression == 'gzip') { $mime_type = 'application/x-gzip'; $ext = 'gz'; } elseif ($type == 'csv') { $mime_type = 'text/x-csv'; $ext = 'csv'; } elseif ($type == 'excel') { $mime_type = 'application/octet-stream'; $ext = 'xls'; } elseif ($type == 'xml') { $mime_type = 'text/xml'; $ext = 'xml'; } else { $mime_type = 'application/octet-stream'; } header('Content-Type: ' . $mime_type); header('Content-Disposition: filename="' . $fileName . '.' . $ext . '"'); // Fix for downloading (Office) documents using an SSL connection in // combination with MSIE. if (($_SERVER['SERVER_PORT'] == '443' || Tools::atkArrayNvl($_SERVER, 'HTTP_X_FORWARDED_PROTO') == 'https') && preg_match('/msie/i', $_SERVER['HTTP_USER_AGENT'])) { header('Pragma: public'); } else { header('Pragma: no-cache'); } header('Expires: 0'); // 1. as a bzipped file if ($compression == 'bzip') { if (@function_exists('bzcompress')) { echo bzcompress($data); } } else { if ($compression == 'gzip') { if (@function_exists('gzencode')) { // without the optional parameter level because it bug echo gzencode($data); } } else { if ($type == 'csv' || $type == 'excel') { // in order to output UTF-8 content that Excel both on Windows and OS X will be able to successfully read echo mb_convert_encoding($data, 'Windows-1252', 'UTF-8'); } } } flush(); exit; }
function execute(array $params = array()) { $input = (string) $params['input']; $size = $params['size']; if (!is_int($size)) { $size = 4; } $factor = $params['factor']; if (!is_int($factor)) { $factor = 0; } return bzcompress($input, $size, $factor); }
/** * Compress the given value to the specific compression * * @param String $sValue * @param String $iLevel (Optionnal) : Between 0 and 9 * * @return String * * @throws Exception */ public function compress($sValue, $iLevel = null) { if (!is_string($sValue)) { throw new Exception('Invalid first argument, must be a string'); } if (isset($iLevel) && !is_int($iLevel)) { throw new Exception('Invalid second argument, must be an int'); } if ($iValue < 0 || $iValue > 9) { throw new Exception('Invalid second argument, must be between 0 and 9'); } return bzcompress($sValue, $iLevel); }
function errData($id) { db_con("cubit"); $sql = "SELECT errtime::date AS errtime, errdata\n\t\t\tFROM errordumps WHERE id='{$_GET['id']}'"; $rslt = db_exec($sql) or die("Unable to stream error dump"); if (pg_num_rows($rslt) < 1) { $data = array("errtime" => date("Y-m-d"), "errdata" => base64_encode("invalid error id.")); } else { $data = pg_fetch_array($rslt); } $data["errdata"] = bzcompress(base64_decode($data["errdata"]), 9); return $data; }
/** * {@inheritdoc} */ public function setUp() { $this->original = 'Hello, World!'; // @TODO vfsStream does not appear to work with gzopen. see: https://github.com/mikey179/vfsStream/issues/3 $this->filedir = '/tmp/bmtest'; $this->cleanUpFiles($this->filedir); mkdir($this->filedir); file_put_contents($this->filedir . '/item1.txt', $this->original); file_put_contents($this->filedir . '/item2.txt.gz', gzencode($this->original)); file_put_contents($this->filedir . '/item3.txt.bz2', bzcompress($this->original)); // Create a tmp manager. $this->manager = new TempFileManager(new TempFileAdapter('/tmp')); $this->filter = new CompressionFilter(['compression' => 'gzip']); $this->filter->setTempFileManager($this->manager); }
/** * Static method to compress data * * @param string $data * @param int $block * @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); } }
function git_snapshot($projectroot, $project, $hash) { global $gitphp_conf, $tpl; if (!isset($hash)) { $hash = "HEAD"; } $cachekey = sha1($project) . "|" . $hash; $bzcompress = false; $gzencode = false; $rname = str_replace(array("/", ".git"), array("-", ""), $project); /*if ($gitphp_conf['compressformat'] == GITPHP_COMPRESS_ZIP) { header("Content-Type: application/x-zip"); header("Content-Disposition: attachment; filename=" . $rname . ".zip"); } else*/ if ($gitphp_conf['compressformat'] == GITPHP_COMPRESS_BZ2 && function_exists("bzcompress")) { $bzcompress = true; header("Content-Type: application/x-bzip2"); header("Content-Disposition: attachment; filename=" . $rname . ".tar.bz2"); } else { if ($gitphp_conf['compressformat'] == GITPHP_COMPRESS_GZ && function_exists("gzencode")) { $gzencode = true; header("Content-Type: application/x-gzip"); header("Content-Disposition: attachment; filename=" . $rname . ".tar.gz"); } else { header("Content-Type: application/x-tar"); header("Content-Disposition: attachment; filename=" . $rname . ".tar"); } } $git = new Git($projectroot . $project); if (!$tpl->is_cached('snapshot.tpl', $cachekey)) { $arc = git_archive($git, $hash, $rname); if ($gitphp_conf['compressformat'] == GITPHP_COMPRESS_BZ2 && $bzcompress) { $arc = bzcompress($arc, isset($gitphp_conf['compresslevel']) ? $gitphp_conf['compresslevel'] : 4); } else { if ($gitphp_conf['compressformat'] == GITPHP_COMPRESS_GZ && $gzencode) { $arc = gzencode($arc, isset($gitphp_conf['compresslevel']) ? $gitphp_conf['compresslevel'] : -1); } } $tpl->assign("archive", $arc); } $tpl->display('snapshot.tpl', $cachekey); }
function body() { $body = chr($this->algorithm); switch ($this->algorithm) { case 0: $body .= $this->data->to_bytes(); break; case 1: $body .= gzdeflate($this->data->to_bytes()); break; case 2: $body .= gzcompress($this->data->to_bytes()); break; case 3: $body .= bzcompress($this->data->to_bytes()); break; default: throw new Exception("Bad value for Compression Algorithm (compress)"); } return $body; }
private function zl_compress($data, $level = "", $type = "") { if ($type != "g" && $type != "b" && $type != "n") { // Darnit, they forgot to set the type. Assuming gZip if any compression if ($level >= 1 && $level <= 9) { $type = "g"; } elseif ($level > 9) { die("Compression level set too high"); } else { $type = "n"; } } if ($type == "g") { $this->compr_lvl_last = 8; return gzdeflate($data, $level); } elseif ($type == "b") { $this->compr_lvl_last = 12; return bzcompress($data, $level); } else { $this->compr_lvl_last = 0; return $data; } }
<?php $string = "Life it seems, will fade away\nDrifting further everyday\nGetting lost within myself\nNothing matters no one else"; var_dump(bzcompress()); var_dump(bzcompress(1, 1, 1)); var_dump(bzcompress($string, 100)); var_dump(bzcompress($string, 100, -1)); var_dump(bzcompress($string, 100, 1000)); var_dump(bzcompress($string, -1, 1)); $data = bzcompress($string); $data2 = bzcompress($string, 1, 10); $data3 = $data2; $data3[3] = 0; var_dump(bzdecompress()); var_dump(bzdecompress(1, 1, 1)); var_dump(bzdecompress(1, 1)); var_dump(bzdecompress($data3)); var_dump(bzdecompress($data3, 1)); var_dump(bzdecompress($data, -1)); var_dump(bzdecompress($data, 0)); var_dump(bzdecompress($data, 1000)); var_dump(bzdecompress($data)); var_dump(bzdecompress($data2)); echo "Done\n";
/** * Verify that contents can be compressed using bzip2. */ public function testSetAPathUsingBzip2Compression() { $this->manager->setCompression(Database::BZIP2); $this->manager->setPath($this->values['path'], new Memory($this->values['contents'], $this->values['type'], $this->values['modified'], $this->values['permissions'])); $query = $this->pdo->query("SELECT * FROM paths WHERE path = '{$this->values['path']}'"); $records = $query->fetchAll(); $query->closeCursor(); self::assertEquals(array_merge($this->values, ['compression' => Database::BZIP2, 'contents' => bzcompress($this->values['contents'])]), $records[0], 'The contents were not compressed using bzip2.'); $path = $this->manager->getPath($this->values['path']); self::assertEquals($this->values['contents'], $path->getContents(), 'The contents were not decompressed after selection.'); }
function create_bzip() { if ($this->options['inmemory'] == 0) { $Pwd = getcwd(); chdir($this->options['basedir']); if ($fp = bzopen($this->options['name'], "wb")) { fseek($this->archive, 0); while ($temp = fread($this->archive, 1048576)) { bzwrite($fp, $temp); } bzclose($fp); chdir($Pwd); } else { $this->error[] = "Could not open {$this->options['name']} for writing."; chdir($Pwd); return 0; } } else { $this->archive = bzcompress($this->archive, $this->options['level']); } return 1; }
/** */ function write($data, $key) { if (strlen($data) < $this->_min_length) { return $data; } if (!is_null($this->_block_size)) { if (!is_null($this->_work_level)) { return bzcompress($data, $this->_block_size, $this->_work_level); } else { return bzcompress($data, $this->_block_size); } } return bzcompress($data); }
/** * Compresses the given content * * @param string $content * @return string */ public function compress($content) { $archive = $this->getArchive(); if (!empty($archive)) { $file = bzopen($archive, 'w'); if (!$file) { require_once 'Zend/Filter/Exception.php'; throw new Zend_Filter_Exception("Error opening the archive '" . $archive . "'"); } bzwrite($file, $content); bzclose($file); $compressed = true; } else { $compressed = bzcompress($content, $this->getBlocksize()); } if (is_int($compressed)) { require_once 'Zend/Filter/Exception.php'; throw new Zend_Filter_Exception('Error during compression'); } return $compressed; }
function compress(&$filename, &$filedump, $compress) { global $content_encoding; global $mime_type; if ($compress == 'bzip' && @function_exists('bzcompress')) { $filename .= '.bz2'; $mime_type = 'application/x-bzip2'; $filedump = bzcompress($filedump); } else { if ($compress == 'gzip' && @function_exists('gzencode')) { $filename .= '.gz'; $content_encoding = 'x-gzip'; $mime_type = 'application/x-gzip'; $filedump = gzencode($filedump); } else { if ($compress == 'zip' && @function_exists('gzcompress')) { $filename .= '.zip'; $mime_type = 'application/zip'; $zipfile = new zipfile(); $zipfile->addFile($filedump, substr($filename, 0, -4)); $filedump = $zipfile->file(); } else { $mime_type = 'application/octet-stream'; } } } }
/** * Output handler for all exports, if needed buffering, it stores data into * $dump_buffer, otherwise it prints thems out. * * @param string the insert statement * * @return bool Whether output suceeded */ function PMA_exportOutputHandler($line) { global $time_start, $dump_buffer, $dump_buffer_len, $save_filename; // Kanji encoding convert feature if ($GLOBALS['output_kanji_conversion']) { $line = PMA_kanji_str_conv($line, $GLOBALS['knjenc'], isset($GLOBALS['xkana']) ? $GLOBALS['xkana'] : ''); } // If we have to buffer data, we will perform everything at once at the end if ($GLOBALS['buffer_needed']) { $dump_buffer .= $line; if ($GLOBALS['onfly_compression']) { $dump_buffer_len += strlen($line); if ($dump_buffer_len > $GLOBALS['memory_limit']) { if ($GLOBALS['output_charset_conversion']) { $dump_buffer = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $dump_buffer); } // as bzipped if ($GLOBALS['compression'] == 'bzip' && @function_exists('bzcompress')) { $dump_buffer = bzcompress($dump_buffer); } elseif ($GLOBALS['compression'] == 'gzip' && @function_exists('gzencode')) { // without the optional parameter level because it bug $dump_buffer = gzencode($dump_buffer); } if ($GLOBALS['save_on_server']) { $write_result = @fwrite($GLOBALS['file_handle'], $dump_buffer); if (!$write_result || $write_result != strlen($dump_buffer)) { $GLOBALS['message'] = PMA_Message::error('strNoSpace'); $GLOBALS['message']->addParam($save_filename); return false; } } else { echo $dump_buffer; } $dump_buffer = ''; $dump_buffer_len = 0; } } else { $time_now = time(); if ($time_start >= $time_now + 30) { $time_start = $time_now; header('X-pmaPing: Pong'); } // end if } } else { if ($GLOBALS['asfile']) { if ($GLOBALS['output_charset_conversion']) { $line = PMA_convert_string($GLOBALS['charset'], $GLOBALS['charset_of_file'], $line); } if ($GLOBALS['save_on_server'] && strlen($line) > 0) { $write_result = @fwrite($GLOBALS['file_handle'], $line); if (!$write_result || $write_result != strlen($line)) { $GLOBALS['message'] = PMA_Message::error('strNoSpace'); $GLOBALS['message']->addParam($save_filename); return false; } $time_now = time(); if ($time_start >= $time_now + 30) { $time_start = $time_now; header('X-pmaPing: Pong'); } // end if } else { // We export as file - output normally echo $line; } } else { // We export as html - replace special chars echo htmlspecialchars($line); } } return true; }