Skip to content

Commit

Permalink
joined main progress with tar progress
Browse files Browse the repository at this point in the history
  • Loading branch information
insign committed Apr 7, 2024
1 parent cb7bc95 commit 801fe18
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 29 deletions.
62 changes: 40 additions & 22 deletions src/SevenZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class SevenZip
*/
protected int $lastProgress = -1;

protected int $divideProgressBy = 1;

/**
* The compression format to be used.
* @var ?string
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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 (
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
Expand All @@ -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)
Expand All @@ -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');
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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)
Expand All @@ -1426,6 +1443,7 @@ protected function executeTarBefore(): self
}

$sz->compress();

unset($sz);

$this->setAlreadyTarred(true);
Expand Down
14 changes: 7 additions & 7 deletions try.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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";

Expand All @@ -59,7 +59,7 @@
->extract();
echo "\n";
echo "Output: " . $output . "\n";
//

//echo "Deleting archive... ";
//unlink($archivePath);
//echo "✅\n";
Expand Down

0 comments on commit 801fe18

Please sign in to comment.