function readChunk($posx, $posz) { global $REGION_DIR; // calculate region file to read $regionX = floor($posx / 32); $regionZ = floor($posz / 32); // open region file, seek to header info $file = gzopen($REGION_DIR . "r.{$regionX}.{$regionZ}.mcr", 'r'); $chunkHeaderLoc = 4 * (floormod($posx, 32) + floormod($posz, 32) * 32); gzseek($file, $chunkHeaderLoc); $info = unpack('C*', gzread($file, 4)); $chunkDataLoc = $info[1] << 16 | $info[2] << 8 | $info[3]; // if chunk hasn't been generated, return empty if ($chunkDataLoc == 0) { return array(); } // seek to data, write to gz and return gzseek($file, $chunkDataLoc * 4096); $info = unpack('C*', gzread($file, 4)); $chunkLength = $info[1] << 32 | $info[2] << 16 | $info[3] << 8 | $info[4]; // read to skip over compression byte gzread($file, 1); // skip first two bytes for deflate gzread($file, 2); // leave off last four bytes for deflate $chunkLength -= 4; $contents = gzread($file, $chunkLength - 1); $contents = gzinflate($contents); $data = array_merge(unpack("C*", $contents)); return $data; }
function extract_file_from_tarball($pkg, $filename, $dest_dir) { global $packages; $name = $pkg . '-' . $packages[$pkg]; $tarball = $dest_dir . "/" . $name . '.tgz'; $filename = $name . '/' . $filename; $destfilename = $dest_dir . "/" . basename($filename); $fp = gzopen($tarball, 'rb'); $done = false; do { /* read the header */ $hdr_data = gzread($fp, 512); if (strlen($hdr_data) == 0) { break; } $checksum = 0; for ($i = 0; $i < 148; $i++) { $checksum += ord($hdr_data[$i]); } for ($i = 148; $i < 156; $i++) { $checksum += 32; } for ($i = 156; $i < 512; $i++) { $checksum += ord($hdr_data[$i]); } $hdr = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $hdr_data); $hdr['checksum'] = octdec(trim($hdr['checksum'])); if ($hdr['checksum'] != $checksum) { echo "Checksum for {$tarball} {$hdr['filename']} is invalid\n"; print_r($hdr); return; } $hdr['size'] = octdec(trim($hdr['size'])); echo "File: {$hdr['filename']} {$hdr['size']}\n"; if ($filename == $hdr['filename']) { echo "Found the file we want\n"; $dest = fopen($destfilename, 'wb'); $x = stream_copy_to_stream($fp, $dest, $hdr['size']); fclose($dest); echo "Wrote {$x} bytes into {$destfilename}\n"; break; } /* skip body of the file */ $size = 512 * ceil((int) $hdr['size'] / 512); echo "Skipping {$size} bytes\n"; gzseek($fp, gztell($fp) + $size); } while (!$done); }
$offset = ftell($fp); } //Header auslesen $sline = ReadStatusline($statusline); $anzahl_tabellen = $sline['tables']; $anzahl_eintraege = $sline['records']; $tbl_zeile = ''; $part = $sline['part'] == '' ? 0 : substr($sline['part'], 3); if ($anzahl_eintraege == -1) { // not a backup of MySQLDumper $tpl->assign_block_vars('NO_MSD_BACKUP', array()); } else { $tabledata = array(); $i = 0; //Tabellenfinos lesen gzseek($fp, $offset); $eof = false; while (!$eof) { $line = $gz ? gzgets($fp, 40960) : fgets($fp, 40960); if (substr($line, 0, 9) == '-- TABLE|') { $d = explode('|', $line); $tabledata[$i]['name'] = $d[1]; $tabledata[$i]['records'] = $d[2]; $tabledata[$i]['size'] = $d[3]; $tabledata[$i]['update'] = $d[4]; $tabledata[$i]['engine'] = isset($d[5]) ? $d[5] : ''; $i++; } if (substr($line, 0, 6) == '-- EOF') { $eof = true; }
private function gzopen_for_read($file, &$warn, &$err) { if (!function_exists('gzopen') || !function_exists('gzread')) { $missing = ''; if (!function_exists('gzopen')) { $missing .= 'gzopen'; } if (!function_exists('gzread')) { $missing .= $missing ? ', gzread' : 'gzread'; } $err[] = sprintf(__("Your web server's PHP installation has these functions disabled: %s.", 'updraftplus'), $missing) . ' ' . sprintf(__('Your hosting company must enable these functions before %s can work.', 'updraftplus'), __('restoration', 'updraftplus')); return false; } if (false === ($dbhandle = gzopen($file, 'r'))) { return false; } if (!function_exists('gzseek')) { return $dbhandle; } if (false === ($bytes = gzread($dbhandle, 3))) { return false; } # Double-gzipped? if ('H4sI' != base64_encode($bytes)) { if (0 === gzseek($dbhandle, 0)) { return $dbhandle; } else { @gzclose($dbhandle); return gzopen($file, 'r'); } } # Yes, it's double-gzipped $what_to_return = false; $mess = __('The database file appears to have been compressed twice - probably the website you downloaded it from had a mis-configured webserver.', 'updraftplus'); $messkey = 'doublecompress'; $err_msg = ''; if (false === ($fnew = fopen($file . ".tmp", 'w')) || !is_resource($fnew)) { @gzclose($dbhandle); $err_msg = __('The attempt to undo the double-compression failed.', 'updraftplus'); } else { @fwrite($fnew, $bytes); $emptimes = 0; while (!gzeof($dbhandle)) { $bytes = @gzread($dbhandle, 131072); if (empty($bytes)) { $emptimes++; $this->log("Got empty gzread ({$emptimes} times)"); if ($emptimes > 2) { break; } } else { @fwrite($fnew, $bytes); } } gzclose($dbhandle); fclose($fnew); # On some systems (all Windows?) you can't rename a gz file whilst it's gzopened if (!rename($file . ".tmp", $file)) { $err_msg = __('The attempt to undo the double-compression failed.', 'updraftplus'); } else { $mess .= ' ' . __('The attempt to undo the double-compression succeeded.', 'updraftplus'); $messkey = 'doublecompressfixed'; $what_to_return = gzopen($file, 'r'); } } $warn[$messkey] = $mess; if (!empty($err_msg)) { $err[] = $err_msg; } return $what_to_return; }
function _jumpBlock($p_len = null) { if (is_resource($this->_file)) { if ($p_len === null) { $p_len = 1; } if ($this->_compress_type == 'gz') { @gzseek($this->_file, @gztell($this->_file) + $p_len * 512); } else { if ($this->_compress_type == 'bz2') { // ----- Replace missing bztell() and bzseek() for ($i = 0; $i < $p_len; $i++) { $this->_readBlock(); } } else { if ($this->_compress_type == 'none') { @fseek($this->_file, @ftell($this->_file) + $p_len * 512); } else { $this->_error('Unknown or missing compression type (' . $this->_compress_type . ')'); } } } } return true; }
function doSeek($offset, $whence) { if ($whence == SEEK_SET) { $offset = $offset - gztell($this->File); } else { if ($whence == SEEK_END) { eZDebugSetting::writeError('lib-ezfile-gziplibz', "Seeking from end is not supported for gzipped files"); return false; } } return @gzseek($this->File, $offset); }
/** * Seek the file * * Note: the return value is different to that of fseek * * @param integer $offset Offset to use when seeking. * @param integer $whence Seek mode to use. * * @return boolean True on success, false on failure * * @see http://php.net/manual/en/function.fseek.php * @since 11.1 */ public function seek($offset, $whence = SEEK_SET) { if (!$this->_fh) { $this->setError(JText::_('JLIB_FILESYSTEM_ERROR_STREAMS_FILE_NOT_OPEN')); return false; } $retval = false; // Capture PHP errors $php_errormsg = ''; $track_errors = ini_get('track_errors'); ini_set('track_errors', true); switch ($this->processingmethod) { case 'gz': $res = gzseek($this->_fh, $offset, $whence); break; case 'bz': case 'f': default: $res = fseek($this->_fh, $offset, $whence); break; } // Seek, interestingly, returns 0 on success or -1 on failure. if ($res == -1) { $this->setError($php_errormsg); } else { $retval = true; } // Restore error tracking to what it was before ini_set('track_errors', $track_errors); // Return the result return $retval; }
$filename = tempnam("/tmp", "phpt"); $fp = fopen($filename, "wb"); fwrite($fp, $original); var_dump(strlen($original)); var_dump(ftell($fp)); fclose($fp); $fp = gzopen($filename, "rb"); $data = ''; while ($buf = gzread($fp, 8192)) { $data .= $buf; } if ($data == $original) { echo "Strings are equal\n"; } else { echo "Strings are not equal\n"; var_dump($data); } gzseek($fp, strlen($original) / 2); $data = ''; while ($buf = gzread($fp, 8192)) { $data .= $buf; } var_dump(strlen($data)); if ($data == substr($original, strlen($original) / 2)) { echo "Strings are equal\n"; } else { echo "Strings are not equal\n"; var_dump($data); } gzclose($fp); unlink($filename);
function _extractList($p_path, &$p_list_detail, $p_mode, $p_file_list, $p_remove_path) { $v_result = true; $v_nb = 0; $v_extract_all = true; $v_listing = false; $p_path = $this->_translateWinPath($p_path, false); if ($p_path == '' || substr($p_path, 0, 1) != '/' && substr($p_path, 0, 3) != "../" && !strpos($p_path, ':')) { $p_path = "./" . $p_path; } $p_remove_path = $this->_translateWinPath($p_remove_path); // ----- Look for path to remove format (should end by /) if ($p_remove_path != '' && substr($p_remove_path, -1) != '/') { $p_remove_path .= '/'; } $p_remove_path_size = strlen($p_remove_path); switch ($p_mode) { case "complete": $v_extract_all = TRUE; $v_listing = FALSE; break; case "partial": $v_extract_all = FALSE; $v_listing = FALSE; break; case "list": $v_extract_all = FALSE; $v_listing = TRUE; break; default: $this->_error('Invalid extract mode (' . $p_mode . ')'); return false; } clearstatcache(); while (!($v_end_of_file = $this->_compress ? @gzeof($this->_file) : @feof($this->_file))) { $v_extract_file = FALSE; $v_extraction_stopped = 0; if ($this->_compress) { $v_binary_data = @gzread($this->_file, 512); } else { $v_binary_data = @fread($this->_file, 512); } if (!$this->_readHeader($v_binary_data, $v_header)) { return false; } if ($v_header['filename'] == '') { continue; } // ----- Look for long filename if ($v_header['typeflag'] == 'L') { if (!$this->_readLongHeader($v_header)) { return false; } } if (!$v_extract_all && is_array($p_file_list)) { // ----- By default no unzip if the file is not found $v_extract_file = false; for ($i = 0; $i < sizeof($p_file_list); $i++) { // ----- Look if it is a directory if (substr($p_file_list[$i], -1) == '/') { // ----- Look if the directory is in the filename path if (strlen($v_header['filename']) > strlen($p_file_list[$i]) && substr($v_header['filename'], 0, strlen($p_file_list[$i])) == $p_file_list[$i]) { $v_extract_file = TRUE; break; } } elseif ($p_file_list[$i] == $v_header['filename']) { $v_extract_file = TRUE; break; } } } else { $v_extract_file = TRUE; } // ----- Look if this file need to be extracted if ($v_extract_file && !$v_listing) { if ($p_remove_path != '' && substr($v_header['filename'], 0, $p_remove_path_size) == $p_remove_path) { $v_header['filename'] = substr($v_header['filename'], $p_remove_path_size); } if ($p_path != './' && $p_path != '/') { while (substr($p_path, -1) == '/') { $p_path = substr($p_path, 0, strlen($p_path) - 1); } if (substr($v_header['filename'], 0, 1) == '/') { $v_header['filename'] = $p_path . $v_header['filename']; } else { $v_header['filename'] = $p_path . '/' . $v_header['filename']; } } if (file_exists($v_header['filename'])) { if (@is_dir($v_header['filename']) && $v_header['typeflag'] == '') { $this->_error('File ' . $v_header['filename'] . ' already exists as a directory'); return false; } if (is_file($v_header['filename']) && $v_header['typeflag'] == "5") { $this->_error('Directory ' . $v_header['filename'] . ' already exists as a file'); return false; } if (!is_writeable($v_header['filename'])) { $this->_error('File ' . $v_header['filename'] . ' already exists and is write protected'); return false; } if (filemtime($v_header['filename']) > $v_header['mtime']) { // To be completed : An error or silent no replace ? } } elseif (($v_result = $this->_dirCheck($v_header['typeflag'] == "5" ? $v_header['filename'] : dirname($v_header['filename']))) != 1) { $this->_error('Unable to create path for ' . $v_header['filename']); return false; } if ($v_extract_file) { if ($v_header['typeflag'] == "5") { if (!@file_exists($v_header['filename'])) { if (!@mkdir($v_header['filename'], 0777)) { $this->_error('Unable to create directory {' . $v_header['filename'] . '}'); return false; } } } else { if (($v_dest_file = @fopen($v_header['filename'], "wb")) == 0) { $this->_error('Error while opening {' . $v_header['filename'] . '} in write binary mode'); return false; } else { $n = floor($v_header['size'] / 512); for ($i = 0; $i < $n; $i++) { if ($this->_compress) { $v_content = @gzread($this->_file, 512); } else { $v_content = @fread($this->_file, 512); } fwrite($v_dest_file, $v_content, 512); } if ($v_header['size'] % 512 != 0) { if ($this->_compress) { $v_content = @gzread($this->_file, 512); } else { $v_content = @fread($this->_file, 512); } fwrite($v_dest_file, $v_content, $v_header['size'] % 512); } @fclose($v_dest_file); // ----- Change the file mode, mtime @touch($v_header['filename'], $v_header['mtime']); // To be completed //chmod($v_header[filename], DecOct($v_header[mode])); } // ----- Check the file size clearstatcache(); if (filesize($v_header['filename']) != $v_header['size']) { $this->_error('Extracted file ' . $v_header['filename'] . ' does not have the correct file size \'' . filesize($v_filename) . '\' (' . $v_header['size'] . ' expected). Archive may be corrupted.'); return false; } } } else { // ----- Jump to next file if ($this->_compress) { @gzseek($this->_file, @gztell($this->_file) + ceil($v_header['size'] / 512) * 512); } else { @fseek($this->_file, @ftell($this->_file) + ceil($v_header['size'] / 512) * 512); } } } else { // ----- Jump to next file if ($this->_compress) { @gzseek($this->_file, @gztell($this->_file) + ceil($v_header['size'] / 512) * 512); } else { @fseek($this->_file, @ftell($this->_file) + ceil($v_header['size'] / 512) * 512); } } if ($this->_compress) { $v_end_of_file = @gzeof($this->_file); } else { $v_end_of_file = @feof($this->_file); } if ($v_listing || $v_extract_file || $v_extraction_stopped) { // ----- Log extracted files if (($v_file_dir = dirname($v_header['filename'])) == $v_header['filename']) { $v_file_dir = ''; } if (substr($v_header['filename'], 0, 1) == '/' && $v_file_dir == '') { $v_file_dir = '/'; } $p_list_detail[$v_nb++] = $v_header; } } return true; }
$restore['tablelock'] = 0; $restore['erweiterte_inserts'] = 0; if ($sline['tables'] == "-1") { $restore['compressed'] ? gzseek($restore['filehandle'], 0) : fseek($restore['filehandle'], 0); } if ($restore['part'] > 0) { WriteLog('Start Multipart-Restore \'' . $restore['filename'] . '\''); } else { WriteLog('Start Restore \'' . $restore['filename'] . '\''); } } else { if ($restore['compressed'] == 0) { $filegroesse = filesize($fpath . $restore['filename']); } // Dateizeiger an die richtige Stelle setzen $restore['compressed'] ? gzseek($restore['filehandle'], $restore['offset']) : fseek($restore['filehandle'], $restore['offset']); // Jetzt basteln wir uns mal unsere Befehle zusammen... $a = 0; $dauer = 0; $restore['EOB'] = false; //lock_table($restore['actual_table']); while ($a < $restore['anzahl_zeilen'] && !$restore['fileEOF'] && $dauer < $restore['max_zeit'] && !$restore['EOB']) { $sql_command = get_sqlbefehl(); if ($sql_command > '') { //WriteLog($sql_command); $meldung = ''; $res = mysql_query($sql_command, $config['dbconnection']); if (!$res === false) { $anzsql = mysql_affected_rows($config['dbconnection']); // Anzahl der eingetragenen Datensaetze ermitteln (Indexaktionen nicht zaehlen) $command = strtoupper(substr($sql_command, 0, 7));
public function stream_seek($offset, $whence) { return gzseek($this->_resource, $offset, $whence); }
/** * Read data from dictionary. * * @param int $offset * @param int $size * @return string * @throws DictException When seek operation to offset failed. */ public function getData($offset, $size) { if ($this->isCompressed) { if (($seek = gzseek($this->handle, $offset)) !== -1) { $data = gzread($this->handle, $size); } } else { if (($seek = fseek($this->handle, $offset)) !== -1) { $data = fread($this->handle, $offset); } } if ($seek === -1) { throw new DictException($this, 'File seek error.'); } return $data; }
protected function jumpBlock($p_len=null) { if (is_resource($this->file)) { if ($p_len === null) $p_len = 1; if ($this->compressType == 'gz') { @gzseek($this->file, gztell($this->file)+($p_len*512)); } else if ($this->compressType == 'bz2') { // ----- Replace missing bztell() and bzseek() for ($i=0; $i<$p_len; $i++) $this->readBlock(); } else if ($this->compressType == 'none') @fseek($this->file, ftell($this->file)+($p_len*512)); else $this->error('Unknown or missing compression type (' .$this->compressType.')'); } return true; }
public function entrySeek($offset, $whence) { if (!$this->entryFile) { return false; } return $this->writeMode ? gzseek($this->entryFile, $offset, $whence) : fseek($this->entryFile, $offset, $whence); }
/** * Skip forward in the open file pointer * * This is basically a wrapper around seek() (and a workaround for bzip2) * * @param int $bytes seek to this position */ function skipbytes($bytes) { if ($this->comptype === Tar::COMPRESS_GZIP) { @gzseek($this->fh, $bytes, SEEK_CUR); } elseif ($this->comptype === Tar::COMPRESS_BZIP) { // there is no seek in bzip2, we simply read on @bzread($this->fh, $bytes); } else { @fseek($this->fh, $bytes, SEEK_CUR); } }
function PclTarHandleExtractFile($p_tar, &$v_header, $p_path, $p_remove_path, $p_tar_mode) { TrFctStart(__FILE__, __LINE__, "PclTarHandleExtractFile", "archive_descr='{$p_tar}', path={$p_path}, remove_path='{$p_remove_path}', tar_mode={$p_tar_mode}"); $v_result = 1; // TBC : I should replace all $v_tar by $p_tar in this function .... $v_tar = $p_tar; $v_extract_file = 1; $p_remove_path_size = strlen($p_remove_path); // ----- Look for path to remove if ($p_remove_path != "" && substr($v_header[filename], 0, $p_remove_path_size) == $p_remove_path) { TrFctMessage(__FILE__, __LINE__, 3, "Found path '{$p_remove_path}' to remove in file '{$v_header['filename']}'"); // ----- Remove the path $v_header[filename] = substr($v_header[filename], $p_remove_path_size); TrFctMessage(__FILE__, __LINE__, 3, "Resulting file is '{$v_header['filename']}'"); } // ----- Add the path to the file if ($p_path != "./" && $p_path != "/") { // ----- Look for the path end '/' while (substr($p_path, -1) == "/") { TrFctMessage(__FILE__, __LINE__, 3, "Destination path [{$p_path}] ends by '/'"); $p_path = substr($p_path, 0, strlen($p_path) - 1); TrFctMessage(__FILE__, __LINE__, 3, "Modified to [{$p_path}]"); } // ----- Add the path if (substr($v_header[filename], 0, 1) == "/") { $v_header[filename] = $p_path . $v_header[filename]; } else { $v_header[filename] = $p_path . "/" . $v_header[filename]; } } // ----- Trace TrFctMessage(__FILE__, __LINE__, 2, "Extracting file (with path) '{$v_header['filename']}', size '{$v_header['size']}'"); // ----- Check that the file does not exists if (file_exists($v_header[filename])) { TrFctMessage(__FILE__, __LINE__, 2, "File '{$v_header['filename']}' already exists"); // ----- Look if file is a directory if (is_dir($v_header[filename])) { TrFctMessage(__FILE__, __LINE__, 2, "Existing file '{$v_header['filename']}' is a directory"); // ----- Change the file status $v_header[status] = "already_a_directory"; // ----- Skip the extract $v_extraction_stopped = 1; $v_extract_file = 0; } else { if (!is_writeable($v_header[filename])) { if (!is_writeable($v_header[filename])) { TrFctMessage(__FILE__, __LINE__, 2, "Existing file '{$v_header['filename']}' is write protected"); // ----- Change the file status $v_header[status] = "write_protected"; // ----- Skip the extract $v_extraction_stopped = 1; $v_extract_file = 0; } else { if (filemtime($v_header[filename]) > $v_header[mtime]) { TrFctMessage(__FILE__, __LINE__, 2, "Existing file '{$v_header['filename']}' is newer (" . date("l dS of F Y h:i:s A", filemtime($v_header[filename])) . ") than the extracted file (" . date("l dS of F Y h:i:s A", $v_header[mtime]) . ")"); // ----- Change the file status $v_header[status] = "newer_exist"; // ----- Skip the extract $v_extraction_stopped = 1; $v_extract_file = 0; } } } } } else { if ($v_header[typeflag] == "5") { $v_dir_to_check = $v_header[filename]; } else { if (!strstr($v_header[filename], "/")) { $v_dir_to_check = ""; } else { $v_dir_to_check = dirname($v_header[filename]); } } if (($v_result = PclTarHandlerDirCheck($v_dir_to_check)) != 1) { TrFctMessage(__FILE__, __LINE__, 2, "Unable to create path for '{$v_header['filename']}'"); // ----- Change the file status $v_header[status] = "path_creation_fail"; // ----- Skip the extract $v_extraction_stopped = 1; $v_extract_file = 0; } } // ----- Do the real bytes extraction (if not a directory) if ($v_extract_file && $v_header[typeflag] != "5") { // ----- Open the destination file in write mode if (($v_dest_file = @fopen($v_header[filename], "wb")) == 0) { TrFctMessage(__FILE__, __LINE__, 2, "Error while opening '{$v_header['filename']}' in write binary mode"); // ----- Change the file status $v_header[status] = "write_error"; // ----- Jump to next file TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file"); if ($p_tar_mode == "tar") { fseek($v_tar, ftell($v_tar) + ceil($v_header[size] / 512) * 512); } else { gzseek($v_tar, gztell($v_tar) + ceil($v_header[size] / 512) * 512); } } else { TrFctMessage(__FILE__, __LINE__, 2, "Start extraction of '{$v_header['filename']}'"); // ----- Read data $n = floor($v_header[size] / 512); for ($i = 0; $i < $n; $i++) { TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number " . ($i + 1)); if ($p_tar_mode == "tar") { $v_content = fread($v_tar, 512); } else { $v_content = gzread($v_tar, 512); } fwrite($v_dest_file, $v_content, 512); } if ($v_header[size] % 512 != 0) { TrFctMessage(__FILE__, __LINE__, 3, "Read last " . $v_header[size] % 512 . " bytes in a 512 block"); if ($p_tar_mode == "tar") { $v_content = fread($v_tar, 512); } else { $v_content = gzread($v_tar, 512); } fwrite($v_dest_file, $v_content, $v_header[size] % 512); } // ----- Close the destination file fclose($v_dest_file); // ----- Change the file mode, mtime touch($v_header[filename], $v_header[mtime]); //chmod($v_header[filename], DecOct($v_header[mode])); } // ----- Check the file size clearstatcache(); if (filesize($v_header[filename]) != $v_header[size]) { // ----- Error log PclErrorLog(-7, "Extracted file '{$v_header['filename']}' does not have the correct file size '" . filesize($v_filename) . "' ('{$v_header['size']}' expected). Archive may be corrupted."); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Trace TrFctMessage(__FILE__, __LINE__, 2, "Extraction done"); } else { TrFctMessage(__FILE__, __LINE__, 2, "Extraction of file '{$v_header['filename']}' skipped."); // ----- Jump to next file TrFctMessage(__FILE__, __LINE__, 2, "Jump to next file"); if ($p_tar_mode == "tar") { fseek($v_tar, ftell($v_tar) + ceil($v_header[size] / 512) * 512); } else { gzseek($v_tar, gztell($v_tar) + ceil($v_header[size] / 512) * 512); } } // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }
/** * Reads in block $this->buffer_block_num of size self::BUFFER_SIZE from * the archive file * * @param string $buffer * @param bool $return_string * @return mixed whether successfully read in block or not */ function makeBuffer($buffer = "", $return_string = false) { if ($buffer == "") { if (!$this->checkFileHandle()) { return false; } $success = 1; $seek_pos = $this->buffer_block_num * self::BUFFER_SIZE; if ($this->compression == "gzip") { $success = gzseek($this->fh, $seek_pos); } if ($this->compression == "plain") { $success = fseek($this->fh, $seek_pos); } if ($success == -1 || !$this->checkFileHandle() || $this->checkEof()) { return false; } if (is_resource($this->buffer_fh)) { fclose($this->buffer_fh); } $padded_buffer_size = self::BUFFER_SIZE + 2 * self::MAX_RECORD_SIZE; switch ($this->compression) { case 'bzip2': $buffer = ""; while (strlen($buffer) < $padded_buffer_size) { while (!is_string($block = $this->bz2_iterator->nextBlock())) { if ($this->bz2_iterator->eof()) { break; } } $buffer .= $block; if ($this->bz2_iterator->eof()) { break; } } if ($buffer == "") { return false; } break; case 'gzip': $buffer = gzread($this->fh, $padded_buffer_size); break; case 'plain': $buffer = fread($this->fh, $padded_buffer_size); break; } } if ($return_string) { return $buffer; } file_put_contents($this->buffer_filename, $buffer); $this->buffer_fh = fopen($this->buffer_filename, "rb"); return true; }
function _jumpBlock($p_len = false) { if (is_resource($this->_dFile)) { if ($p_len === false) { $p_len = 1; } if ($this->_bCompress) { gzseek($this->_dFile, gztell($this->_dFile) + $p_len * 512); } else { fseek($this->_dFile, ftell($this->_dFile) + $p_len * 512); } } return true; }
function _seek($p_flen, $tell = 0) { if ($this->_nomf === TarLib::ARCHIVE_DYNAMIC) { $this->_memdat = substr($this->_memdat, 0, ($tell ? strlen($this->_memdat) : 0) + $p_flen); } elseif ($this->_comptype == TarLib::COMPRESS_GZIP) { @gzseek($this->_fp, ($tell ? @gztell($this->_fp) : 0) + $p_flen); } elseif ($this->_comptype == TarLib::COMPRESS_BZIP) { @fseek($this->_fp, ($tell ? @ftell($this->_fp) : 0) + $p_flen); } else { @fseek($this->_fp, ($tell ? @ftell($this->_fp) : 0) + $p_flen); } }
$microtime = explode(" ", microtime()); $starttime = $microtime[0] + $microtime[1]; } else { $starttime = time(); } if (file_exists($_GET["fn"] . ".zip")) { echo "<tr><td><div class=\"bold_left\">" . BI_IMPORTING_FILE . ":</div></td><td>" . basename($_GET["fn"]) . ".zip</td></tr>\n"; } else { echo "<tr><td><div class=\"bold_left\">" . BI_IMPORTING_FILE . ":</div></td><td>" . basename($_GET["fn"]) . "</td></tr>\n"; } echo "<tr><td><div class=\"bold_left\">" . BI_INTO_DB . ":</div></td><td>" . $_GET["dbn"] . "</td></tr>\n"; echo "<tr><td><div class=\"bold_left\">" . BI_SESSION_NO . ":</div></td><td>" . $_GET["sn"] . "</td></tr>\n"; echo "<tr><td><div class=\"bold_left\">" . BI_STARTING_LINE . ":</div></td><td>" . $_GET["start"] . "</td></tr>\n"; // start or continue the import process if (!isset($firstSession)) { if (gzseek($file, $_GET["foffset"]) != 0) { $error = "UNEXPECTED ERROR: Can't set gzip file pointer to offset: " . $_GET["foffset"]; } // execute sql queries if (!$error) { extract(PMBP_exec_sql($file, $con, $linespersession), EXTR_OVERWRITE); } // get the current file position if (!$error) { $foffset = gztell($file); if ($foffset === false) { $error = "UNEXPECTED ERROR: Can't read the file pointer offset"; } } } // clean up
function doSeek($offset, $whence) { if ($whence == SEEK_CUR) { $offset = gztell($this->File) + $offset; } else { if ($whence == SEEK_END) { eZDebug::writeError("Seeking from end is not supported for gzipped files", __METHOD__); return false; } } return @gzseek($this->File, $offset); }
function restore_file1($datefile) { global $strDataRestoreNoFile, $DMC, $strDataRestoreBad, $strDataRestoreSuccess, $data_path, $_SESSION; $filename = $data_path . "/" . $datefile; if (!file_exists($filename)) { $ActionMessage = "{$strDataRestoreNoFile}"; } else { $filesize = filesize($filename); $file_position = isset($HTTP_GET_VARS['pos']) ? $HTTP_GET_VARS['pos'] : 0; $errors = isset($HTTP_GET_VARS['ignore_errors']) ? 0 : 1; $buffer = ''; $inside_quote = 0; $quote_inside = ''; $started_query = 0; $data_buffer = ''; $last_char = "\n"; if (strpos($filename, ".zip") > 0) { $fp = gzopen($filename, 'rb'); gzseek($fp, $file_position); while (!gzeof($fp) || strlen($buffer)) { do { // Deals with the length of the buffer if (!strlen($buffer)) { $buffer .= gzread($fp, 1024); } // Fiddle around with the buffers $current_char = $buffer[0]; $buffer = substr($buffer, 1); if ($started_query) { $data_buffer .= $current_char; } elseif (preg_match("/[A-Za-z]/i", $current_char) && $last_char == "\n") { $started_query = 1; $data_buffer = $current_char; } else { $last_char = $current_char; } } while (!$started_query && (!gzeof($fp) || strlen($buffer))); if ($inside_quote && $current_char == $quote_inside && $last_char != '\\') { $inside_quote = 0; } elseif ($current_char == '\\' && $last_char == '\\') { $current_char = ''; } elseif (!$inside_quote && ($current_char == '"' || $current_char == '`' || $current_char == '\'')) { $inside_quote = 1; $quote_inside = $current_char; } elseif (!$inside_quote && $current_char == ';') { if (strpos($data_buffer, "TABLE IF EXISTS") > 0) { $name = substr($data_buffer, 21, strlen($data_buffer) - 22); } if (mysql_get_server_info() < 4.1) { $data_buffer = str_replace("ENGINE=MyISAM DEFAULT CHARSET=utf8", "TYPE=MyISAM", $data_buffer); } else { $data_buffer = str_replace("TYPE=MyISAM", "ENGINE=MyISAM DEFAULT CHARSET=utf8", $data_buffer); } $DMC->query($data_buffer, "T"); if ($DMC->error()) { $_SESSION['array_errorsql'][$DMC->error()] = $data_buffer; } $data_buffer = ''; $last_char = "\n"; $started_query = 0; } $last_char = $current_char; } gzclose($fp); } else { $fp = fopen($filename, 'rb'); fseek($fp, $file_position); while (!feof($fp) || strlen($buffer)) { do { // Deals with the length of the buffer if (!strlen($buffer)) { $buffer .= fread($fp, 1024); } // Fiddle around with the buffers $current_char = $buffer[0]; $buffer = substr($buffer, 1); if ($started_query) { $data_buffer .= $current_char; } elseif (preg_match("/[A-Za-z]/i", $current_char) && $last_char == "\n") { $started_query = 1; $data_buffer = $current_char; } else { $last_char = $current_char; } } while (!$started_query && (!feof($fp) || strlen($buffer))); if ($inside_quote && $current_char == $quote_inside && $last_char != '\\') { $inside_quote = 0; } elseif ($current_char == '\\' && $last_char == '\\') { $current_char = ''; } elseif (!$inside_quote && ($current_char == '"' || $current_char == '`' || $current_char == '\'')) { $inside_quote = 1; $quote_inside = $current_char; } elseif (!$inside_quote && $current_char == ';') { if (strpos($data_buffer, "TABLE IF EXISTS") > 0) { $name = substr($data_buffer, 21, strlen($data_buffer) - 22); } $data_buffer = preg_replace('/AUTO_INCREMENT=([0-9]+?) /is', '', $data_buffer); if (mysql_get_server_info() < 4.1) { $data_buffer = str_replace("ENGINE=MyISAM DEFAULT CHARSET=utf8", "TYPE=MyISAM", $data_buffer); } else { $data_buffer = str_replace("TYPE=MyISAM", "ENGINE=MyISAM DEFAULT CHARSET=utf8", $data_buffer); } $DMC->query($data_buffer, "T"); if ($DMC->error()) { $_SESSION['array_errorsql'][$DMC->error()] = $data_buffer; } $data_buffer = ''; $last_char = "\n"; $started_query = 0; } $last_char = $current_char; } fclose($fp); } } if (is_array($_SESSION['array_errorsql'])) { $ActionMessage = "{$datefile} ==> " . $strDataRestoreBad; } else { $ActionMessage = "{$datefile} ==> " . $strDataRestoreSuccess; } return $ActionMessage; }
function PclTarHandleUpdate($p_tarname, $p_file_list, &$p_list_detail, $p_tar_mode, $p_add_dir, $p_remove_dir) { TrFctStart(__FILE__, __LINE__, "PclTarHandleUpdate", "archive='{$p_tarname}', list, tar_mode={$p_tar_mode}"); $v_result = 1; $v_nb = 0; $v_found_list = array(); // ----- Look for regular tar file if ($p_tar_mode == "tar") { // ----- Open file TrFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode"); if (($v_tar = @fopen($p_tarname, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '{$p_tarname}' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Open a temporary file in write mode $v_temp_tarname = uniqid("pcltar-") . ".tmp"; TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file {$v_temp_tarname}"); if (($v_temp_tar = @fopen($v_temp_tarname, "wb")) == 0) { // ----- Close tar file fclose($v_tar); // ----- Error log PclErrorLog(-1, "Unable to open file '{$v_temp_tarname}' in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } } else { // ----- Open the file in read mode TrFctMessage(__FILE__, __LINE__, 3, "Open file in gzip binary read mode"); if (($v_tar = @gzopen($p_tarname, "rb")) == 0) { // ----- Error log PclErrorLog(-2, "Unable to open file '{$p_tarname}' in binary read mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Open a temporary file in write mode $v_temp_tarname = uniqid("pcltar-") . ".tmp"; TrFctMessage(__FILE__, __LINE__, 2, "Creating temporary archive file {$v_temp_tarname}"); if (($v_temp_tar = @gzopen($v_temp_tarname, "wb")) == 0) { // ----- Close tar file gzclose($v_tar); // ----- Error log PclErrorLog(-1, "Unable to open file '{$v_temp_tarname}' in binary write mode"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } } // ----- Prepare the list of files for ($i = 0; $i < sizeof($p_file_list); $i++) { // ----- Reset the found list $v_found_list[$i] = 0; // ----- Calculate the stored filename $v_stored_list[$i] = $p_file_list[$i]; if ($p_remove_dir != "") { if (substr($p_file_list[$i], -1) != '/') { $p_remove_dir .= "/"; } if (substr($p_file_list[$i], 0, strlen($p_remove_dir)) == $p_remove_dir) { $v_stored_list[$i] = substr($p_file_list[$i], strlen($p_remove_dir)); TrFctMessage(__FILE__, __LINE__, 3, "Remove path '{$p_remove_dir}' in file '{$p_file_list[$i]}' = '{$v_stored_list[$i]}'"); } } if ($p_add_dir != "") { if (substr($p_add_dir, -1) == "/") { $v_stored_list[$i] = $p_add_dir . $v_stored_list[$i]; } else { $v_stored_list[$i] = $p_add_dir . "/" . $v_stored_list[$i]; } TrFctMessage(__FILE__, __LINE__, 3, "Add path '{$p_add_dir}' in file '{$p_file_list[$i]}' = '{$v_stored_list[$i]}'"); } $v_stored_list[$i] = PclTarHandlePathReduction($v_stored_list[$i]); TrFctMessage(__FILE__, __LINE__, 3, "After reduction '{$v_stored_list[$i]}'"); } // ----- Update file cache clearstatcache(); // ----- Read the blocks while (!($v_end_of_file = $p_tar_mode == "tar" ? feof($v_tar) : gzeof($v_tar))) { TrFctMessage(__FILE__, __LINE__, 3, "Looking for next header ..."); // ----- Clear cache of file infos clearstatcache(); // ----- Reset current found filename $v_current_filename = ""; // ----- Reset delete tag $v_delete_file = FALSE; // ----- Read the first 512 block header if ($p_tar_mode == "tar") { $v_binary_data = fread($v_tar, 512); } else { $v_binary_data = gzread($v_tar, 512); } // ----- Read the header properties if (($v_result = PclTarHandleReadHeader($v_binary_data, $v_header)) != 1) { // ----- Close the archive file if ($p_tar_mode == "tar") { fclose($v_tar); fclose($v_temp_tar); } else { gzclose($v_tar); gzclose($v_temp_tar); } @unlink($v_temp_tarname); // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } // ----- Look for empty blocks to skip if ($v_header[filename] == "") { TrFctMessage(__FILE__, __LINE__, 2, "Empty block found. End of archive ?"); continue; } TrFctMessage(__FILE__, __LINE__, 2, "Found file '{$v_header['filename']}', size '{$v_header['size']}'"); // ----- Look for filenames to update for ($i = 0, $v_update_file = FALSE, $v_found_file = FALSE; $i < sizeof($v_stored_list) && !$v_update_file; $i++) { TrFctMessage(__FILE__, __LINE__, 4, "Compare with file '{$v_stored_list[$i]}'"); // ----- Compare the file names if ($v_stored_list[$i] == $v_header[filename]) { TrFctMessage(__FILE__, __LINE__, 3, "File '{$v_stored_list[$i]}' is present in archive"); TrFctMessage(__FILE__, __LINE__, 3, "File '{$v_stored_list[$i]}' mtime=" . filemtime($p_file_list[$i]) . " " . date("l dS of F Y h:i:s A", filemtime($p_file_list[$i]))); TrFctMessage(__FILE__, __LINE__, 3, "Archived mtime=" . $v_header[mtime] . " " . date("l dS of F Y h:i:s A", $v_header[mtime])); // ----- Store found informations $v_found_file = TRUE; $v_current_filename = $p_file_list[$i]; // ----- Look if the file need to be updated if (filemtime($p_file_list[$i]) > $v_header[mtime]) { TrFctMessage(__FILE__, __LINE__, 3, "File '{$p_file_list[$i]}' need to be updated"); $v_update_file = TRUE; } else { TrFctMessage(__FILE__, __LINE__, 3, "File '{$p_file_list[$i]}' does not need to be updated"); $v_update_file = FALSE; } // ----- Flag the name in order not to add the file at the end $v_found_list[$i] = 1; } else { TrFctMessage(__FILE__, __LINE__, 4, "File '{$p_file_list[$i]}' is not '{$v_header['filename']}'"); } } // ----- Copy files that do not need to be updated if (!$v_update_file) { TrFctMessage(__FILE__, __LINE__, 2, "Keep file '{$v_header['filename']}'"); // ----- Write the file header if ($p_tar_mode == "tar") { fputs($v_temp_tar, $v_binary_data, 512); } else { gzputs($v_temp_tar, $v_binary_data, 512); } // ----- Write the file data $n = ceil($v_header[size] / 512); for ($j = 0; $j < $n; $j++) { TrFctMessage(__FILE__, __LINE__, 3, "Read complete 512 bytes block number " . ($j + 1)); if ($p_tar_mode == "tar") { $v_content = fread($v_tar, 512); fwrite($v_temp_tar, $v_content, 512); } else { $v_content = gzread($v_tar, 512); gzwrite($v_temp_tar, $v_content, 512); } } // ----- File name and properties are logged if listing mode or file is extracted TrFctMessage(__FILE__, __LINE__, 2, "Memorize info about file '{$v_header['filename']}'"); // ----- Add the array describing the file into the list $p_list_detail[$v_nb] = $v_header; $p_list_detail[$v_nb][status] = $v_found_file ? "not_updated" : "ok"; // ----- Increment $v_nb++; } else { // ----- Trace TrFctMessage(__FILE__, __LINE__, 2, "Start update of file '{$v_current_filename}'"); // ----- Store the old file size $v_old_size = $v_header[size]; // ----- Add the file if (($v_result = PclTarHandleAddFile($v_temp_tar, $v_current_filename, $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1) { // ----- Close the tarfile if ($p_tar_mode == "tar") { fclose($v_tar); fclose($v_temp_tar); } else { gzclose($v_tar); gzclose($v_temp_tar); } @unlink($p_temp_tarname); // ----- Return status TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } // ----- Trace TrFctMessage(__FILE__, __LINE__, 2, "Skip old file '{$v_header['filename']}'"); // ----- Jump to next file if ($p_tar_mode == "tar") { fseek($v_tar, ftell($v_tar) + ceil($v_old_size / 512) * 512); } else { gzseek($v_tar, gztell($v_tar) + ceil($v_old_size / 512) * 512); } // ----- Add the array describing the file into the list $p_list_detail[$v_nb] = $v_header; $p_list_detail[$v_nb][status] = "updated"; // ----- Increment $v_nb++; } // ----- Look for end of file if ($p_tar_mode == "tar") { $v_end_of_file = feof($v_tar); } else { $v_end_of_file = gzeof($v_tar); } } // ----- Look for files that does not exists in the archive and need to be added for ($i = 0; $i < sizeof($p_file_list); $i++) { // ----- Look if file not found in the archive if (!$v_found_list[$i]) { TrFctMessage(__FILE__, __LINE__, 3, "File '{$p_file_list[$i]}' need to be added"); // ----- Add the file if (($v_result = PclTarHandleAddFile($v_temp_tar, $p_file_list[$i], $p_tar_mode, $v_header, $p_add_dir, $p_remove_dir)) != 1) { // ----- Close the tarfile if ($p_tar_mode == "tar") { fclose($v_tar); fclose($v_temp_tar); } else { gzclose($v_tar); gzclose($v_temp_tar); } @unlink($p_temp_tarname); // ----- Return status TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; } // ----- Add the array describing the file into the list $p_list_detail[$v_nb] = $v_header; $p_list_detail[$v_nb][status] = "added"; // ----- Increment $v_nb++; } else { TrFctMessage(__FILE__, __LINE__, 3, "File '{$p_file_list[$i]}' was already updated if needed"); } } // ----- Write the last empty buffer PclTarHandleFooter($v_temp_tar, $p_tar_mode); // ----- Close the tarfile if ($p_tar_mode == "tar") { fclose($v_tar); fclose($v_temp_tar); } else { gzclose($v_tar); gzclose($v_temp_tar); } // ----- Unlink tar file if (!@unlink($p_tarname)) { // ----- Error log PclErrorLog(-11, "Error while deleting archive name {$p_tarname}"); } // ----- Rename tar file if (!@rename($v_temp_tarname, $p_tarname)) { // ----- Error log PclErrorLog(-12, "Error while renaming temporary file {$v_temp_tarname} to archive name {$p_tarname}"); // ----- Return TrFctEnd(__FILE__, __LINE__, PclErrorCode(), PclErrorString()); return PclErrorCode(); } // ----- Return TrFctEnd(__FILE__, __LINE__, $v_result); return $v_result; }
function Skip($Block) { if ($Block == 0) { return true; } $this->Block += $Block; $toSkip = $Block * 512; if (self::strlen($this->Buffer) > $toSkip) { $this->Buffer = self::substr($this->Buffer, $toSkip); return true; } $this->Buffer = ''; $NewPos = $this->Block * 512; if ($ArchiveSize = $this->getDataSize($file = self::getFirstName($this->file))) { while ($NewPos > $ArchiveSize) { $file = $this->getNextName($file); $NewPos -= $ArchiveSize; } } if ($file != $this->file) { $this->close(); if (!$this->open($file, $this->mode)) { return false; } } if (0 === ($this->gzip ? gzseek($this->res, $NewPos) : fseek($this->res, $NewPos))) { return true; } return $this->Error('File seek error (file: ' . $this->file . ', position: ' . $NewPos . ')'); }
<?php $f = "gzseek_variation1.gz"; $h = gzopen($f, 'w'); $str1 = "This is the first line."; $str2 = "This is the second line."; gzwrite($h, $str1); //seek forwards 20 bytes. gzseek($h, strlen($str1) + 20); gzwrite($h, $str2); gzclose($h); $h = gzopen($f, 'r'); echo gzread($h, strlen($str1)) . "\n"; var_dump(bin2hex(gzread($h, 20))); echo gzread($h, strlen($str2)) . "\n"; gzclose($h); unlink($f); ?> ===DONE===
/** Restore a mysql dump * * @param $DB DB object * @param $dumpFile dump file * @param $duree max delay before refresh **/ function restoreMySqlDump($DB, $dumpFile, $duree) { global $DB, $TPSCOUR, $offset, $cpt; // $dumpFile, fichier source // $duree=timeout pour changement de page (-1 = aucun) // Desactivation pour empecher les addslashes au niveau de la creation des tables // En plus, au niveau du dump on considere qu'on est bon // set_magic_quotes_runtime(0); if (!file_exists($dumpFile)) { echo sprintf(__('File %s not found.'), $dumpFile) . "<br>"; return false; } if (substr($dumpFile, -2) == "gz") { $fileHandle = gzopen($dumpFile, "rb"); } else { $fileHandle = fopen($dumpFile, "rb"); } if (!$fileHandle) { //TRASN: %s is the name of the file echo sprintf(__('Unauthorized access to the file %s'), $dumpFile) . "<br>"; return false; } if ($offset != 0) { if (substr($dumpFile, -2) == "gz") { if (gzseek($fileHandle, $offset, SEEK_SET) != 0) { //erreur //TRANS: %s is the number of the byte printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0)); echo "<br>"; return false; } } else { if (fseek($fileHandle, $offset, SEEK_SET) != 0) { //erreur //TRANS: %s is the number of the byte printf(__("Unable to find the byte %s"), Html::formatNumber($offset, false, 0)); echo "<br>"; return false; } } Html::glpi_flush(); } $formattedQuery = ""; if (substr($dumpFile, -2) == "gz") { while (!gzeof($fileHandle)) { current_time(); if ($duree > 0 && $TPSCOUR >= $duree) { //on atteint la fin du temps imparti return true; } // specify read length to be able to read long lines $buffer = gzgets($fileHandle, 102400); // do not strip comments due to problems when # in begin of a data line $formattedQuery .= $buffer; if (substr(rtrim($formattedQuery), -1) == ";") { // Do not use the $DB->query if ($DB->query($formattedQuery)) { //if no success continue to concatenate $offset = gztell($fileHandle); $formattedQuery = ""; $cpt++; } } } } else { while (!feof($fileHandle)) { current_time(); if ($duree > 0 && $TPSCOUR >= $duree) { //on atteint la fin du temps imparti return true; } // specify read length to be able to read long lines $buffer = fgets($fileHandle, 102400); // do not strip comments due to problems when # in begin of a data line $formattedQuery .= $buffer; if (substr(rtrim($formattedQuery), -1) == ";") { // Do not use the $DB->query if ($DB->query($formattedQuery)) { //if no success continue to concatenate $offset = ftell($fileHandle); $formattedQuery = ""; $cpt++; } } } } if ($DB->error) { echo "<hr>"; //TRANS: %s is the SQL query which generates the error printf(__("SQL error starting from %s"), "[{$formattedQuery}]"); echo "<br>" . $DB->error() . "<hr>"; } if (substr($dumpFile, -2) == "gz") { gzclose($fileHandle); } else { fclose($fileHandle); } $offset = -1; return true; }
<?php $f = "temp3.txt.gz"; $h = gzopen($f, 'w'); $str1 = "This is the first line."; $str2 = "This is the second line."; gzwrite($h, $str1); echo "tell="; var_dump(gztell($h)); //seek to the end which is not sensible of course. echo "move to the end of the file\n"; var_dump(gzseek($h, 0, SEEK_END)); echo "tell="; var_dump(gztell($h)); gzwrite($h, $str2); echo "tell="; var_dump(gztell($h)); gzclose($h); echo "\nreading the output file\n"; $h = gzopen($f, 'r'); gzpassthru($h); gzclose($h); echo "\n"; unlink($f); ?> ===DONE===
<?php $f = "temp3.txt.gz"; $h = gzopen($f, 'w'); $str1 = "This is the first line."; $str2 = "This is the second line."; gzwrite($h, $str1); echo "tell=" . gztell($h) . "\n"; //seek forwards 20 bytes. gzseek($h, 20, SEEK_CUR); echo "tell=" . gztell($h) . "\n"; gzwrite($h, $str2); echo "tell=" . gztell($h) . "\n"; gzclose($h); echo "\nreading the output file\n"; $h = gzopen($f, 'r'); echo gzread($h, strlen($str1)) . "\n"; var_dump(bin2hex(gzread($h, 20))); echo gzread($h, strlen($str2)) . "\n"; gzclose($h); unlink($f); ?> ===DONE===
if (!$error) { skin_open(); if (TESTMODE) { // echo ("<p class=\"centr\">TEST MODE ENABLED</p>\n"); // echo ("<p class=\"centr\">Processing file: <b>".$curfilename."</b></p>\n"); // echo ("<p class=\"smlcentr\">Starting from line: ".$_REQUEST["start"]."</p>\n"); skin_close(); } } // Check $_REQUEST["foffset"] upon $filesize (can't do it on gzipped files) if (!$error && !$gzipmode && $_REQUEST["foffset"] > $filesize) { echo "<p class=\"error\">UNEXPECTED: Can't set file pointer behind the end of file</p>\n"; $error = true; } // Set file pointer to $_REQUEST["foffset"] if (!$error && (!$gzipmode && fseek($file, $_REQUEST["foffset"]) != 0 || $gzipmode && gzseek($file, $_REQUEST["foffset"]) != 0)) { echo "<p class=\"error\">UNEXPECTED: Can't set file pointer to offset: " . $_REQUEST["foffset"] . "</p>\n"; $error = true; } // Start processing queries from $file if (!$error) { $query = ""; $queries = 0; $totalqueries = $_REQUEST["totalqueries"]; $linenumber = $_REQUEST["start"]; $querylines = 0; $inparents = false; // Stay processing as long as the $linespersession is not reached or the query is still incomplete while ($linenumber < $_REQUEST["start"] + $linespersession || $query != "") { // Read the whole next line $dumpline = "";
public function import($start) { //还原数据 $db = M(); if ($this->config['compress']) { $gz = gzopen($this->file[1], 'r'); $size = 0; } else { $size = filesize($this->file[1]); $gz = fopen($this->file[1], 'r'); } $sql = ''; if ($start) { $this->config['compress'] ? gzseek($gz, $start) : fseek($gz, $start); } for ($i = 0; $i < 1000; $i++) { $sql .= $this->config['compress'] ? gzgets($gz) : fgets($gz); if (preg_match('/.*;$/', trim($sql))) { if (false !== $db->query($sql)) { $start += strlen($sql); } else { return false; } $sql = ''; } elseif ($this->config['compress'] ? gzeof($gz) : feof($gz)) { return 0; } } return array($start, $size); }