Skip to content

Commit f5f03a5

Browse files
committed
Emulate \ZipArchive::extractTo for PCLZip
1 parent 4db75c3 commit f5f03a5

File tree

11 files changed

+113
-91
lines changed

11 files changed

+113
-91
lines changed

samples/Sample_07_TemplateCloneRow.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@
6060
$document->saveAs($name);
6161
rename($name, "results/{$name}");
6262

63-
$writers = array('Word2007' => 'docx');
64-
echo getEndingNotes($writers);
63+
echo getEndingNotes(array('Word2007' => 'docx'));
6564
if (!CLI) {
6665
include_once 'Sample_Footer.php';
6766
}

samples/Sample_23_TemplateBlock.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
$document->saveAs($name);
1919
rename($name, "results/{$name}");
2020

21-
$writers = array('Word2007' => 'docx');
22-
echo getEndingNotes($writers);
21+
echo getEndingNotes(array('Word2007' => 'docx'));
2322
if (!CLI) {
2423
include_once 'Sample_Footer.php';
2524
}

src/PhpWord/Element/Image.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace PhpOffice\PhpWord\Element;
1111

12+
use PhpOffice\PhpWord\Settings;
1213
use PhpOffice\PhpWord\Exception\InvalidImageException;
1314
use PhpOffice\PhpWord\Exception\UnsupportedImageTypeException;
1415
use PhpOffice\PhpWord\Style\Image as ImageStyle;
@@ -283,7 +284,8 @@ private function getArchiveImageSize($source)
283284
list($zipFilename, $imageFilename) = explode('#', $source);
284285
$tempFilename = tempnam(sys_get_temp_dir(), 'PHPWordImage');
285286

286-
$zip = new \ZipArchive();
287+
$zipClass = Settings::getZipClass();
288+
$zip = new $zipClass();
287289
if ($zip->open($zipFilename) !== false) {
288290
if ($zip->locateName($imageFilename)) {
289291
$imageContent = $zip->getFromName($imageFilename);

src/PhpWord/Reader/Word2007.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use PhpOffice\PhpWord\PhpWord;
1313
use PhpOffice\PhpWord\Settings;
1414
use PhpOffice\PhpWord\Footnote;
15-
use PhpOffice\PhpWord\Endnotes;
1615
use PhpOffice\PhpWord\DocumentProperties;
1716
use PhpOffice\PhpWord\Shared\XMLReader;
1817
use PhpOffice\PhpWord\Element\Section;

src/PhpWord/Shared/ZipArchive.php

Lines changed: 54 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111

1212
use PhpOffice\PhpWord\Exception\Exception;
1313

14+
// PCLZIP needs the temp path to end in a back slash
1415
// @codeCoverageIgnoreStart
1516
if (!defined('PCLZIP_TEMPORARY_DIR')) {
16-
// PCLZIP needs the temp path to end in a back slash
1717
define('PCLZIP_TEMPORARY_DIR', sys_get_temp_dir() . '/');
1818
}
1919
require_once 'PCLZip/pclzip.lib.php';
@@ -67,7 +67,7 @@ public function open($filename)
6767
}
6868

6969
/**
70-
* Close this zip archive
70+
* Close this zip archive (emulate \ZipArchive)
7171
*
7272
* @codeCoverageIgnore
7373
*/
@@ -76,7 +76,7 @@ public function close()
7676
}
7777

