Merge branch 'zip' into develop
This commit is contained in:
commit
d64fc98a51
|
|
@ -49,6 +49,7 @@ This release marked the change of PHPWord license from LGPL 2.1 to LGPL 3; new r
|
||||||
- Writer: Refactor writer parts using composite pattern - @ivanlanin
|
- Writer: Refactor writer parts using composite pattern - @ivanlanin
|
||||||
- Docs: Show code quality and test code coverage badge on README
|
- Docs: Show code quality and test code coverage badge on README
|
||||||
- Style: Change behaviour of `set...` function of boolean properties; when none is defined, assumed true - @ivanlanin
|
- Style: Change behaviour of `set...` function of boolean properties; when none is defined, assumed true - @ivanlanin
|
||||||
|
- Shared: Unify PHP ZipArchive and PCLZip features into PhpWord ZipArchive - @ivanlanin
|
||||||
|
|
||||||
## 0.10.0 - 4 May 2014
|
## 0.10.0 - 4 May 2014
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ namespace PhpOffice\PhpWord\Element;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Exception\InvalidImageException;
|
use PhpOffice\PhpWord\Exception\InvalidImageException;
|
||||||
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
|
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
use PhpOffice\PhpWord\Style\Image as ImageStyle;
|
use PhpOffice\PhpWord\Style\Image as ImageStyle;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -348,8 +348,7 @@ class Image extends AbstractElement
|
||||||
list($zipFilename, $imageFilename) = explode('#', $source);
|
list($zipFilename, $imageFilename) = explode('#', $source);
|
||||||
$tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
|
$tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
|
||||||
|
|
||||||
$zipClass = Settings::getZipClass();
|
$zip = new ZipArchive();
|
||||||
$zip = new $zipClass();
|
|
||||||
if ($zip->open($zipFilename) !== false) {
|
if ($zip->open($zipFilename) !== false) {
|
||||||
if ($zip->locateName($imageFilename)) {
|
if ($zip->locateName($imageFilename)) {
|
||||||
$imageContent = $zip->getFromName($imageFilename);
|
$imageContent = $zip->getFromName($imageFilename);
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
namespace PhpOffice\PhpWord\Reader;
|
namespace PhpOffice\PhpWord\Reader;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
use PhpOffice\PhpWord\Shared\XMLReader;
|
use PhpOffice\PhpWord\Shared\XMLReader;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -109,8 +109,7 @@ class Word2007 extends AbstractReader implements ReaderInterface
|
||||||
|
|
||||||
// word/_rels/*.xml.rels
|
// word/_rels/*.xml.rels
|
||||||
$wordRelsPath = 'word/_rels/';
|
$wordRelsPath = 'word/_rels/';
|
||||||
$zipClass = Settings::getZipClass();
|
$zip = new ZipArchive();
|
||||||
$zip = new $zipClass();
|
|
||||||
if ($zip->open($docFile) === true) {
|
if ($zip->open($docFile) === true) {
|
||||||
for ($i = 0; $i < $zip->numFiles; $i++) {
|
for ($i = 0; $i < $zip->numFiles; $i++) {
|
||||||
$xmlFile = $zip->getNameIndex($i);
|
$xmlFile = $zip->getNameIndex($i);
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
namespace PhpOffice\PhpWord\Shared;
|
namespace PhpOffice\PhpWord\Shared;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Exception\Exception;
|
use PhpOffice\PhpWord\Exception\Exception;
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XML Reader wrapper
|
* XML Reader wrapper
|
||||||
|
|
@ -47,7 +47,6 @@ class XMLReader
|
||||||
* @param string $zipFile
|
* @param string $zipFile
|
||||||
* @param string $xmlFile
|
* @param string $xmlFile
|
||||||
* @return \DOMDocument|false
|
* @return \DOMDocument|false
|
||||||
* @throws \PhpOffice\PhpWord\Exception\Exception
|
|
||||||
*/
|
*/
|
||||||
public function getDomFromZip($zipFile, $xmlFile)
|
public function getDomFromZip($zipFile, $xmlFile)
|
||||||
{
|
{
|
||||||
|
|
@ -55,12 +54,8 @@ class XMLReader
|
||||||
throw new Exception('Cannot find archive file.');
|
throw new Exception('Cannot find archive file.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$zipClass = Settings::getZipClass();
|
$zip = new ZipArchive();
|
||||||
$zip = new $zipClass();
|
$zip->open($zipFile);
|
||||||
$canOpen = $zip->open($zipFile);
|
|
||||||
if ($canOpen === false) {
|
|
||||||
throw new Exception('Cannot open archive file.');
|
|
||||||
}
|
|
||||||
$contents = $zip->getFromName($xmlFile);
|
$contents = $zip->getFromName($xmlFile);
|
||||||
$zip->close();
|
$zip->close();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,6 @@ namespace PhpOffice\PhpWord\Shared;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Settings;
|
||||||
|
|
||||||
// @codeCoverageIgnoreStart
|
|
||||||
if (!defined('DATE_W3C')) {
|
|
||||||
define('DATE_W3C', 'Y-m-d\TH:i:sP');
|
|
||||||
}
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* XMLWriter wrapper
|
* XMLWriter wrapper
|
||||||
*
|
*
|
||||||
|
|
@ -64,6 +58,11 @@ class XMLWriter
|
||||||
*/
|
*/
|
||||||
public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder = './')
|
public function __construct($tempLocation = self::STORAGE_MEMORY, $tempFolder = './')
|
||||||
{
|
{
|
||||||
|
// Define date format
|
||||||
|
if (!defined('DATE_W3C')) {
|
||||||
|
define('DATE_W3C', 'Y-m-d\TH:i:sP');
|
||||||
|
}
|
||||||
|
|
||||||
// Create internal XMLWriter
|
// Create internal XMLWriter
|
||||||
$this->xmlWriter = new \XMLWriter();
|
$this->xmlWriter = new \XMLWriter();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,32 +17,46 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Shared;
|
namespace PhpOffice\PhpWord\Shared;
|
||||||
|
|
||||||
// PCLZIP needs the temp path to end in a back slash
|
use PhpOffice\PhpWord\Exception\Exception;
|
||||||
// @codeCoverageIgnoreStart
|
use PhpOffice\PhpWord\Settings;
|
||||||
if (!defined('PCLZIP_TEMPORARY_DIR')) {
|
|
||||||
define('PCLZIP_TEMPORARY_DIR', sys_get_temp_dir() . '/');
|
|
||||||
}
|
|
||||||
require_once 'PCLZip/pclzip.lib.php';
|
|
||||||
// @codeCoverageIgnoreEnd
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PCLZip wrapper
|
* ZipArchive wrapper
|
||||||
*
|
*
|
||||||
|
* Wraps zip archive functionality of PHP ZipArchive and PCLZip. PHP ZipArchive
|
||||||
|
* properties and methods are bypassed and used as the model for the PCLZip
|
||||||
|
* emulation. Only needed PHP ZipArchive features are implemented.
|
||||||
|
*
|
||||||
|
* @method bool addFile(string $filename, string $localname = null)
|
||||||
|
* @method bool addFromString(string $localname, string $contents)
|
||||||
|
* @method bool close()
|
||||||
|
* @method bool extractTo(string $destination, mixed $entries = null)
|
||||||
|
* @method string getFromName(string $name)
|
||||||
|
* @method string getNameIndex(int $index)
|
||||||
|
* @method int locateName(string $name)
|
||||||
|
* @method bool open(string $filename, int $flags = null)
|
||||||
* @since 0.10.0
|
* @since 0.10.0
|
||||||
*/
|
*/
|
||||||
class ZipArchive
|
class ZipArchive
|
||||||
{
|
{
|
||||||
/** constants */
|
/** @const int Flags for open method */
|
||||||
const OVERWRITE = 'OVERWRITE';
|
const CREATE = 1; // Emulate \ZipArchive::CREATE
|
||||||
const CREATE = 'CREATE';
|
const OVERWRITE = 8; // Emulate \ZipArchive::OVERWRITE
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Number of files (emulate ZipArchive::$numFiles)
|
* Number of files (emulate ZipArchive::$numFiles)
|
||||||
*
|
*
|
||||||
* @var string
|
* @var int
|
||||||
*/
|
*/
|
||||||
public $numFiles = 0;
|
public $numFiles = 0;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Archive filename (emulate ZipArchive::$filename)
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
public $filename;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Temporary storage directory
|
* Temporary storage directory
|
||||||
*
|
*
|
||||||
|
|
@ -51,34 +65,141 @@ class ZipArchive
|
||||||
private $tempDir;
|
private $tempDir;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Zip Archive Stream Handle
|
* Internal zip archive object
|
||||||
*
|
*
|
||||||
* @var string
|
* @var \ZipArchive|\PclZip
|
||||||
*/
|
*/
|
||||||
private $zip;
|
private $zip;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Use PCLZip (default behaviour)
|
||||||
|
*
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $usePclzip = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create new instance
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
$this->usePclzip = (Settings::getZipClass() != 'ZipArchive');
|
||||||
|
if ($this->usePclzip) {
|
||||||
|
if (!defined('PCLZIP_TEMPORARY_DIR')) {
|
||||||
|
define('PCLZIP_TEMPORARY_DIR', sys_get_temp_dir() . '/');
|
||||||
|
}
|
||||||
|
require_once 'PCLZip/pclzip.lib.php';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Catch function calls: pass to ZipArchive or PCLZip
|
||||||
|
*
|
||||||
|
* `call_user_func_array` can only used for public function, hence the `public` in all `pcl...` methods
|
||||||
|
*
|
||||||
|
* @param mixed $function
|
||||||
|
* @param mixed $args
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function __call($function, $args)
|
||||||
|
{
|
||||||
|
// Set object and function
|
||||||
|
$zipFunction = $function;
|
||||||
|
if (!$this->usePclzip) {
|
||||||
|
$zipObject = $this->zip;
|
||||||
|
} else {
|
||||||
|
$zipObject = $this;
|
||||||
|
$zipFunction = "pclzip{$zipFunction}";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run function
|
||||||
|
$result = @call_user_func_array(array($zipObject, $zipFunction), $args);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Open a new zip archive
|
* Open a new zip archive
|
||||||
*
|
*
|
||||||
* @param string $filename Filename for the zip archive
|
* @param string $filename The file name of the ZIP archive to open
|
||||||
* @return boolean
|
* @param int $flags The mode to use to open the archive
|
||||||
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function open($filename)
|
public function open($filename, $flags = null)
|
||||||
{
|
{
|
||||||
$this->tempDir = sys_get_temp_dir();
|
$result = true;
|
||||||
$this->zip = new \PclZip($filename);
|
$this->filename = $filename;
|
||||||
$this->numFiles = count($this->zip->listContent());
|
|
||||||
|
if (!$this->usePclzip) {
|
||||||
|
$this->zip = new \ZipArchive();
|
||||||
|
$result = $this->zip->open($this->filename, $flags);
|
||||||
|
$this->numFiles = $this->zip->numFiles;
|
||||||
|
} else {
|
||||||
|
$this->zip = new \PclZip($this->filename);
|
||||||
|
$this->tempDir = sys_get_temp_dir();
|
||||||
|
$this->numFiles = count($this->zip->listContent());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the active archive
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
||||||
|
*/
|
||||||
|
public function close()
|
||||||
|
{
|
||||||
|
if (!$this->usePclzip) {
|
||||||
|
if ($this->zip->close() === false) {
|
||||||
|
throw new Exception("Could not close zip file $this->filename.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Close this zip archive (emulate \ZipArchive)
|
* Extract the archive contents (emulate \ZipArchive)
|
||||||
*
|
*
|
||||||
* @codeCoverageIgnore
|
* @param string $destination
|
||||||
|
* @param string|array $entries
|
||||||
|
* @return bool
|
||||||
|
* @since 0.10.0
|
||||||
*/
|
*/
|
||||||
public function close()
|
public function extractTo($destination, $entries = null)
|
||||||
{
|
{
|
||||||
|
if (!is_dir($destination)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->usePclzip) {
|
||||||
|
return $this->zip->extractTo($destination, $entries);
|
||||||
|
} else {
|
||||||
|
return $this->pclzipExtractTo($destination, $entries);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract file from archive by given file name (emulate \ZipArchive)
|
||||||
|
*
|
||||||
|
* @param string $filename Filename for the file in zip archive
|
||||||
|
* @return string|false $contents File string contents
|
||||||
|
*/
|
||||||
|
public function getFromName($filename)
|
||||||
|
{
|
||||||
|
if (!$this->usePclzip) {
|
||||||
|
$contents = $this->zip->getFromName($filename);
|
||||||
|
if ($contents === false) {
|
||||||
|
$filename = substr($filename, 1);
|
||||||
|
$contents = $this->zip->getFromName($filename);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$contents = $this->pclzipGetFromName($filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -88,7 +209,7 @@ class ZipArchive
|
||||||
* @param string $localname Directory/Name of the file added to the zip
|
* @param string $localname Directory/Name of the file added to the zip
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function addFile($filename, $localname = null)
|
public function pclzipAddFile($filename, $localname = null)
|
||||||
{
|
{
|
||||||
$filename = realpath($filename);
|
$filename = realpath($filename);
|
||||||
$filenameParts = pathinfo($filename);
|
$filenameParts = pathinfo($filename);
|
||||||
|
|
@ -103,13 +224,9 @@ class ZipArchive
|
||||||
$filenameParts = pathinfo($temppath);
|
$filenameParts = pathinfo($temppath);
|
||||||
}
|
}
|
||||||
|
|
||||||
$res = $this->zip->add(
|
$pathRemoved = $filenameParts['dirname'];
|
||||||
$filename,
|
$pathAdded = $localnameParts['dirname'];
|
||||||
PCLZIP_OPT_REMOVE_PATH,
|
$res = $this->zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
|
||||||
$filenameParts['dirname'],
|
|
||||||
PCLZIP_OPT_ADD_PATH,
|
|
||||||
$localnameParts["dirname"]
|
|
||||||
);
|
|
||||||
|
|
||||||
return ($res == 0) ? false : true;
|
return ($res == 0) ? false : true;
|
||||||
}
|
}
|
||||||
|
|
@ -121,8 +238,9 @@ class ZipArchive
|
||||||
* @param string $contents String of data to add to the zip archive
|
* @param string $contents String of data to add to the zip archive
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function addFromString($localname, $contents)
|
public function pclzipAddFromString($localname, $contents)
|
||||||
{
|
{
|
||||||
|
// PCLZip emulation
|
||||||
$filenameParts = pathinfo($localname);
|
$filenameParts = pathinfo($localname);
|
||||||
|
|
||||||
// Write $contents to a temp file
|
// Write $contents to a temp file
|
||||||
|
|
@ -131,13 +249,10 @@ class ZipArchive
|
||||||
fclose($handle);
|
fclose($handle);
|
||||||
|
|
||||||
// Add temp file to zip
|
// Add temp file to zip
|
||||||
$res = $this->zip->add(
|
$filename = $this->tempDir . '/' . $filenameParts["basename"];
|
||||||
$this->tempDir . '/' . $filenameParts["basename"],
|
$pathRemoved = $this->tempDir;
|
||||||
PCLZIP_OPT_REMOVE_PATH,
|
$pathAdded = $filenameParts['dirname'];
|
||||||
$this->tempDir,
|
$res = $this->zip->add($filename, PCLZIP_OPT_REMOVE_PATH, $pathRemoved, PCLZIP_OPT_ADD_PATH, $pathAdded);
|
||||||
PCLZIP_OPT_ADD_PATH,
|
|
||||||
$filenameParts["dirname"]
|
|
||||||
);
|
|
||||||
|
|
||||||
// Remove temp file
|
// Remove temp file
|
||||||
@unlink($this->tempDir . '/' . $filenameParts["basename"]);
|
@unlink($this->tempDir . '/' . $filenameParts["basename"]);
|
||||||
|
|
@ -146,25 +261,34 @@ class ZipArchive
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the index of the entry in the archive (emulate \ZipArchive)
|
* Extract the archive contents (emulate \ZipArchive)
|
||||||
*
|
*
|
||||||
* @param string $filename Filename for the file in zip archive
|
* @param string $destination
|
||||||
* @return integer|false
|
* @param string|array $entries
|
||||||
|
* @return bool
|
||||||
|
* @since 0.10.0
|
||||||
*/
|
*/
|
||||||
public function locateName($filename)
|
public function pclzipExtractTo($destination, $entries = null)
|
||||||
{
|
{
|
||||||
$list = $this->zip->listContent();
|
// Extract all files
|
||||||
$listCount = count($list);
|
if (is_null($entries)) {
|
||||||
$listIndex = -1;
|
$result = $this->zip->extract(PCLZIP_OPT_PATH, $destination);
|
||||||
for ($i = 0; $i < $listCount; ++$i) {
|
return ($result > 0) ? true : false;
|
||||||
if (strtolower($list[$i]["filename"]) == strtolower($filename) ||
|
}
|
||||||
strtolower($list[$i]["stored_filename"]) == strtolower($filename)) {
|
|
||||||
$listIndex = $i;
|
// Extract by entries
|
||||||
break;
|
if (!is_array($entries)) {
|
||||||
|
$entries = array($entries);
|
||||||
|
}
|
||||||
|
foreach ($entries as $entry) {
|
||||||
|
$entryIndex = $this->locateName($entry);
|
||||||
|
$result = $this->zip->extractByIndex($entryIndex, PCLZIP_OPT_PATH, $destination);
|
||||||
|
if ($result <= 0) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ($listIndex > -1) ? $listIndex : false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -173,7 +297,7 @@ class ZipArchive
|
||||||
* @param string $filename Filename for the file in zip archive
|
* @param string $filename Filename for the file in zip archive
|
||||||
* @return string|false $contents File string contents
|
* @return string|false $contents File string contents
|
||||||
*/
|
*/
|
||||||
public function getFromName($filename)
|
public function pclzipGetFromName($filename)
|
||||||
{
|
{
|
||||||
$listIndex = $this->locateName($filename);
|
$listIndex = $this->locateName($filename);
|
||||||
$contents = false;
|
$contents = false;
|
||||||
|
|
@ -195,11 +319,11 @@ class ZipArchive
|
||||||
/**
|
/**
|
||||||
* Returns the name of an entry using its index (emulate \ZipArchive)
|
* Returns the name of an entry using its index (emulate \ZipArchive)
|
||||||
*
|
*
|
||||||
* @param integer $index
|
* @param int $index
|
||||||
* @return string|false
|
* @return string|false
|
||||||
* @since 0.10.0
|
* @since 0.10.0
|
||||||
*/
|
*/
|
||||||
public function getNameIndex($index)
|
public function pclzipGetNameIndex($index)
|
||||||
{
|
{
|
||||||
$list = $this->zip->listContent();
|
$list = $this->zip->listContent();
|
||||||
if (isset($list[$index])) {
|
if (isset($list[$index])) {
|
||||||
|
|
@ -210,37 +334,24 @@ class ZipArchive
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extract the archive contents (emulate \ZipArchive)
|
* Returns the index of the entry in the archive (emulate \ZipArchive)
|
||||||
*
|
*
|
||||||
* @param string $destination
|
* @param string $filename Filename for the file in zip archive
|
||||||
* @param string|array $entries
|
* @return int|false
|
||||||
* @return boolean
|
|
||||||
* @since 0.10.0
|
|
||||||
*/
|
*/
|
||||||
public function extractTo($destination, $entries = null)
|
public function pclzipLocateName($filename)
|
||||||
{
|
{
|
||||||
if (!is_dir($destination)) {
|
$list = $this->zip->listContent();
|
||||||
return false;
|
$listCount = count($list);
|
||||||
}
|
$listIndex = -1;
|
||||||
|
for ($i = 0; $i < $listCount; ++$i) {
|
||||||
// Extract all files
|
if (strtolower($list[$i]["filename"]) == strtolower($filename) ||
|
||||||
if (is_null($entries)) {
|
strtolower($list[$i]["stored_filename"]) == strtolower($filename)) {
|
||||||
$result = $this->zip->extract(PCLZIP_OPT_PATH, $destination);
|
$listIndex = $i;
|
||||||
return ($result > 0) ? true : false;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
// Extract by entries
|
|
||||||
if (!is_array($entries)) {
|
|
||||||
$entries = array($entries);
|
|
||||||
}
|
|
||||||
foreach ($entries as $entry) {
|
|
||||||
$entryIndex = $this->locateName($entry);
|
|
||||||
$result = $this->zip->extractByIndex($entryIndex, PCLZIP_OPT_PATH, $destination);
|
|
||||||
if ($result <= 0) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return ($listIndex > -1) ? $listIndex : false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ namespace PhpOffice\PhpWord;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Exception\Exception;
|
use PhpOffice\PhpWord\Exception\Exception;
|
||||||
use PhpOffice\PhpWord\Shared\String;
|
use PhpOffice\PhpWord\Shared\String;
|
||||||
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Template
|
* Template
|
||||||
|
|
@ -78,8 +79,7 @@ class Template
|
||||||
throw new Exception("Could not copy the template from {$strFilename} to {$this->tempFileName}.");
|
throw new Exception("Could not copy the template from {$strFilename} to {$this->tempFileName}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$zipClass = Settings::getZipClass();
|
$this->zipClass = new ZipArchive();
|
||||||
$this->zipClass = new $zipClass();
|
|
||||||
$this->zipClass->open($this->tempFileName);
|
$this->zipClass->open($this->tempFileName);
|
||||||
|
|
||||||
// Find and load headers and footers
|
// Find and load headers and footers
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ namespace PhpOffice\PhpWord\Writer;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Exception\Exception;
|
use PhpOffice\PhpWord\Exception\Exception;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract writer class
|
* Abstract writer class
|
||||||
|
|
@ -257,42 +257,34 @@ abstract class AbstractWriter implements WriterInterface
|
||||||
* Get ZipArchive object
|
* Get ZipArchive object
|
||||||
*
|
*
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @return mixed ZipArchive object
|
* @return \PhpOffice\PhpWord\Shared\ZipArchive
|
||||||
* @throws \PhpOffice\PhpWord\Exception\Exception
|
* @throws \PhpOffice\PhpWord\Exception\Exception
|
||||||
*/
|
*/
|
||||||
protected function getZipArchive($filename)
|
protected function getZipArchive($filename)
|
||||||
{
|
{
|
||||||
// Create new ZIP file and open it for writing
|
|
||||||
$zipClass = Settings::getZipClass();
|
|
||||||
$objZip = new $zipClass();
|
|
||||||
|
|
||||||
// Retrieve OVERWRITE and CREATE constants from the instantiated zip class
|
|
||||||
$reflection = new \ReflectionObject($objZip);
|
|
||||||
$zipOverWrite = $reflection->getConstant('OVERWRITE');
|
|
||||||
$zipCreate = $reflection->getConstant('CREATE');
|
|
||||||
|
|
||||||
// Remove any existing file
|
// Remove any existing file
|
||||||
if (file_exists($filename)) {
|
if (file_exists($filename)) {
|
||||||
unlink($filename);
|
unlink($filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try opening the ZIP file
|
// Try opening the ZIP file
|
||||||
if ($objZip->open($filename, $zipOverWrite) !== true) {
|
$zip = new ZipArchive();
|
||||||
if ($objZip->open($filename, $zipCreate) !== true) {
|
if ($zip->open($filename, ZipArchive::OVERWRITE) !== true) {
|
||||||
|
if ($zip->open($filename, ZipArchive::CREATE) !== true) {
|
||||||
throw new Exception("Could not open " . $filename . " for writing.");
|
throw new Exception("Could not open " . $filename . " for writing.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $objZip;
|
return $zip;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add files to package
|
* Add files to package
|
||||||
*
|
*
|
||||||
* @param mixed $objZip
|
* @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
|
||||||
* @param mixed $elements
|
* @param mixed $elements
|
||||||
*/
|
*/
|
||||||
protected function addFilesToPackage($objZip, $elements)
|
protected function addFilesToPackage(ZipArchive $zip, $elements)
|
||||||
{
|
{
|
||||||
foreach ($elements as $element) {
|
foreach ($elements as $element) {
|
||||||
$type = $element['type']; // image|object|link
|
$type = $element['type']; // image|object|link
|
||||||
|
|
@ -310,10 +302,10 @@ abstract class AbstractWriter implements WriterInterface
|
||||||
call_user_func($element['imageFunction'], $image);
|
call_user_func($element['imageFunction'], $image);
|
||||||
$imageContents = ob_get_contents();
|
$imageContents = ob_get_contents();
|
||||||
ob_end_clean();
|
ob_end_clean();
|
||||||
$objZip->addFromString($target, $imageContents);
|
$zip->addFromString($target, $imageContents);
|
||||||
imagedestroy($image);
|
imagedestroy($image);
|
||||||
} else {
|
} else {
|
||||||
$this->addFileToPackage($objZip, $element['source'], $target);
|
$this->addFileToPackage($zip, $element['source'], $target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -323,11 +315,11 @@ abstract class AbstractWriter implements WriterInterface
|
||||||
*
|
*
|
||||||
* Get the actual source from an archive image
|
* Get the actual source from an archive image
|
||||||
*
|
*
|
||||||
* @param mixed $objZip
|
* @param \PhpOffice\PhpWord\Shared\ZipArchive $zipPackage
|
||||||
* @param string $source
|
* @param string $source
|
||||||
* @param string $target
|
* @param string $target
|
||||||
*/
|
*/
|
||||||
protected function addFileToPackage($objZip, $source, $target)
|
protected function addFileToPackage($zipPackage, $source, $target)
|
||||||
{
|
{
|
||||||
$isArchive = strpos($source, 'zip://') !== false;
|
$isArchive = strpos($source, 'zip://') !== false;
|
||||||
$actualSource = null;
|
$actualSource = null;
|
||||||
|
|
@ -335,8 +327,7 @@ abstract class AbstractWriter implements WriterInterface
|
||||||
$source = substr($source, 6);
|
$source = substr($source, 6);
|
||||||
list($zipFilename, $imageFilename) = explode('#', $source);
|
list($zipFilename, $imageFilename) = explode('#', $source);
|
||||||
|
|
||||||
$zipClass = Settings::getZipClass();
|
$zip = new ZipArchive;
|
||||||
$zip = new $zipClass();
|
|
||||||
if ($zip->open($zipFilename) !== false) {
|
if ($zip->open($zipFilename) !== false) {
|
||||||
if ($zip->locateName($imageFilename)) {
|
if ($zip->locateName($imageFilename)) {
|
||||||
$zip->extractTo($this->getTempDir(), $imageFilename);
|
$zip->extractTo($this->getTempDir(), $imageFilename);
|
||||||
|
|
@ -349,7 +340,7 @@ abstract class AbstractWriter implements WriterInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_null($actualSource)) {
|
if (!is_null($actualSource)) {
|
||||||
$objZip->addFile($actualSource, $target);
|
$zipPackage->addFile($actualSource, $target);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
namespace PhpOffice\PhpWord\Writer\HTML\Element;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Element\Image as ImageElement;
|
use PhpOffice\PhpWord\Element\Image as ImageElement;
|
||||||
use PhpOffice\PhpWord\Settings;
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
use PhpOffice\PhpWord\Writer\HTML\Style\Image as ImageStyleWriter;
|
use PhpOffice\PhpWord\Writer\HTML\Style\Image as ImageStyleWriter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -77,8 +77,7 @@ class Image extends Text
|
||||||
$source = substr($source, 6);
|
$source = substr($source, 6);
|
||||||
list($zipFilename, $imageFilename) = explode('#', $source);
|
list($zipFilename, $imageFilename) = explode('#', $source);
|
||||||
|
|
||||||
$zipClass = Settings::getZipClass();
|
$zip = new ZipArchive();
|
||||||
$zip = new $zipClass();
|
|
||||||
if ($zip->open($zipFilename) !== false) {
|
if ($zip->open($zipFilename) !== false) {
|
||||||
if ($zip->locateName($imageFilename)) {
|
if ($zip->locateName($imageFilename)) {
|
||||||
$zip->extractTo($this->parentWriter->getTempDir(), $imageFilename);
|
$zip->extractTo($this->parentWriter->getTempDir(), $imageFilename);
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,6 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Writer;
|
namespace PhpOffice\PhpWord\Writer;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Exception\Exception;
|
|
||||||
use PhpOffice\PhpWord\Media;
|
use PhpOffice\PhpWord\Media;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
|
||||||
|
|
@ -64,31 +63,27 @@ class ODText extends AbstractWriter implements WriterInterface
|
||||||
* Save PhpWord to file
|
* Save PhpWord to file
|
||||||
*
|
*
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @throws \PhpOffice\PhpWord\Exception\Exception
|
|
||||||
*/
|
*/
|
||||||
public function save($filename = null)
|
public function save($filename = null)
|
||||||
{
|
{
|
||||||
$filename = $this->getTempFile($filename);
|
$filename = $this->getTempFile($filename);
|
||||||
$objZip = $this->getZipArchive($filename);
|
$zip = $this->getZipArchive($filename);
|
||||||
|
|
||||||
// Add section media files
|
// Add section media files
|
||||||
$sectionMedia = Media::getElements('section');
|
$sectionMedia = Media::getElements('section');
|
||||||
if (!empty($sectionMedia)) {
|
if (!empty($sectionMedia)) {
|
||||||
$this->addFilesToPackage($objZip, $sectionMedia);
|
$this->addFilesToPackage($zip, $sectionMedia);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write parts
|
// Write parts
|
||||||
foreach ($this->parts as $partName => $fileName) {
|
foreach ($this->parts as $partName => $fileName) {
|
||||||
if ($fileName != '') {
|
if ($fileName != '') {
|
||||||
$objZip->addFromString($fileName, $this->getWriterPart($partName)->write());
|
$zip->addFromString($fileName, $this->getWriterPart($partName)->write());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close file
|
// Close zip archive and cleanup temp file
|
||||||
if ($objZip->close() === false) {
|
$zip->close();
|
||||||
throw new Exception("Could not close zip file $filename.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->cleanupTempFile();
|
$this->cleanupTempFile();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@
|
||||||
namespace PhpOffice\PhpWord\Writer;
|
namespace PhpOffice\PhpWord\Writer;
|
||||||
|
|
||||||
use PhpOffice\PhpWord\Element\Section;
|
use PhpOffice\PhpWord\Element\Section;
|
||||||
use PhpOffice\PhpWord\Exception\Exception;
|
|
||||||
use PhpOffice\PhpWord\Media;
|
use PhpOffice\PhpWord\Media;
|
||||||
use PhpOffice\PhpWord\PhpWord;
|
use PhpOffice\PhpWord\PhpWord;
|
||||||
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Word2007 writer
|
* Word2007 writer
|
||||||
|
|
@ -88,13 +88,12 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
* Save document by name
|
* Save document by name
|
||||||
*
|
*
|
||||||
* @param string $filename
|
* @param string $filename
|
||||||
* @throws \PhpOffice\PhpWord\Exception\Exception
|
|
||||||
*/
|
*/
|
||||||
public function save($filename = null)
|
public function save($filename = null)
|
||||||
{
|
{
|
||||||
$phpWord = $this->getPhpWord();
|
|
||||||
$filename = $this->getTempFile($filename);
|
$filename = $this->getTempFile($filename);
|
||||||
$objZip = $this->getZipArchive($filename);
|
$zip = $this->getZipArchive($filename);
|
||||||
|
$phpWord = $this->getPhpWord();
|
||||||
|
|
||||||
// Content types
|
// Content types
|
||||||
$this->contentTypes['default'] = array(
|
$this->contentTypes['default'] = array(
|
||||||
|
|
@ -105,7 +104,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
// Add section media files
|
// Add section media files
|
||||||
$sectionMedia = Media::getElements('section');
|
$sectionMedia = Media::getElements('section');
|
||||||
if (!empty($sectionMedia)) {
|
if (!empty($sectionMedia)) {
|
||||||
$this->addFilesToPackage($objZip, $sectionMedia);
|
$this->addFilesToPackage($zip, $sectionMedia);
|
||||||
$this->registerContentTypes($sectionMedia);
|
$this->registerContentTypes($sectionMedia);
|
||||||
foreach ($sectionMedia as $element) {
|
foreach ($sectionMedia as $element) {
|
||||||
$this->relationships[] = $element;
|
$this->relationships[] = $element;
|
||||||
|
|
@ -113,32 +112,29 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add header/footer media files & relations
|
// Add header/footer media files & relations
|
||||||
$this->addHeaderFooterMedia($objZip, 'header');
|
$this->addHeaderFooterMedia($zip, 'header');
|
||||||
$this->addHeaderFooterMedia($objZip, 'footer');
|
$this->addHeaderFooterMedia($zip, 'footer');
|
||||||
|
|
||||||
// Add header/footer contents
|
// Add header/footer contents
|
||||||
$rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements
|
$rId = Media::countElements('section') + 6; // @see Rels::writeDocRels for 6 first elements
|
||||||
$sections = $phpWord->getSections();
|
$sections = $phpWord->getSections();
|
||||||
foreach ($sections as $section) {
|
foreach ($sections as $section) {
|
||||||
$this->addHeaderFooterContent($section, $objZip, 'header', $rId);
|
$this->addHeaderFooterContent($section, $zip, 'header', $rId);
|
||||||
$this->addHeaderFooterContent($section, $objZip, 'footer', $rId);
|
$this->addHeaderFooterContent($section, $zip, 'footer', $rId);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->addNotes($objZip, $rId, 'footnote');
|
$this->addNotes($zip, $rId, 'footnote');
|
||||||
$this->addNotes($objZip, $rId, 'endnote');
|
$this->addNotes($zip, $rId, 'endnote');
|
||||||
|
|
||||||
// Write parts
|
// Write parts
|
||||||
foreach ($this->parts as $partName => $fileName) {
|
foreach ($this->parts as $partName => $fileName) {
|
||||||
if ($fileName != '') {
|
if ($fileName != '') {
|
||||||
$objZip->addFromString($fileName, $this->getWriterPart($partName)->write());
|
$zip->addFromString($fileName, $this->getWriterPart($partName)->write());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Close file
|
// Close zip archive and cleanup temp file
|
||||||
if ($objZip->close() === false) {
|
$zip->close();
|
||||||
throw new Exception("Could not close zip file $filename.");
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->cleanupTempFile();
|
$this->cleanupTempFile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -165,22 +161,22 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
/**
|
/**
|
||||||
* Add header/footer media files, e.g. footer1.xml.rels
|
* Add header/footer media files, e.g. footer1.xml.rels
|
||||||
*
|
*
|
||||||
* @param mixed $objZip
|
* @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
|
||||||
* @param string $docPart
|
* @param string $docPart
|
||||||
*/
|
*/
|
||||||
private function addHeaderFooterMedia($objZip, $docPart)
|
private function addHeaderFooterMedia(ZipArchive $zip, $docPart)
|
||||||
{
|
{
|
||||||
$elements = Media::getElements($docPart);
|
$elements = Media::getElements($docPart);
|
||||||
if (!empty($elements)) {
|
if (!empty($elements)) {
|
||||||
foreach ($elements as $file => $media) {
|
foreach ($elements as $file => $media) {
|
||||||
if (count($media) > 0) {
|
if (count($media) > 0) {
|
||||||
if (!empty($media)) {
|
if (!empty($media)) {
|
||||||
$this->addFilesToPackage($objZip, $media);
|
$this->addFilesToPackage($zip, $media);
|
||||||
$this->registerContentTypes($media);
|
$this->registerContentTypes($media);
|
||||||
}
|
}
|
||||||
|
|
||||||
$writerPart = $this->getWriterPart('relspart')->setMedia($media);
|
$writerPart = $this->getWriterPart('relspart')->setMedia($media);
|
||||||
$objZip->addFromString("word/_rels/{$file}.xml.rels", $writerPart->write());
|
$zip->addFromString("word/_rels/{$file}.xml.rels", $writerPart->write());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -190,11 +186,11 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
* Add header/footer content
|
* Add header/footer content
|
||||||
*
|
*
|
||||||
* @param \PhpOffice\PhpWord\Element\Section $section
|
* @param \PhpOffice\PhpWord\Element\Section $section
|
||||||
* @param mixed $objZip
|
* @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
|
||||||
* @param string $elmType header|footer
|
* @param string $elmType header|footer
|
||||||
* @param integer $rId
|
* @param integer $rId
|
||||||
*/
|
*/
|
||||||
private function addHeaderFooterContent(Section &$section, $objZip, $elmType, &$rId)
|
private function addHeaderFooterContent(Section &$section, ZipArchive $zip, $elmType, &$rId)
|
||||||
{
|
{
|
||||||
$getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
|
$getFunction = $elmType == 'header' ? 'getHeaders' : 'getFooters';
|
||||||
$elmCount = ($section->getSectionId() - 1) * 3;
|
$elmCount = ($section->getSectionId() - 1) * 3;
|
||||||
|
|
@ -207,18 +203,18 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
$this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
|
$this->relationships[] = array('target' => $elmFile, 'type' => $elmType, 'rID' => $rId);
|
||||||
|
|
||||||
$writerPart = $this->getWriterPart($elmType)->setElement($element);
|
$writerPart = $this->getWriterPart($elmType)->setElement($element);
|
||||||
$objZip->addFromString("word/$elmFile", $writerPart->write());
|
$zip->addFromString("word/$elmFile", $writerPart->write());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add footnotes/endnotes
|
* Add footnotes/endnotes
|
||||||
*
|
*
|
||||||
* @param mixed $objZip
|
* @param \PhpOffice\PhpWord\Shared\ZipArchive $zip
|
||||||
* @param integer $rId
|
* @param integer $rId
|
||||||
* @param string $noteType
|
* @param string $noteType
|
||||||
*/
|
*/
|
||||||
private function addNotes($objZip, &$rId, $noteType = 'footnote')
|
private function addNotes(ZipArchive $zip, &$rId, $noteType = 'footnote')
|
||||||
{
|
{
|
||||||
$phpWord = $this->getPhpWord();
|
$phpWord = $this->getPhpWord();
|
||||||
$noteType = ($noteType == 'endnote') ? 'endnote' : 'footnote';
|
$noteType = ($noteType == 'endnote') ? 'endnote' : 'footnote';
|
||||||
|
|
@ -229,7 +225,7 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
// Add footnotes media files, relations, and contents
|
// Add footnotes media files, relations, and contents
|
||||||
if ($collection->countItems() > 0) {
|
if ($collection->countItems() > 0) {
|
||||||
$media = Media::getElements($noteType);
|
$media = Media::getElements($noteType);
|
||||||
$this->addFilesToPackage($objZip, $media);
|
$this->addFilesToPackage($zip, $media);
|
||||||
$this->registerContentTypes($media);
|
$this->registerContentTypes($media);
|
||||||
$this->contentTypes['override']["/word/{$partName}.xml"] = $partName;
|
$this->contentTypes['override']["/word/{$partName}.xml"] = $partName;
|
||||||
$this->relationships[] = array('target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId);
|
$this->relationships[] = array('target' => "{$partName}.xml", 'type' => $partName, 'rID' => ++$rId);
|
||||||
|
|
@ -237,12 +233,12 @@ class Word2007 extends AbstractWriter implements WriterInterface
|
||||||
// Write relationships file, e.g. word/_rels/footnotes.xml
|
// Write relationships file, e.g. word/_rels/footnotes.xml
|
||||||
if (!empty($media)) {
|
if (!empty($media)) {
|
||||||
$writerPart = $this->getWriterPart('relspart')->setMedia($media);
|
$writerPart = $this->getWriterPart('relspart')->setMedia($media);
|
||||||
$objZip->addFromString("word/_rels/{$partName}.xml.rels", $writerPart->write());
|
$zip->addFromString("word/_rels/{$partName}.xml.rels", $writerPart->write());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write content file, e.g. word/footnotes.xml
|
// Write content file, e.g. word/footnotes.xml
|
||||||
$writerPart = $this->getWriterPart($partName)->setElements($collection->getItems());
|
$writerPart = $this->getWriterPart($partName)->setElements($collection->getItems());
|
||||||
$objZip->addFromString("word/{$partName}.xml", $writerPart->write());
|
$zip->addFromString("word/{$partName}.xml", $writerPart->write());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
namespace PhpOffice\PhpWord\Tests\Shared;
|
namespace PhpOffice\PhpWord\Tests\Shared;
|
||||||
|
|
||||||
|
use PhpOffice\PhpWord\Settings;
|
||||||
use PhpOffice\PhpWord\Shared\ZipArchive;
|
use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -27,28 +28,60 @@ use PhpOffice\PhpWord\Shared\ZipArchive;
|
||||||
*/
|
*/
|
||||||
class ZipArchiveTest extends \PHPUnit_Framework_TestCase
|
class ZipArchiveTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test close method exception: Working in local, not working in Travis
|
||||||
|
*
|
||||||
|
* expectedException \PhpOffice\PhpWord\Exception\Exception
|
||||||
|
* expectedExceptionMessage Could not close zip file
|
||||||
|
* covers ::close
|
||||||
|
*/
|
||||||
|
public function testCloseException()
|
||||||
|
{
|
||||||
|
// $zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
|
||||||
|
|
||||||
|
// $object = new ZipArchive();
|
||||||
|
// $object->open($zipFile, ZipArchive::CREATE);
|
||||||
|
// $object->addFromString('content/string.txt', 'Test');
|
||||||
|
|
||||||
|
// // Lock the file
|
||||||
|
// $resource = fopen($zipFile, "w");
|
||||||
|
// flock($resource, LOCK_EX);
|
||||||
|
|
||||||
|
// // Closing the file should throws an exception
|
||||||
|
// $object->close();
|
||||||
|
|
||||||
|
// // Unlock the file
|
||||||
|
// flock($resource, LOCK_UN);
|
||||||
|
// fclose($resource);
|
||||||
|
|
||||||
|
// @unlink($zipFile);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test all methods
|
* Test all methods
|
||||||
*
|
*
|
||||||
|
* @param string $zipClass
|
||||||
* @covers ::<public>
|
* @covers ::<public>
|
||||||
*/
|
*/
|
||||||
public function testAllMethods()
|
public function testZipArchive($zipClass = 'ZipArchive')
|
||||||
{
|
{
|
||||||
// Preparation
|
// Preparation
|
||||||
$existingFile = __DIR__ . "/../_files/documents/sheet.xls";
|
$existingFile = __DIR__ . "/../_files/documents/sheet.xls";
|
||||||
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
|
$zipFile = __DIR__ . "/../_files/documents/ziptest.zip";
|
||||||
$destination1 = __DIR__ . "/../_files/extract1";
|
$destination1 = __DIR__ . "/../_files/documents/extract1";
|
||||||
$destination2 = __DIR__ . "/../_files/extract2";
|
$destination2 = __DIR__ . "/../_files/documents/extract2";
|
||||||
$destination3 = __DIR__ . "/../_files/extract3";
|
|
||||||
@mkdir($destination1);
|
@mkdir($destination1);
|
||||||
@mkdir($destination2);
|
@mkdir($destination2);
|
||||||
@mkdir($destination3);
|
|
||||||
|
Settings::setZipClass($zipClass);
|
||||||
|
|
||||||
$object = new ZipArchive();
|
$object = new ZipArchive();
|
||||||
$object->open($zipFile);
|
$object->open($zipFile, ZipArchive::CREATE);
|
||||||
$object->addFile($existingFile, 'xls/new.xls');
|
$object->addFile($existingFile, 'xls/new.xls');
|
||||||
$object->addFromString('content/string.txt', 'Test');
|
$object->addFromString('content/string.txt', 'Test');
|
||||||
$object->close();
|
$object->close();
|
||||||
|
$object->open($zipFile);
|
||||||
|
|
||||||
// Run tests
|
// Run tests
|
||||||
$this->assertEquals(0, $object->locateName('xls/new.xls'));
|
$this->assertEquals(0, $object->locateName('xls/new.xls'));
|
||||||
|
|
@ -68,10 +101,19 @@ class ZipArchiveTest extends \PHPUnit_Framework_TestCase
|
||||||
// Cleanup
|
// Cleanup
|
||||||
$this->deleteDir($destination1);
|
$this->deleteDir($destination1);
|
||||||
$this->deleteDir($destination2);
|
$this->deleteDir($destination2);
|
||||||
$this->deleteDir($destination3);
|
|
||||||
@unlink($zipFile);
|
@unlink($zipFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test PclZip
|
||||||
|
*
|
||||||
|
* @covers ::<public>
|
||||||
|
*/
|
||||||
|
public function testPCLZip()
|
||||||
|
{
|
||||||
|
$this->testZipArchive('PhpOffice\PhpWord\Shared\ZipArchive');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete directory
|
* Delete directory
|
||||||
*
|
*
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue