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

Commit

Permalink
Feature: (adamriyadi) GH-247 - Add locateName() and getFromName() to …
Browse files Browse the repository at this point in the history
…PHPExcel_Shared_ZipArchive
  • Loading branch information
Mark Baker committed Nov 17, 2013
1 parent 732cb11 commit 51a1661
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Classes/PHPExcel/Shared/ZipArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,66 @@ public function addFromString($localname, $contents)
unlink($this->_tempDir.'/'.$filenameParts["basename"]);
}

/**
* 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
*/
public function locateName($name)
{
$list = $this->_zip->listContent();
$listCount = count($list);
$list_index = -1;
for ($i = 0; $i < $listCount; $i++) {
if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
$list_index = $i;
break;
}
}
return ($list_index > -1);
}

/**
* 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
*/
public function getFromName($fileName)
{
$list = $this->_zip->listContent();
$listCount = count($list);
$list_index = -1;
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)) {
$list_index = $i;
break;
}
}
$extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
}
if ((is_array($extracted)) && ($extracted != 0)) {
$contents = $extracted[0]["content"];
}

return $contents;
}
}
1 change: 1 addition & 0 deletions Examples/runall.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
// List of tests
$aTests = array(
'01simple.php'
, '01simplePCLZip.php'
, '02types.php'
, '02types-xls.php'
, '03formulas.php'
Expand Down

0 comments on commit 51a1661

Please sign in to comment.