7878
/**
79-
* Add a new file to the zip archive.
79+
* Add a new file to the zip archive (emulate \ZipArchive)
8080
*
8181
* @param string $filename Directory/Name of the file to add to the zip archive
8282
* @param string $localname Directory/Name of the file added to the zip
@@ -104,15 +104,11 @@ public function addFile($filename, $localname = null)
104104
$localnameParts["dirname"]
105105
);
106106

107-
if ($res == 0) {
108-
throw new Exception("Error zipping files : " . $this->zip->errorInfo(true));
109-
}
110-
111-
return true;
107+
return ($res == 0) ? false : true;
112108
}
113109

114110
/**
115-
* Add a new file to the zip archive from a string of raw data.
111+
* Add a new file to the zip archive from a string of raw data (emulate \ZipArchive)
116112
*
117113
* @param string $localname Directory/Name of the file to add to the zip archive
118114
* @param string $contents String of data to add to the zip archive
@@ -134,21 +130,18 @@ public function addFromString($localname, $contents)
134130
PCLZIP_OPT_ADD_PATH,
135131
$filenameParts["dirname"]
136132
);
137-
if ($res == 0) {
138-
throw new Exception("Error zipping files : " . $this->zip->errorInfo(true));
139-
}
140133

141134
// Remove temp file
142-
unlink($this->tempDir . '/' . $filenameParts["basename"]);
135+
@unlink($this->tempDir . '/' . $filenameParts["basename"]);
143136

144-
return true;
137+
return ($res == 0) ? false : true;
145138
}
146139

147140
/**
148-
* Find if given file name exist in archive (Emulate ZipArchive locateName())
141+
* Returns the index of the entry in the archive (emulate \ZipArchive)
149142
*
150-
* @param string $filename Filename for the file in zip archive
151-
* @return boolean
143+
* @param string $filename Filename for the file in zip archive
144+
* @return integer|false
152145
*/
153146
public function locateName($filename)
154147
{
@@ -163,42 +156,25 @@ public function locateName($filename)
163156
}
164157
}
165158

166-
return ($listIndex > -1);
159+
return ($listIndex > -1) ? $listIndex : false;
167160
}
168161

169162
/**
170-
* Extract file from archive by given file name (Emulate ZipArchive getFromName())
163+
* Extract file from archive by given file name (emulate \ZipArchive)
171164
*
172165
* @param string $filename Filename for the file in zip archive
173-
* @return string $contents File string contents
166+
* @return string|false $contents File string contents
174167
*/
175168
public function getFromName($filename)
176169
{
177-
$list = $this->zip->listContent();
178-
$listCount = count($list);
179-
$listIndex = -1;
180-
$contents = null;
170+
$listIndex = $this->locateName($filename);
171+
$contents = false;
181172

182-
for ($i = 0; $i < $listCount; ++$i) {
183-
if (strtolower($list[$i]["filename"]) == strtolower($filename) ||
184-
strtolower($list[$i]["stored_filename"]) == strtolower($filename)) {
185-
$listIndex = $i;
186-
break;
187-
}
188-
}
189-
190-
if ($listIndex != -1) {
173+
if ($listIndex !== false) {
191174
$extracted = $this->zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
192175
} else {
193176
$filename = substr($filename, 1);
194-
$listIndex = -1;
195-
for ($i = 0; $i < $listCount; ++$i) {
196-
if (strtolower($list[$i]["filename"]) == strtolower($filename) ||
197-
strtolower($list[$i]["stored_filename"]) == strtolower($filename)) {
198-
$listIndex = $i;
199-
break;
200-
}
201-
}
177+
$listIndex = $this->locateName($filename);
202178
$extracted = $this->zip->extractByIndex($listIndex, PCLZIP_OPT_EXTRACT_AS_STRING);
203179
}
204180
if ((is_array($extracted)) && ($extracted != 0)) {
@@ -209,10 +185,11 @@ public function getFromName($filename)
209185
}
210186

211187
/**
212-
* Returns the name of an entry using its index
188+
* Returns the name of an entry using its index (emulate \ZipArchive)
213189
*
214190
* @param integer $index
215191
* @return string|false
192+
* @since 0.10.0
216193
*/
217194
public function getNameIndex($index)
218195
{
@@ -223,4 +200,39 @@ public function getNameIndex($index)
223200
return false;
224201
}
225202
}
203+
204+
/**
205+
* Extract the archive contents (emulate \ZipArchive)
206+
*
207+
* @param string $destination
208+
* @param string|array $entries
209+
* @return boolean
210+
* @since 0.10.0
211+
*/
212+
public function extractTo($destination, $entries = null)
213+
{
214+
if (!is_dir($destination)) {
215+
return false;
216+
}
217+
218+
// Extract all files
219+
if (is_null($entries)) {
220+
$result = $this->zip->extract(PCLZIP_OPT_PATH, $destination);
221+
return ($result > 0) ? true : false;
222+
}
223+
224+
// Extract by entries
225+
if (!is_array($entries)) {
226+
$entries = array($entries);
227+
}
228+
foreach ($entries as $entry) {
229+
$entryIndex = $this->locateName($entry);
230+
$result = $this->zip->extractByIndex($entryIndex, PCLZIP_OPT_PATH, $destination);
231+
if ($result <= 0) {
232+
return false;
233+
}
234+
}
235+
236+
return true;
237+
}
226238
}

