This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: (adamriyadi) GH-247 - Add locateName() and getFromName() to …
…PHPExcel_Shared_ZipArchive
- Loading branch information
Mark Baker
committed
Nov 17, 2013
1 parent
732cb11
commit 51a1661
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ([email protected]) | ||
* | ||
* @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 ([email protected]) | ||
* | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters