• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/classes/general/zip.php
  • Класс: CZip
  • Вызов: CZip::_addData
private function _addData($arFilesList, &$arResultList, &$arParams)
{
	$addDir = $arParams['add_path'];
	$removeDir = $arParams['remove_path'];
	$removeAllDir = $arParams['remove_all_path'];

	if ((!is_file($this->io->GetPhysicalName($this->zipname))) || (filesize($this->io->GetPhysicalName($this->zipname)) == 0))
	{
		$res = $this->_createArchive($arFilesList, $arResultList, $arParams);
		return $res;
	}

	if (($res = $this->_openFile('rb')) != 1)
	{
		return $res;
	}

	$arCentralDirInfo = [];
	if (($res = $this->_readEndCentralDir($arCentralDirInfo)) != 1)
	{
		$this->_closeFile();
		return $res;
	}

	@rewind($this->zipfile);

	$zipname_tmp = GetDirPath($this->zipname) . uniqid('ziparc') . '.tmp';

	if (($zipfile_tmp = @fopen($this->io->GetPhysicalName($zipname_tmp), 'wb')) == 0)
	{
		$this->_closeFile();
		$this->_errorLog("ERR_READ_TMP", str_replace("#FILE_NAME#", removeDocRoot($zipname_tmp), GetMessage("MAIN_ZIP_ERR_READ_TMP")));
		return $this->arErrors;
	}

	//copy files from archive to the tmp file
	$size = $arCentralDirInfo['offset'];
	while ($size != 0)
	{
		$length = ($size < self::ReadBlockSize ? $size : self::ReadBlockSize);

		$buffer = fread($this->zipfile, $length);

		@fwrite($zipfile_tmp, $buffer, $length);
		$size -= $length;
	}

	//changing file handles to use methods on the temporary file, not the real archive
	$tmp_id = $this->zipfile;
	$this->zipfile = $zipfile_tmp;
	$zipfile_tmp = $tmp_id;

	$arHeaders = [];
	if (($res = $this->_addFileList($arFilesList, $arHeaders,
			$addDir, $removeDir,
			$removeAllDir, $arParams)) != 1)
	{
		fclose($zipfile_tmp);
		$this->_closeFile();
		@unlink($this->io->GetPhysicalName($zipname_tmp));

		return $res;
	}

	//save central dir offset
	$offset = @ftell($this->zipfile);

	//copy file headers block from the old archive
	$size = $arCentralDirInfo['size'];
	while ($size != 0)
	{
		$length = ($size < self::ReadBlockSize ? $size : self::ReadBlockSize);
		$buffer = @fread($zipfile_tmp, $length);
		@fwrite($this->zipfile, $buffer, $length);
		$size -= $length;
	}

	//write central dir files header
	for ($i = 0, $counter = 0; $i < sizeof($arHeaders); $i++)
	{
		//add the file header
		if ($arHeaders[$i]['status'] == 'ok')
		{
			if (($res = $this->_writeCentralFileHeader($arHeaders[$i])) != 1)
			{
				fclose($zipfile_tmp);
				$this->_closeFile();
				@unlink($this->io->GetPhysicalName($zipname_tmp));
				return $res;
			}
			$counter++;
		}

		$this->_convertHeader2FileInfo($arHeaders[$i], $arResultList[$i]);
	}

	$zip_comment = '';

	//size of the central header
	$size = @ftell($this->zipfile) - $offset;

	//make central dir footer
	if (($res = $this->_writeCentralHeader($counter + $arCentralDirInfo['entries'], $size, $offset, $zip_comment)) != 1)
	{
		//reset files list
		unset($arHeaders);
		return $res;
	}

	//change back file handler
	$tmp_id = $this->zipfile;
	$this->zipfile = $zipfile_tmp;
	$zipfile_tmp = $tmp_id;

	$this->_closeFile();
	@fclose($zipfile_tmp);
	@unlink($this->io->GetPhysicalName($this->zipname));
	//possibly test the result @rename($zipname_tmp, $this->zipname);
	$this->_renameTmpFile($zipname_tmp, $this->zipname);

	return $res;
}