Skip to content

Commit

Permalink
feat(DocxDocument): add nelexa zip
Browse files Browse the repository at this point in the history
  • Loading branch information
voinmerk committed Nov 11, 2020
1 parent 05cb1ab commit a85e889
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
/vendor
composer.lock
*.cache
/tmp
/tmp
.idea
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
"vendor-dir": "./vendor"
},
"require": {
"ext-dom": "*",
"php": "^7.2",
"twig/twig":"^3.0"
"twig/twig":"^3.0",
"nelexa/zip": "^3.3"
},
"require-dev": {
"doctrine/coding-standard": "^6.0",
Expand Down
14 changes: 9 additions & 5 deletions src/DocxDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DOMDocument;
use DOMElement;
use Exception;
use ZipArchive;
use PhpZip\ZipFile;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;

Expand Down Expand Up @@ -40,6 +40,8 @@ public function __construct(string $path)

/**
* Extract (unzip) document contents
*
* @throws \PhpZip\Exception\ZipException
*/
private function extract(): void
{
Expand All @@ -49,8 +51,8 @@ private function extract(): void

mkdir($this->tmpDir);

$zip = new ZipArchive();
$zip->open($this->path);
$zip = new ZipFile();
$zip->openFile($this->path);
$zip->extractTo($this->tmpDir);
$zip->close();

Expand Down Expand Up @@ -222,13 +224,14 @@ private function getCellLen(array $cells): int
* Save the document to the target path
*
* @param string $path - target path
*
* @throws \PhpZip\Exception\ZipException
*/
public function save(string $path): void
{
$rootPath = realpath($this->tmpDir);

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip = new ZipFile();

$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
Expand All @@ -243,6 +246,7 @@ public function save(string $path): void
}
}

$zip->saveAsFile($path);
$zip->close();

$this->rrmdir($this->tmpDir);
Expand Down

0 comments on commit a85e889

Please sign in to comment.