Skip to content
This repository was archived by the owner on Jan 2, 2019. It is now read-only.

Excel2007 disk cache #353

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions Classes/PHPExcel/Shared/TempFileTracker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* PHPExcel
*
* Copyright (c) 2006 - 2014 PHPExcel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category PHPExcel
* @package PHPExcel_Shared_TempFileTracker
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
* @version ##VERSION##, ##DATE##
*/

/**
* PHPExcel_Shared_TempFileTracker
*
* @category PHPExcel
* @package PHPExcel_Shared_TempFileTracker
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Shared_TempFileTracker
{
/**
* List of created temporary files
*
* @var array
*/
private $_files;

function __construct ()
{
$this->_files = array ();
}

function __destruct ()
{
$this->removeAllFiles ();
}

/**
* Register a new temporary file.
*
* @param string $filename
*/
public function registerFile ($filename)
{
$this->_files[] = $filename;
}

/**
* Generate a new temporary file name, and register it.
*
* @param string $prefix the new filename prefix.
* @return string the new temporary filename.
*/
public function getNewFile ($prefix)
{
$filename = @tempnam (PHPExcel_Shared_File::sys_get_temp_dir(), $prefix);
$this->registerFile ($filename);
return $filename;
}

/**
* Remove all the registered files.
*/
public function removeAllFiles ()
{
foreach ($this->_files as $file)
unlink ($file);
$this->_files = array ();
}
}

?>
80 changes: 57 additions & 23 deletions Classes/PHPExcel/Shared/XMLWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,36 +42,33 @@
* @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
*/
class PHPExcel_Shared_XMLWriter extends XMLWriter {
/** Temporary storage method */
const STORAGE_MEMORY = 1;
const STORAGE_DISK = 2;
const FLUSH_FREQUENCY = 100;

/**
* Temporary filename
*
* @var string
*/
private $_tempFileName = '';
private $_filename = '';

private $counter;

/**
* Create a new PHPExcel_Shared_XMLWriter instance
*
* @param int $pTemporaryStorage Temporary storage location
* @param string $pTemporaryStorageFolder Temporary storage folder
* @param string $filename Temporary storage filename (empty to use memory)
*/
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageFolder = NULL) {
public function __construct($filename = NULL) {
// Open temporary storage
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
if (!$filename) {
$this->openMemory();
} else {
// Create temporary filename
if ($pTemporaryStorageFolder === NULL)
$pTemporaryStorageFolder = PHPExcel_Shared_File::sys_get_temp_dir();
$this->_tempFileName = @tempnam($pTemporaryStorageFolder, 'xml');
$this->_filename = $filename;

// Open storage
if ($this->openUri($this->_tempFileName) === false) {
if ($this->openUri($this->_filename) === false) {
// Fallback to memory...
$this->_filename = '';
$this->openMemory();
}
}
Expand All @@ -80,16 +77,44 @@ public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTempora
if (DEBUGMODE_ENABLED) {
$this->setIndent(true);
}
$this->counter = self::FLUSH_FREQUENCY;
}

/**
* Destructor
*/
public function __destruct() {
// Unlink temporary files
if ($this->_tempFileName != '') {
@unlink($this->_tempFileName);
}
public function flush($empty=true)
{
parent::flush($empty);
$this->counter = self::FLUSH_FREQUENCY;
}

private function flushIfNecessary()
{
if ($this->_filename == '')
return;

--$this->counter;
if ($this->counter <= 0)
$this->flush();
}

public function endElement()
{
$ret = parent::endElement();
$this->flushIfNecessary();
return $ret;
}

public function writeElement($name, $content=null)
{
$ret = parent::writeElement($name, $content);
$this->flushIfNecessary();
return $ret;
}

public function writeAttribute($name, $value)
{
$ret = parent::writeAttribute($name, $value);
$this->flushIfNecessary();
return $ret;
}

/**
Expand All @@ -98,14 +123,23 @@ public function __destruct() {
* @return $data
*/
public function getData() {
if ($this->_tempFileName == '') {
if ($this->_filename == '') {
return $this->outputMemory(true);
} else {
$this->flush();
return file_get_contents($this->_tempFileName);
return file_get_contents($this->_filename);
}
}

/**
* Get the temporary file name.
*
* @return filename;
*/
public function getFileName() {
return $this->_filename;
}

/**
* Fallback method for writeRaw, introduced in PHP 5.2
*
Expand Down
24 changes: 24 additions & 0 deletions Classes/PHPExcel/Shared/ZipArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,30 @@ public function close()
{
}

/**
* Add a new file to the zip archive.
*
* @param string $filename The path to the file to add to the zip archive
* @param string $localname Directory/Name of the file to add to the zip archive
*/
public function addFile($filename, $localname=null)
{
if (is_null ($localname))
$localname = $filename;

$filenameParts = pathinfo($localname);
copy($filename, $this->_tempDir.'/'.$filenameParts["basename"]);

$res = $this->_zip->add($this->_tempDir.'/'.$filenameParts["basename"],
PCLZIP_OPT_REMOVE_PATH, $this->_tempDir,
PCLZIP_OPT_ADD_PATH, $filenameParts["dirname"]
);
if ($res == 0) {
throw new PHPExcel_Writer_Exception("Error zipping files : " . $this->_zip->errorInfo(true));
}

unlink($this->_tempDir.'/'.$filenameParts["basename"]);
}

/**
* Add a new file to the zip archive from a string of raw data.
Expand Down
Loading