You are here

function PclZip::privAddFileUsingTempFile in Loft Data Grids 6.2

Same name and namespace in other branches
  1. 7.2 vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/PCLZip/pclzip.lib.php \PclZip::privAddFileUsingTempFile()
1 call to PclZip::privAddFileUsingTempFile()
PclZip::privAddFile in vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/PCLZip/pclzip.lib.php

File

vendor/phpoffice/phpexcel/Classes/PHPExcel/Shared/PCLZip/pclzip.lib.php, line 2798

Class

PclZip

Code

function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options) {
  $v_result = PCLZIP_ERR_NO_ERROR;

  // ----- Working variable
  $p_filename = $p_filedescr['filename'];

  // ----- Open the source file
  if (($v_file = @fopen($p_filename, "rb")) == 0) {
    PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '{$p_filename}' in binary read mode");
    return PclZip::errorCode();
  }

  // ----- Creates a compressed temporary file
  $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.gz';
  if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
    fclose($v_file);
    PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary write mode');
    return PclZip::errorCode();
  }

  // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  $v_size = filesize($p_filename);
  while ($v_size != 0) {
    $v_read_size = $v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE;
    $v_buffer = @fread($v_file, $v_read_size);

    //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
    @gzputs($v_file_compressed, $v_buffer, $v_read_size);
    $v_size -= $v_read_size;
  }

  // ----- Close the file
  @fclose($v_file);
  @gzclose($v_file_compressed);

  // ----- Check the minimum file size
  if (filesize($v_gzip_temp_name) < 18) {
    PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \'' . $v_gzip_temp_name . '\' has invalid filesize - should be minimum 18 bytes');
    return PclZip::errorCode();
  }

  // ----- Extract the compressed attributes
  if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
    PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
    return PclZip::errorCode();
  }

  // ----- Read the gzip file header
  $v_binary_data = @fread($v_file_compressed, 10);
  $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);

  // ----- Check some parameters
  $v_data_header['os'] = bin2hex($v_data_header['os']);

  // ----- Read the gzip file footer
  @fseek($v_file_compressed, filesize($v_gzip_temp_name) - 8);
  $v_binary_data = @fread($v_file_compressed, 8);
  $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);

  // ----- Set the attributes
  $p_header['compression'] = ord($v_data_header['cm']);

  //$p_header['mtime'] = $v_data_header['mtime'];
  $p_header['crc'] = $v_data_footer['crc'];
  $p_header['compressed_size'] = filesize($v_gzip_temp_name) - 18;

  // ----- Close the file
  @fclose($v_file_compressed);

  // ----- Call the header generation
  if (($v_result = $this
    ->privWriteFileHeader($p_header)) != 1) {
    return $v_result;
  }

  // ----- Add the compressed data
  if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
    PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \'' . $v_gzip_temp_name . '\' in binary read mode');
    return PclZip::errorCode();
  }

  // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
  fseek($v_file_compressed, 10);
  $v_size = $p_header['compressed_size'];
  while ($v_size != 0) {
    $v_read_size = $v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE;
    $v_buffer = @fread($v_file_compressed, $v_read_size);

    //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
    @fwrite($this->zip_fd, $v_buffer, $v_read_size);
    $v_size -= $v_read_size;
  }

  // ----- Close the file
  @fclose($v_file_compressed);

  // ----- Unlink the temporary file
  @unlink($v_gzip_temp_name);

  // ----- Return
  return $v_result;
}