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

Commit

Permalink
General: (adamriyadi) Work Item GH-247 - Modify PHPExcel_Reader_Excel…
Browse files Browse the repository at this point in the history
…2007 to use zipClass from PHPExcel_Settings::getZipClass()

This allows the use of PCLZip when reading for people that don't have access to ZipArchive
  • Loading branch information
Mark Baker committed Nov 17, 2013
1 parent 7fb98db commit 0d8a5d1
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
27 changes: 18 additions & 9 deletions Classes/PHPExcel/Reader/Excel2007.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,16 @@ public function canRead($pFilename)
throw new PHPExcel_Reader_Exception("Could not open " . $pFilename . " for reading! File does not exist.");
}

$zipClass = PHPExcel_Settings::getZipClass();

// Check if zip class exists
if (!class_exists('ZipArchive',FALSE)) {
throw new PHPExcel_Reader_Exception("ZipArchive library is not enabled");
}
// if (!class_exists($zipClass, FALSE)) {
// throw new PHPExcel_Reader_Exception($zipClass . " library is not enabled");
// }

$xl = false;
// Load file
$zip = new ZipArchive;
$zip = new $zipClass;
if ($zip->open($pFilename) === true) {
// check if it is an OOXML archive
$rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels"));
Expand Down Expand Up @@ -127,7 +129,9 @@ public function listWorksheetNames($pFilename)

$worksheetNames = array();

$zip = new ZipArchive;
$zipClass = PHPExcel_Settings::getZipClass();

$zip = new $zipClass;
$zip->open($pFilename);

// The files we're looking at here are small enough that simpleXML is more efficient than XMLReader
Expand Down Expand Up @@ -171,7 +175,9 @@ public function listWorksheetInfo($pFilename)

$worksheetInfo = array();

$zip = new ZipArchive;
$zipClass = PHPExcel_Settings::getZipClass();

$zip = new $zipClass;
$zip->open($pFilename);

$rels = simplexml_load_string($this->_getFromZipArchive($zip, "_rels/.rels")); //~ http://schemas.openxmlformats.org/package/2006/relationships");
Expand Down Expand Up @@ -308,7 +314,7 @@ private function _castToFormula($c,$r,&$cellDataType,&$value,&$calculatedValue,&
}


public function _getFromZipArchive(ZipArchive $archive, $fileName = '')
public function _getFromZipArchive($archive, $fileName = '')
{
// Root-relative paths
if (strpos($fileName, '//') !== false)
Expand Down Expand Up @@ -348,7 +354,10 @@ public function load($pFilename)
$excel->removeCellStyleXfByIndex(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);

// Read the theme first, because we need the colour scheme when reading the styles
Expand Down Expand Up @@ -1409,7 +1418,7 @@ public function load($pFilename)

}

// TODO: Autoshapes from twoCellAnchors!
// TODO: Autoshapes from twoCellAnchors!
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");
$drawings = array();
Expand Down
13 changes: 6 additions & 7 deletions Classes/PHPExcel/Shared/ZipArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public function addFromString($localname, $contents)

/**
* 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
* @return boolean
Expand All @@ -123,7 +122,7 @@ public function locateName($fileName)
$list = $this->_zip->listContent();
$listCount = count($list);
$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)) {
$list_index = $i;
Expand All @@ -135,7 +134,6 @@ public function locateName($fileName)

/**
* 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
* @return string $contents File string contents
Expand All @@ -145,22 +143,23 @@ public function getFromName($fileName)
$list = $this->_zip->listContent();
$listCount = count($list);
$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)) {
$list_index = $i;
break;
}
}

$extracted = "";
if ($list_index != -1) {
$extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
} else {
$filename = substr($fileName, 1);
$list_index = -1;
for ($i = 0; $i < $listCount; $i++) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) || strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
for ($i = 0; $i < $listCount; ++$i) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
$list_index = $i;
break;
}
Expand Down
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ Fixed in develop branch for release v1.8.0:
- General: (MBaker) - Modified Excel2007 Writer to default preCalculateFormulas to false
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: (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:
Expand Down

0 comments on commit 0d8a5d1

Please sign in to comment.