Skip to content

Commit

Permalink
Merge pull request #44 from kiwilan/develop
Browse files Browse the repository at this point in the history
Password and override binary path for Windows
  • Loading branch information
ewilan-riviere authored Mar 8, 2024
2 parents 28a9bae + 9a77cbe commit 0861f86
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 16 deletions.
1 change: 1 addition & 0 deletions .github/workflows/run-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jobs:
iex "& {$(irm get.scoop.sh)} -RunAsAdmin"
scoop update
scoop install 7zip
scoop install imagemagick ghostscript ffmpeg
scoop install which
scoop checkup
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ vendor
.DS_Store
example
temp
CHANGELOG-draft.md
.phpunit.cache
13 changes: 1 addition & 12 deletions src/Processes/SevenZipProcess.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public static function make(string $path, ?string $password = null, ?string $bin
public static function test(bool $exception = true): bool
{
exec('7z', $output, $res);
// $process = new Process(['7z']);
// $process->run();

$isValid = $res === 0;
// $isValid = $process->isSuccessful();

// check if 7z is installed
if (! $isValid) {
Expand Down Expand Up @@ -82,25 +79,17 @@ public function execute(string $command, array $args): array
}

if ($this->binaryPath) {

$command = $this->binaryPath;
}

$command = "{$command} ".implode(' ', $args);

// $process = new Process([$command, ...$args]);
// $process->run();

// if (! $process->isSuccessful()) {
// throw new ProcessFailedException($process);
// }

try {
exec($command, $output, $res);
} catch (\Throwable $th) {
throw new \Error($th->getMessage());
}

// $output = explode(PHP_EOL, $output);
array_unshift($output, '');

return $output;
Expand Down
12 changes: 12 additions & 0 deletions src/Readers/BaseArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,24 @@ public function getFileItems(): array
*/
public function getFileItem(string $path): ?ArchiveItem
{
$original_path = $path;
if (PHP_OS_FAMILY === 'Windows') {
$path = str_replace('/', DIRECTORY_SEPARATOR, $path);
}

$files = array_filter($this->files, fn (ArchiveItem $item) => $item->getPath() === $path);

if (count($files) > 0) {
return reset($files);
}

if (PHP_OS_FAMILY === 'Windows') {
$files = array_filter($this->files, fn (ArchiveItem $item) => $item->getPath() === $original_path);
if (count($files) > 0) {
return reset($files);
}
}

return null;
}

Expand Down
3 changes: 2 additions & 1 deletion tests/ArchiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ function (Pest\Expectation $item) use ($ext) {

it('can handle archive with binary path', function (string $path) {
if (PHP_OS_FAMILY === 'Windows') {
$binary_path = 'C:\Users\runneradmin\scoop\shims\7z.EXE';
$current_user = exec('echo %USERNAME%');
$binary_path = "C:\\Users\\{$current_user}\\scoop\\apps\\7zip\\current\\7z.exe";
} else {
$binary_path = '/opt/homebrew/bin/7z';
}
Expand Down
6 changes: 3 additions & 3 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

expect($content)->toBeString();
expect($file)->toBeReadableFile();
})->skip(PHP_OS_FAMILY === 'Windows' || PHP_VERSION >= '8.3', 'Not supported on Windows');
});

it('can extract some files', function () {
$archive = Archive::read(PDF);
Expand All @@ -40,15 +40,15 @@
expect($paths)->toHaveCount(2);
expect($paths[0])->toBeString();
expect($paths[0])->toBeReadableFile();
})->skip(PHP_OS_FAMILY === 'Windows' || PHP_VERSION >= '8.3', 'Not supported on Windows');
});

it('can extract files', function () {
$archive = Archive::read(PDF);
$paths = $archive->extractAll(outputPath());

expect($paths)->toBeArray();
expect($paths)->toBeGreaterThanOrEqual(5);
})->skip(PHP_OS_FAMILY === 'Windows' || PHP_VERSION >= '8.3', 'Not supported on Windows');
});

it('can read metadata', function () {
$archive = Archive::read(PDF);
Expand Down

0 comments on commit 0861f86

Please sign in to comment.