src/PhpWord/Writer/HTML.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,9 @@ private function getBase64ImageData(Image $element)
714714
if ($element->getSourceType() == Image::SOURCE_ARCHIVE) {
715715
$source = substr($source, 6);
716716
list($zipFilename, $imageFilename) = explode('#', $source);
717-
$zip = new \ZipArchive();
717+
718+
$zipClass = \PhpOffice\PhpWord\Settings::getZipClass();
719+
$zip = new $zipClass();
718720
if ($zip->open($zipFilename) !== false) {
719721
if ($zip->locateName($imageFilename)) {
720722
$zip->extractTo($this->getTempDir(), $imageFilename);

src/PhpWord/Writer/ODText.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public function __construct(PhpWord $phpWord = null)
5050
*
5151
* @param string $filename
5252
* @throws \PhpOffice\PhpWord\Exception\Exception
53-
* @todo Not in \ZipArchive::CM_STORE mode
5453
*/
5554
public function save($filename = null)
5655
{

src/PhpWord/Writer/PDF/AbstractRenderer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public function getPaperSize()
109109
/**
110110
* Set Paper Size
111111
*
112-
* @param string $pValue Paper size = PAPERSIZE_A4
112+
* @param int $pValue Paper size = PAPERSIZE_A4
113113
* @return self
114114
*/
115115
public function setPaperSize($pValue = 9)

src/PhpWord/Writer/Word2007.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@
99

1010
namespace PhpOffice\PhpWord\Writer;
1111

12-
use PhpOffice\PhpWord\Exception\Exception;
1312
use PhpOffice\PhpWord\PhpWord;
1413
use PhpOffice\PhpWord\Media;
1514
use PhpOffice\PhpWord\Element\Section;
15+
use PhpOffice\PhpWord\Exception\Exception;
1616
use PhpOffice\PhpWord\Writer\Word2007\ContentTypes;
17-
use PhpOffice\PhpWord\Writer\Word2007\Rels;
1817
use PhpOffice\PhpWord\Writer\Word2007\DocProps;
1918
use PhpOffice\PhpWord\Writer\Word2007\Document;
2019
use PhpOffice\PhpWord\Writer\Word2007\Footer;
20+
use PhpOffice\PhpWord\Writer\Word2007\Header;
2121
use PhpOffice\PhpWord\Writer\Word2007\Notes;
2222
use PhpOffice\PhpWord\Writer\Word2007\Numbering;
23-
use PhpOffice\PhpWord\Writer\Word2007\Header;
24-
use PhpOffice\PhpWord\Writer\Word2007\Styles;
23+
use PhpOffice\PhpWord\Writer\Word2007\Rels;
2524
use PhpOffice\PhpWord\Writer\Word2007\Settings;
25+
use PhpOffice\PhpWord\Writer\Word2007\Styles;
2626
use PhpOffice\PhpWord\Writer\Word2007\WebSettings;
2727

2828
/**
@@ -199,7 +199,8 @@ private function addFileToPackage($objZip, $source, $target)
199199
$source = substr($source, 6);
200200
list($zipFilename, $imageFilename) = explode('#', $source);
201201

202-
$zip = new \ZipArchive();
202+
$zipClass = \PhpOffice\PhpWord\Settings::getZipClass();
203+
$zip = new $zipClass();
203204
if ($zip->open($zipFilename) !== false) {
204205
if ($zip->locateName($imageFilename)) {
205206
$zip->extractTo($this->getTempDir(), $imageFilename);

src/PhpWord/Writer/Word2007/WebSettings.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
namespace PhpOffice\PhpWord\Writer\Word2007;
1111

12-
use PhpOffice\PhpWord\Shared\XMLWriter;
13-
1412
/**
1513
* Word2007 web settings part writer
1614
*/

0 commit comments

Comments
 (0)