diff --git a/src/SevenZip.php b/src/SevenZip.php index 0849b80..fd72096 100644 --- a/src/SevenZip.php +++ b/src/SevenZip.php @@ -76,6 +76,8 @@ class SevenZip */ protected int $lastProgress = -1; + protected int $divideProgressBy = 1; + /** * The compression format to be used. * @var ?string @@ -535,7 +537,7 @@ protected function runCommand(array $command, bool $secondary = false): string $process->getErrorOutput()); } - if (!$secondary) { + if (!$secondary && $this->getDivideProgressBy() === 1) { $this->setProgress(100); $this->reset(); } @@ -569,6 +571,8 @@ protected function parseProgress(string $output): void foreach ($lines as $line) { if (preg_match("/(\d+)%\s+\d+/", $line, $matches)) { $progress = intval($matches[1]); + $progress = floor($progress / $this->getDivideProgressBy()); + $this->setProgress($progress); } } @@ -587,15 +591,26 @@ public function getProgressCallback(): ?callable /** * Set the progress callback. * - * @param callable $callback The callback function to be called during the compression progress. + * @param ?callable $callback The callback function to be called during the compression progress. * @return $this The current instance of the SevenZip class. */ - public function setProgressCallback(callable $callback): self + public function setProgressCallback(?callable $callback): self { $this->progressCallback = $callback; return $this; } + public function getDivideProgressBy(): int + { + return $this->divideProgressBy; + } + + public function setDivideProgressBy(int $number = 1): SevenZip + { + $this->divideProgressBy = is_int($number) && $number > 0 ? $number : 1; + return $this; + } + protected function setProgress(int $progress): void { if ( @@ -620,12 +635,12 @@ public function getLastProgress(): int /** * Set the last reported progress. * - * @param int $lastProgress The last reported progress percentage. + * @param int $value The last reported progress percentage. * @return SevenZip The current instance of the SevenZip class. */ - protected function setLastProgress(int $lastProgress): self + protected function setLastProgress(int $value): self { - $this->lastProgress = $lastProgress; + $this->lastProgress = $value; return $this; } @@ -1148,6 +1163,9 @@ public function shouldDeleteSourceAfterExtract(): bool */ public function executeUntarAfter(string $tarFile, array $extractCommand): string { + $this->setProgress(20); + $this->setDivideProgressBy(5); + $sourceTar = str_replace('//', '/', $this->getTargetPath() . '/' . $tarFile); $sz = new self(); @@ -1159,9 +1177,7 @@ public function executeUntarAfter(string $tarFile, array $extractCommand): strin ->target($this->getTargetPath()); - if ($this->getProgressCallback() !== null) { - $sz->progress($this->getProgressCallback()); - } + $sz->progress($this->getProgressCallback()); // 'snoi' => store owner id in archive, extract owner id from archive (tar/Linux) @@ -1172,7 +1188,7 @@ public function executeUntarAfter(string $tarFile, array $extractCommand): strin if ($this->shouldKeepFileInfoOnTar()) { $sz ->addFlag('snoi') -// ->addFlag('snon') // @FIXME on linux causes a error "Segmentation fault" +// ->addFlag('snon') // @FIXME on linux causes an error "Segmentation fault" ->addFlag('mtc', 'on') ->addFlag('mta', 'on') ->addFlag('mtm', 'on'); @@ -1218,10 +1234,10 @@ public function deleteSourceAfterExtract(bool $delete = true): SevenZip /** * Set the progress callback using a fluent interface. * - * @param callable $callback The callback function to be called during the compression progress. + * @param ?callable $callback The callback function to be called during the compression progress. * @return $this The current instance of the SevenZip class. */ - public function progress(callable $callback): self + public function progress(?callable $callback): self { return $this->setProgressCallback($callback); } @@ -1345,6 +1361,13 @@ public function getEncryptNames(): ?bool return $this->encryptNames; } + /* + * Sets level of file analysis. + * + * @param int $level + * @return $this + */ + /** * Set whether to encrypt file names or not. * @@ -1367,13 +1390,6 @@ public function shouldForceTarBefore(): bool return $this->forceTarBefore; } - /* - * Sets level of file analysis. - * - * @param int $level - * @return $this - */ - /** * Tars the source file or directory before compressing. * @@ -1404,9 +1420,10 @@ protected function executeTarBefore(): self ->target($tarPath) ->source($sourcePath); - if ($this->getProgressCallback() !== null) { - $sz->progress($this->getProgressCallback()); - } + $partOfTotal = 5; + $sz->setDivideProgressBy($partOfTotal); + $this->setLastProgress(100 / $partOfTotal); + $sz->progress($this->getProgressCallback()); // 'snoi' => store owner id in archive, extract owner id from archive (tar/Linux) @@ -1426,6 +1443,7 @@ protected function executeTarBefore(): self } $sz->compress(); + unset($sz); $this->setAlreadyTarred(true); diff --git a/try.php b/try.php index 36b76f6..370b07e 100644 --- a/try.php +++ b/try.php @@ -8,11 +8,11 @@ $format = 'tar.7z'; $archivePath = "/Users/helio/zip.$format"; $extractPath = '/Users/helio/tmp'; -$fileToCompress = '/Users/helio/zip-tiny'; +$fileToCompress = '/Users/helio/zip-big'; $password = 'test2'; -unlink($archivePath); -unlink($extractPath . '/' . $fileToCompress); +@unlink($archivePath); +@unlink($extractPath . '/' . $fileToCompress); echo "Creating instance of SevenZip... "; $sevenZip = new SevenZip(); @@ -29,12 +29,12 @@ ->source($fileToCompress) ->target($archivePath) ->exclude('*.git/*') -// ->solid(true) + ->solid(true) // ->setTimeout(10) // ->ultra() - ->faster() +// ->faster() // ->copy() -->compress(); + ->compress(); echo "✅\n"; echo "Output: " . $output . "\n"; @@ -59,7 +59,7 @@ ->extract(); echo "✅\n"; echo "Output: " . $output . "\n"; -// + //echo "Deleting archive... "; //unlink($archivePath); //echo "✅\n";