public function getAll() { $buffer = ""; $counter = 0; $sum = 0; $total = 0; $last = microtime(true); $bwlimit = $this->getBwlimit(); while (!gzeof($this->datasource)) { $tmp = $this->read(); $bytes = strlen($tmp); $buffer .= $tmp; $sum += $bytes; $total += $bytes; $counter++; if ($bwlimit > 0 && $sum > $bwlimit) { $current = microtime(true); $wait = $current - $last; if ($wait < 1) { $wait = 1 - $wait; usleep($wait * 1000000); } $sum = 0; $last = microtime(true); } } return $buffer; }
public function extractTar($src, $dest) { if ($this->is_tar($src)) { file_put_contents($tmp = '~tmp(' . microtime() . ').tar', $src); $src = $tmp; } $ptr = gzopen($src, 'r'); while (!gzeof($ptr)) { $infos = $this->readTarHeader($ptr); if ($infos['type'] == '5') { if (!file_exists($dest . $infos['name'])) { mkdir($dest . $infos['name'], 0777, true); } $result[] = $dest . $infos['name']; } elseif ($infos['type'] == '0' || $infos['type'] == chr(0)) { $dirPath = substr($infos['name'], 0, strrpos($infos['name'], '/')); if (!file_exists($dest . $dirPath)) { mkdir($dest . $dirPath, 0777, true); } if (file_put_contents($dest . $infos['name'], $infos['data'])) { $result[] = $dest . $infos['name']; } } if ($infos) { chmod($dest . $infos['name'], 0777); } } if (is_file($tmp)) { unlink($tmp); } return $result; }
public function import() { // It might not look like it, but it is actually compatible to // uncompressed files. $gzFileHandler = gzopen($this->file, 'r'); Model\DownloadIntent::delete_all(); Model\DownloadIntentClean::delete_all(); $batchSize = 1000; $batch = array(); while (!gzeof($gzFileHandler)) { $line = gzgets($gzFileHandler); list($id, $user_agent_id, $media_file_id, $request_id, $accessed_at, $source, $context, $geo_area_id, $lat, $lng) = explode(",", $line); $batch[] = array($user_agent_id, $media_file_id, $request_id, $accessed_at, $source, $context, $geo_area_id, $lat, $lng); if (count($batch) >= $batchSize) { self::save_batch_to_db($batch); $batch = []; } } gzclose($gzFileHandler); // save last batch to db self::save_batch_to_db($batch); \Podlove\Analytics\DownloadIntentCleanup::cleanup_download_intents(); \Podlove\Cache\TemplateCache::get_instance()->setup_purge(); wp_redirect(admin_url('admin.php?page=podlove_imexport_migration_handle&status=success')); exit; }
function LoadBackUp() { global $SAMSConf; global $USERConf; $DB = new SAMSDB(); $lang = "./lang/lang.{$SAMSConf->LANG}"; require $lang; if ($USERConf->ToWebInterfaceAccess("C") != 1) { exit; } PageTop("reark_48.jpg", "{$backupbuttom_2_loadbase_LoadBackUp_1}"); if (($finp = gzopen($_FILES['userfile']['tmp_name'], "r")) != NULL) { while (gzeof($finp) == 0) { $string = gzgets($finp, 10000); $QUERY = strtok($string, ";"); if (strstr($QUERY, "#") == FALSE) { echo "{$QUERY}<BR>"; $num_rows = $DB->samsdb_query($QUERY . ";"); } $count++; } } print "<SCRIPT>\n"; print " parent.lframe.location.href=\"lframe.php\"; \n"; print "</SCRIPT> \n"; }
public static function update_database() { set_time_limit(0); $outFile = self::get_upload_file_path(); // for download_url() require_once ABSPATH . 'wp-admin/includes/file.php'; $tmpFile = \download_url(self::SOURCE_URL); if (is_wp_error($tmpFile)) { die($tmpFile->get_error_message()); } $zh = gzopen($tmpFile, 'rb'); $h = fopen($outFile, 'wb'); if (!$zh) { die('Downloaded file could not be opened for reading.'); } if (!$h) { die(sprintf('Database could not be written (%s).', $outFile)); } while (!gzeof($zh)) { fwrite($h, gzread($zh, 4096)); } gzclose($zh); fclose($h); unlink($tmpFile); if (!self::is_db_valid()) { die(sprintf('Checksum does not match (%s).', $outFile)); } }
function gzpassthru($fp) { while (!gzeof($fp)) { print gzred($fp, 1 << 20); } gzclose($fp); }
function feof($fp) { if ($this->has_gzip()) { return gzeof($fp); } return feof($fp); }
function wp_all_import_get_gz($filename, $use_include_path = 0, $targetDir = false) { $type = 'csv'; $uploads = wp_upload_dir(); $targetDir = !$targetDir ? wp_all_import_secure_file($uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY) : $targetDir; $tmpname = wp_unique_filename($targetDir, strlen(basename($filename)) < 30 ? basename($filename) : time()); $localPath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname)); $fp = @fopen($localPath, 'w'); $file = @gzopen($filename, 'rb', $use_include_path); if ($file) { $first_chunk = true; while (!gzeof($file)) { $chunk = gzread($file, 1024); if ($first_chunk and strpos($chunk, "<?") !== false and strpos($chunk, "</") !== false) { $type = 'xml'; $first_chunk = false; } // if it's a 1st chunk, then chunk <? symbols to detect XML file @fwrite($fp, $chunk); } gzclose($file); } else { $tmpname = wp_unique_filename($targetDir, strlen(basename($filename)) < 30 ? basename($filename) : time()); $localGZpath = $targetDir . '/' . urldecode(sanitize_file_name($tmpname)); $request = get_file_curl($filename, $localGZpath, false, true); if (!is_wp_error($request)) { $file = @gzopen($localGZpath, 'rb', $use_include_path); if ($file) { $first_chunk = true; while (!gzeof($file)) { $chunk = gzread($file, 1024); if ($first_chunk and strpos($chunk, "<?") !== false and strpos($chunk, "</") !== false) { $type = 'xml'; $first_chunk = false; } // if it's a 1st chunk, then chunk <? symbols to detect XML file @fwrite($fp, $chunk); } gzclose($file); } @unlink($localGZpath); } else { return $request; } } @fclose($fp); if (preg_match('%\\W(gz)$%i', basename($localPath))) { if (@rename($localPath, str_replace('.gz', '.' . $type, $localPath))) { $localPath = str_replace('.gz', '.' . $type, $localPath); } } else { if (@rename($localPath, $localPath . '.' . $type)) { $localPath = $localPath . '.' . $type; } } return array('type' => $type, 'localPath' => $localPath); }
public function decompress() { $tgzFilePath = $this->getPath(); if (!file_exists($tgzFilePath)) { throw new Exception(sprintf('File %s does not exist!', $tgzFilePath)); } if (!is_readable($tgzFilePath)) { throw new Exception(sprintf('File %s is not readable!', $tgzFilePath)); } // Open .tgz file for reading $gzHandle = @gzopen($tgzFilePath, 'rb'); if (!$gzHandle) { throw new Exception(sprintf('Could not open %s for reading!', $tgzFilePath)); } jimport('joomla.filesystem.file'); while (!gzeof($gzHandle)) { if ($block = gzread($gzHandle, 512)) { $meta['filename'] = trim(substr($block, 0, 99)); $meta['filesize'] = octdec(substr($block, 124, 12)); if ($bytes = $meta['filesize'] % 512) { $meta['nullbytes'] = 512 - $bytes; } else { $meta['nullbytes'] = 0; } if ($meta['filesize']) { // Make sure our extension is .xml if (($ext = JFile::getExt($meta['filename'])) != 'xml') { throw new Exception(sprintf('Attempted to extract a file with an invalid extension (%s) - archive might be damaged.', preg_replace('#[^a-z0-9]#is', '', $ext))); } // Make sure file does not contain invalid characters if (preg_match('/[^a-z_\\-0-9]/i', JFile::stripExt($meta['filename']))) { throw new Exception('Attempted to extract a file with invalid characters in its name.'); } $chunk = 1024 * 1024; $left = $meta['filesize']; $fHandle = @fopen($this->path . '/' . $meta['filename'], 'wb'); if (!$fHandle) { throw new Exception(sprintf('Could not write data to file %s!', htmlentities($meta['filename'], ENT_COMPAT, 'utf-8'))); } do { $left = $left - $chunk; if ($left < 0) { $chunk = $left + $chunk; } $data = gzread($gzHandle, $chunk); fwrite($fHandle, $data); } while ($left > 0); fclose($fHandle); } if ($meta['nullbytes'] > 0) { gzread($gzHandle, $meta['nullbytes']); } } } gzclose($gzHandle); }
/** * Open the file containing the backup data * @return String a String containing the DB Dump * @access private */ function _Open() { $fp = gzopen($this->filename, "rb") or die("Error. No se pudo abrir el archivo {$this->filename}"); while (!gzeof($fp)) { $line = gzgets($fp, 1024); $SQL .= "{$line}"; } gzclose($fp); return $SQL; }
/** * @param $fileName * @param $outputFilePath */ private function decompressFile($fileName, $outputFilePath) { $gz = gzopen($fileName, 'rb'); $outputFile = fopen($outputFilePath, 'wb'); while (!gzeof($gz)) { fwrite($outputFile, gzread($gz, 4096)); } fclose($outputFile); gzclose($gz); }
function __endoffile() { switch ($this->type) { case 'gz': return gzeof($this->handle); break; case 'file': return feof($this->handle); break; } }
function gzUncompressFile($srcName, $dstName) { $sfp = gzopen($srcName, "rb"); $fp = fopen($dstName, "w"); while (!gzeof($sfp)) { $string = gzread($sfp, 4096); fwrite($fp, $string, strlen($string)); } gzclose($sfp); fclose($fp); }
/** * Download remote data store * * Used by the mautic:iplookup:update_data command and form fetch button (if applicable) to update local IP data stores * * @return bool */ public function downloadRemoteDataStore() { $connector = HttpFactory::getHttp(); $package = $this->getRemoteDateStoreDownloadUrl(); try { $data = $connector->get($package); } catch (\Exception $exception) { $this->logger->error('Failed to fetch remote IP data: ' . $exception->getMessage()); } $tempTarget = $this->cacheDir . '/' . basename($package); $tempExt = strtolower(pathinfo($package, PATHINFO_EXTENSION)); $localTarget = $this->getLocalDataStoreFilepath(); $localTargetExt = strtolower(pathinfo($localTarget, PATHINFO_EXTENSION)); try { $success = false; switch (true) { case $localTargetExt === $tempExt: $success = (bool) file_put_contents($localTarget, $data->body); break; case 'gz' == $tempExt: if (function_exists('gzdecode')) { $success = (bool) file_put_contents($localTarget, gzdecode($data->body)); } elseif (function_exists('gzopen')) { if (file_put_contents($tempTarget, $data->body)) { $bufferSize = 4096; // read 4kb at a time $file = gzopen($tempTarget, 'rb'); $outFile = fopen($localTarget, 'wb'); while (!gzeof($file)) { fwrite($outFile, gzread($file, $bufferSize)); } fclose($outFile); gzclose($file); @unlink($tempTarget); $success = true; } } break; case 'zip' == $tempExt: file_put_contents($tempTarget, $data->body); $zipper = new \ZipArchive(); $zipper->open($tempTarget); $success = $zipper->extractTo($localTarget); $zipper->close(); @unlink($tempTarget); break; } } catch (\Exception $exception) { error_log($exception); $success = false; } return $success; }
function gunzip($file) { $file = @gzopen($file, 'rb', 0); if ($file) { $data = ''; while (!gzeof($file)) { $data .= gzread($file, 1024); } gzclose($file); } return $data; }
function gunzip($infile, $outfile) { $string = null; $zp = gzopen($infile, "r"); while (!gzeof($zp)) { $string .= gzread($zp, 4096); } gzclose($zp); $fp = fopen($outfile, "w"); fwrite($fp, $string, strlen($string)); fclose($fp); }
function gzgetcont($f) { $d = ""; $fo = gzopen($f, 'r'); if ($fo) { while (!gzeof($fo)) { $d .= gzgets($fo); } } gzclose($fo); return $d; }
/** * Extracts single gzipped file. If archive will contain more then one file you will got a mess. * * @param $archive * @param $destination * @return int */ public function unGZip($archive, $destination) { $buffer_size = 4096; // read 4kb at a time $archive = gzopen($archive, 'rb'); $dat = fopen($destination, 'wb'); while (!gzeof($archive)) { fwrite($dat, gzread($archive, $buffer_size)); } fclose($dat); gzclose($archive); return filesize($destination); }
static function install($source, $filename) { $target = SIMPLE_EXT . substr($filename, 0, -3); setup::out("{t}Download{/t}: " . $source . " ..."); if ($fz = gzopen($source, "r") and $fp = fopen($target, "w")) { $i = 0; while (!gzeof($fz)) { $i++; setup::out(".", false); if ($i % 160 == 0) { setup::out(); } fwrite($fp, gzread($fz, 16384)); } gzclose($fz); fclose($fp); } else { sys_die("{t}Error{/t}: gzopen [2] " . $source); } setup::out(); if (!file_exists($target) or filesize($target) == 0 or filesize($target) % 10240 != 0) { sys_die("{t}Error{/t}: file-check [3] Filesize: " . filesize($target) . " " . $target); } setup::out(sprintf("{t}Processing %s ...{/t}", basename($target))); $tar_object = new Archive_Tar($target); $tar_object->setErrorHandling(PEAR_ERROR_PRINT); $tar_object->extract(SIMPLE_EXT); $file_list = $tar_object->ListContent(); if (!is_array($file_list) or !isset($file_list[0]["filename"]) or !is_dir(SIMPLE_EXT . $file_list[0]["filename"])) { sys_die("{t}Error{/t}: tar [4] " . $target); } self::update_modules_list(); $ext_folder = db_select_value("simple_sys_tree", "id", "anchor=@anchor@", array("anchor" => "extensions")); foreach ($file_list as $file) { sys_chmod(SIMPLE_EXT . $file["filename"]); setup::out(sprintf("{t}Processing %s ...{/t}", SIMPLE_EXT . $file["filename"])); if (basename($file["filename"]) == "install.php") { setup::out(""); require SIMPLE_EXT . $file["filename"]; setup::out(""); } if (basename($file["filename"]) == "readme.txt") { $data = file_get_contents(SIMPLE_EXT . $file["filename"]); setup::out(nl2br("\n" . q($data) . "\n")); } if (!empty($ext_folder) and basename($file["filename"]) == "folders.xml") { setup::out(sprintf("{t}Processing %s ...{/t}", "folder structure")); folders::create_default_folders(SIMPLE_EXT . $file["filename"], $ext_folder, false); } } }
/** * @param $filename * @return string */ protected function decompressFile($filename) { $dataDir = $this->getDataDirectoryPath(); $zip = gzopen($filename, 'rb'); $outputUncompressedTempFileName = tempnam($dataDir, 'dbupdate'); $outputUncompressedTempFile = fopen($outputUncompressedTempFileName, 'wb'); $bufferSize = 4096; while (!gzeof($zip)) { fwrite($outputUncompressedTempFile, gzread($zip, $bufferSize)); } fclose($outputUncompressedTempFile); gzclose($zip); return $outputUncompressedTempFileName; }
/** * Executes the ImportSqlDump Task. * * @return Robo\Result */ public function run() { // Lets make sure we have a database if (!$this->taskCreateDb()->host($this->host)->user($this->user)->pass($this->pass)->name($this->name)->dropTables(true)->run()->wasSuccessful()) { throw new RuntimeException('We failed to create the db.'); } // Do we need to uncompress it first? if (strpos($this->dump, '.gz') !== false) { // Create a temp dump file $temp_dump = tempnam(sys_get_temp_dir(), 'dump'); // Decompress the dump file if ($fp_out = fopen($temp_dump, 'wb')) { if ($fp_in = gzopen($this->dump, 'rb')) { while (!gzeof($fp_in)) { fwrite($fp_out, gzread($fp_in, 1024 * 512)); } fclose($fp_in); } else { throw new RuntimeException('Failed to open source dump file for reading.'); } gzclose($fp_out); } else { throw new RuntimeException('Failed to open temp dump file for writing.'); } // Set the dump the deflated version $this->dump = $temp_dump; // Delete the temp later $delete_me = $this->dump; } // Construct the command to import the dump $cmd = 'mysql -h' . $this->host . ' -u' . $this->user; if (!empty($this->pass)) { $cmd .= ' -p' . $this->pass; } $cmd .= ' ' . $this->name . ' < ' . $this->dump; // Run the command if (!$this->taskExec($cmd)->run()->wasSuccessful()) { throw new RuntimeException('We failed to import your dump. ' . 'HINT: Is the `mysql` binary in your "PATH"?'); } // Delete the deflated temp dump file if (isset($delete_me)) { $this->printTaskInfo('Deleting temp dump file.'); if (!unlink($delete_me)) { return Result::error($this, 'Couldn`t delete temp file.'); } } // If we get to here assume everything worked return Result::success($this); }
function extractGzip($src, $dest = false) { $zp = gzopen($src, "r"); $data = ''; while (!gzeof($zp)) { $data .= gzread($zp, 1024 * 1024); } gzclose($zp); if (empty($dest)) { return $data; } elseif (file_put_contents($dest, $data)) { return $dest; } return false; }
public static function gunzip($in_file, $out_file_name = '') { $buffer_size = 4096; // read 4kb at a time if ($out_file_name == '') { $out_file_name = str_replace('.gz', '', $in_file); } if ($in_file != '' && file_exists($in_file)) { $file = gzopen($in_file, 'rb'); $out_file = fopen($out_file_name, 'wb'); while (!gzeof($file)) { fwrite($out_file, gzread($file, $buffer_size)); } fclose($out_file); gzclose($file); } }
public function gets() { if (is_null($this->handle) || gzeof($this->handle)) { return false; } $offset = $this->tell(); $data = gzgets($this->handle); if ($data === false) { return false; } $data = Log::factory($data); $data->line_offset = $offset; return $data; }
/** * Unpack file by GZ compressor. * * @param string $source * @param string $destination * @return string */ public function unpack($source, $destination) { $gzPointer = gzopen($source, 'r'); if (empty($gzPointer)) { throw new Mage_Exception('Can\'t open GZ archive : ' . $source); } $data = ''; while (!gzeof($gzPointer)) { $data .= gzread($gzPointer, 131072); } gzclose($gzPointer); if (is_dir($destination)) { $file = $this->getFilename($source); $destination = $destination . $file; } $this->_writeFile($destination, $data); return $destination; }
protected function setUp() { $dbgz = 'http://geolite.maxmind.com/download/' . 'geoip/database/GeoLite2-City.mmdb.gz'; $filename = Path::build(sys_get_temp_dir(), 'GeoLite2-City.mmdb.gz'); $this->_geoipdb = substr($filename, 0, -3); if (!file_exists($this->_geoipdb)) { $opts = ['http' => ['method' => "GET", 'header' => "Accept-language: en\r\n" . "User-Agent: CURL (Cubex Framework; en-us)\r\n"]]; $context = stream_context_create($opts); file_put_contents($filename, file_get_contents($dbgz, false, $context)); $file = gzopen($filename, 'rb'); $out = fopen($this->_geoipdb, 'wb'); while (!gzeof($file)) { fwrite($out, gzread($file, 4096)); } fclose($out); gzclose($file); unlink($filename); } }
public function gzUnpack() { $file_name = storage_path() . '/app/YouBikeTP.gz'; // Raising this value may increase performance $buffer_size = 4096; // read 4kb at a time $out_file_name = str_replace('.gz', '', $file_name); // Open our files (in binary mode) $file = gzopen($file_name, 'rb'); $out_file = fopen($out_file_name, 'wb'); // Keep repeating until the end of the input file while (!gzeof($file)) { // Read buffer-size bytes // Both fwrite and gzread and binary-safe fwrite($out_file, gzread($file, $buffer_size)); } // Files are done, close files fclose($out_file); gzclose($file); }
function doImport() { global $PIOEnv; if (preg_match('/^(.*):\\/\\//i', PIO_ANOTHER_CONNSTR, $backend)) { define('PIO_FROM', $backend[1]); } include './lib/pio/pio.' . PIO_FROM . '.php'; $pio1 = 'PIO' . PIO_FROM; $PIO = new $pio1(PIO_ANOTHER_CONNSTR, $PIOEnv); if (!file_exists('piodata.log.gz')) { echo '檔案不存在,請先放置在相同目錄。'; return; } $data = ''; $gp = gzopen('piodata.log.gz', 'r'); while (!gzeof($gp)) { $data .= gzread($gp, 4096); } gzclose($gp); echo $PIO->dbImport($data) ? '匯入成功' : '匯入失敗'; }
/** * @param InputInterface $input * @param OutputInterface $output * * @return int */ protected function execute(InputInterface $input, OutputInterface $output) { $now = time(); $file_csv = $this->getContainer()->getParameter('kernel.cache_dir') . '/' . $this->getContainer()->getParameter('anime_db.ani_db.titles_db'); if (!file_exists($file_csv) || filemtime($file_csv) + self::CACHE_LIFE_TIME < $now) { try { $file = $this->getOriginDb($output, $now); } catch (\Exception $e) { $output->writeln(sprintf('<error>AniDB list titles is not downloaded: %s</error>', $e->getMessage())); return 0; } $output->writeln('Start assembling database'); // clear list titles and add unified title $fp = gzopen($file, 'r'); $fp_csv = gzopen($file_csv, 'w'); while (!gzeof($fp)) { $line = trim(gzgets($fp, 4096)); // ignore comments if ($line[0] == '#') { continue; } list($aid, $type, $lang, $title) = explode('|', $line); $lang = substr($lang, 0, 2); // ignore not supported locales if ($lang == 'x-') { continue; } gzwrite($fp_csv, $aid . '|' . $type . '|' . $lang . '|' . $this->getUnifiedTitle($title) . '|' . $title . "\n"); } gzclose($fp); gzclose($fp_csv); touch($file, $now); touch($file_csv, $now); $output->writeln('The titles database is updated'); } else { $output->writeln('Update is not needed'); } return 0; }
public function updateDatabase($productId, $filename) { try { if (empty($filename)) { $response = $this->guzzle->get('/app/update_getfilename', ['query' => ['product_id' => $productId]]); $filename = $response->getBody()->getContents(); } $filepath = sprintf('%s/%s', $this->storagePath, $filename); $tmpFilepath = $filepath . '.gz'; $md5File = '00000000000000000000000000000000'; if (file_exists($filepath)) { $md5File = md5_file(sprintf('%s/%s', $this->storagePath, $filename)); } $response = $this->guzzle->get('/app/update_getipaddr'); $clientIpAddress = $response->getBody()->getContents(); $md5Client = md5($this->licenseKey . $clientIpAddress); $this->guzzle->get('/app/update_secure', ['query' => ['db_md5' => $md5File, 'challenge_md5' => $md5Client, 'user_id' => $this->userId, 'edition_id' => $productId], 'save_to' => $tmpFilepath, 'decode_content' => false]); } catch (ClientException $clientException) { $this->output->writeln(sprintf('<error>Error updating database with Product ID "%s"</error>', $productId)); return false; } $noUpdates = 'No new updates available'; if (strcasecmp($noUpdates, file_get_contents($tmpFilepath, false, null, 0, strlen($noUpdates))) == 0) { $this->output->writeln(sprintf('<info>Product ID "%s" database "%s" is already up to date</info>', $productId, $filename)); return true; } $gzFileHandle = gzopen($tmpFilepath, 'r'); $fileHandle = fopen($filepath, 'w'); if ($gzFileHandle !== false && $fileHandle !== false) { while (!gzeof($gzFileHandle)) { fwrite($fileHandle, gzread($gzFileHandle, 1024)); } fclose($fileHandle); gzclose($gzFileHandle); unlink($tmpFilepath); } $this->output->writeln(sprintf('<info>Updated Product ID "%s" database "%s"</info>', $productId, $filename)); return true; }