General: (adamriyadi) Work Item GH-247 - Modify PHPExcel_Reader_Excel2007 to use zipClass from PHPExcel_Settings::getZipClass()

This allows the use of PCLZip when reading for people that don't have access to ZipArchive
This commit is contained in:
Mark Baker 2013-11-17 17:33:56 +00:00
parent 7fb98dbcd5
commit 0d8a5d1f0b
3 changed files with 26 additions and 16 deletions

View File

@ -82,14 +82,16 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist."); throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
} }
$zipClass = PHPExcel_Settings::getZipClass();
// Check if zip class exists // Check if zip class exists
if (!class_exists('ZipArchive',FALSE)) { // if (!class_exists($zipClass, FALSE)) {
throw new PHPExcel_Reader_Exception("ZipArchive library is not enabled"); // throw new PHPExcel_Reader_Exception($zipClass . " library is not enabled");
} // }
$xl = false; $xl = false;
// Load file // Load file
$zip = new ZipArchive; $zip = new $zipClass;
if ($zip->open($pFilename) === true) { if ($zip->open($pFilename) === true) {
// check if it is an OOXML archive // check if it is an OOXML archive
$rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); $rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels"));
@ -127,7 +129,9 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
$worksheetNames = array(); $worksheetNames = array();
$zip = new ZipArchive; $zipClass = PHPExcel_Settings::getZipClass();
$zip = new $zipClass;
$zip->open($pFilename); $zip->open($pFilename);
// The files we're looking at here are small enough that simpleXML is more efficient than XMLReader // The files we're looking at here are small enough that simpleXML is more efficient than XMLReader
@ -171,7 +175,9 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
$worksheetInfo = array(); $worksheetInfo = array();
$zip = new ZipArchive; $zipClass = PHPExcel_Settings::getZipClass();
$zip = new $zipClass;
$zip->open($pFilename); $zip->open($pFilename);
$rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); //~ http://schemas.openxmlformats.org/package/2006/relationships"); $rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); //~ http://schemas.openxmlformats.org/package/2006/relationships");
@ -308,7 +314,7 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
} }
public function _getFromZipArchive(ZipArchive $archive, $fileName = '') public function _getFromZipArchive($archive, $fileName = '')
{ {
// Root-relative paths // Root-relative paths
if (strpos($fileName, '//') !== false) if (strpos($fileName, '//') !== false)
@ -348,7 +354,10 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
$excel->removeCellStyleXfByIndex(0); // remove the default style $excel->removeCellStyleXfByIndex(0); // remove the default style
$excel->removeCellXfByIndex(0); // remove the default style $excel->removeCellXfByIndex(0); // remove the default style
} }
$zip = new ZipArchive;
$zipClass = PHPExcel_Settings::getZipClass();
$zip = new $zipClass;
$zip->open($pFilename); $zip->open($pFilename);
// Read the theme first, because we need the colour scheme when reading the styles // Read the theme first, because we need the colour scheme when reading the styles
@ -1409,7 +1418,7 @@ class PHPExcel_Reader_Excel2007 extends PHPExcel_Reader_Abstract implements PHPE
} }
// TODO: Autoshapes from twoCellAnchors! // TODO: Autoshapes from twoCellAnchors!
if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) { if ($zip->locateName(dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels")) {
$relsWorksheet = simplexml_load_string($this->_getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") ); //~ http://schemas.openxmlformats.org/package/2006/relationships"); $relsWorksheet = simplexml_load_string($this->_getFromZipArchive($zip, dirname("$dir/$fileWorksheet") . "/_rels/" . basename($fileWorksheet) . ".rels") ); //~ http://schemas.openxmlformats.org/package/2006/relationships");
$drawings = array(); $drawings = array();

View File

@ -113,7 +113,6 @@ class PHPExcel_Shared_ZipArchive
/** /**
* Find if given fileName exist in archive (Emulate ZipArchive locateName()) * Find if given fileName exist in archive (Emulate ZipArchive locateName())
* author Adam (adam.riyadi@gmail.com)
* *
* @param string $fileName Filename for the file in zip archive * @param string $fileName Filename for the file in zip archive
* @return boolean * @return boolean
@ -123,7 +122,7 @@ class PHPExcel_Shared_ZipArchive
$list = $this->_zip->listContent(); $list = $this->_zip->listContent();
$listCount = count($list); $listCount = count($list);
$list_index = -1; $list_index = -1;
for ($i = 0; $i < $listCount; $i++) { for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
$list_index = $i; $list_index = $i;
@ -135,7 +134,6 @@ class PHPExcel_Shared_ZipArchive
/** /**
* Extract file from archive by given fileName (Emulate ZipArchive getFromName()) * Extract file from archive by given fileName (Emulate ZipArchive getFromName())
* author Adam (adam.riyadi@gmail.com)
* *
* @param string $fileName Filename for the file in zip archive * @param string $fileName Filename for the file in zip archive
* @return string $contents File string contents * @return string $contents File string contents
@ -145,22 +143,23 @@ class PHPExcel_Shared_ZipArchive
$list = $this->_zip->listContent(); $list = $this->_zip->listContent();
$listCount = count($list); $listCount = count($list);
$list_index = -1; $list_index = -1;
for ($i = 0; $i < $listCount; $i++) { for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
$list_index = $i; $list_index = $i;
break; break;
} }
} }
$extracted = ""; $extracted = "";
if ($list_index != -1) { if ($list_index != -1) {
$extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING); $extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
} else { } else {
$filename = substr($fileName, 1); $filename = substr($fileName, 1);
$list_index = -1; $list_index = -1;
for ($i = 0; $i < $listCount; $i++) { for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) { if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
$list_index = $i; $list_index = $i;
break; break;
} }

View File

@ -45,6 +45,8 @@ Fixed in develop branch for release v1.8.0:
- General: (MBaker) - Modified Excel2007 Writer to default preCalculateFormulas to false - General: (MBaker) - Modified Excel2007 Writer to default preCalculateFormulas to false
Note that autosize columns will still recalculate affected formulae internally Note that autosize columns will still recalculate affected formulae internally
- General: (dresenhista) Work Item GH-242 - Functionality to getHighestRow() for a specified column, and getHighestColumn() for a specified row - General: (dresenhista) Work Item GH-242 - Functionality to getHighestRow() for a specified column, and getHighestColumn() for a specified row
- General: (adamriyadi) Work Item GH-247 - Modify PHPExcel_Reader_Excel2007 to use zipClass from PHPExcel_Settings::getZipClass()
This allows the use of PCLZip when reading for people that don't have access to ZipArchive
Fixed in develop branch for release v1.7.9: Fixed in develop branch for release v1.7.9: