Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Gz
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
2 / 2
17
100.00% covered (success)
100.00%
1 / 1
 pack
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
10
 unpack
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2/**
3 * Jingga
4 *
5 * PHP Version 8.1
6 *
7 * @package   phpOMS\Utils\IO\Zip
8 * @copyright Dennis Eichhorn
9 * @license   OMS License 2.0
10 * @version   1.0.0
11 * @link      https://jingga.app
12 */
13declare(strict_types=1);
14
15namespace phpOMS\Utils\IO\Zip;
16
17/**
18 * Zip class for handling zip files.
19 *
20 * Providing basic zip support
21 *
22 * @package phpOMS\Utils\IO\Zip
23 * @license OMS License 2.0
24 * @link    https://jingga.app
25 * @since   1.0.0
26 */
27class Gz implements ArchiveInterface
28{
29    /**
30     * {@inheritdoc}
31     */
32    public static function pack(string | array $source, string $destination, bool $overwrite = false) : bool
33    {
34        $destination = \strtr($destination, '\\', '/');
35        if ($destination === false
36            || \is_array($source)
37            || (!$overwrite && \is_file($destination))
38            || !\is_file($source)
39        ) {
40            return false;
41        }
42
43        $gz  = \gzopen($destination, 'w');
44        $src = \fopen($source, 'r');
45        if ($gz === false || $src === false) {
46            return false; // @codeCoverageIgnore
47        }
48
49        while (!\feof($src)) {
50            $read = \fread($src, 4096);
51            \gzwrite($gz, $read === false ? '' : $read);
52        }
53
54        \fclose($src);
55
56        return \gzclose($gz);
57    }
58
59    /**
60     * {@inheritdoc}
61     */
62    public static function unpack(string $source, string $destination) : bool
63    {
64        $destination = \strtr($destination, '\\', '/');
65        if (\is_file($destination) || !\is_file($source)) {
66            return false;
67        }
68
69        $gz   = \gzopen($source, 'r');
70        $dest = \fopen($destination, 'w');
71        if ($gz === false || $dest === false) {
72            return false; // @codeCoverageIgnore
73        }
74
75        while (!\gzeof($gz) && ($read = \gzread($gz, 4096)) !== false) {
76            \fwrite($dest, $read);
77        }
78
79        \fclose($dest);
80
81        return \gzclose($gz);
82    }
83}