Skip to content

Commit

Permalink
ZipAdapter: Allow to add a file with no compression (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
Progi1984 authored Sep 3, 2024
1 parent 1e968b2 commit 05bb7bd
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 28 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
with:
php-version: ${{ matrix.php }}
extensions: gd, xml, zip
coverage: xdebug
coverage: ${{ (matrix.php == '7.3') && 'xdebug' || 'none' }}

- name: Generate Locale (for tests)
run: sudo locale-gen de_DE.UTF-8 && sudo update-locale
Expand All @@ -86,6 +86,11 @@ jobs:
run: composer install --ansi --prefer-dist --no-interaction --no-progress

- name: Run phpunit
if: matrix.php != '7.3'
run: ./vendor/bin/phpunit -c phpunit.xml.dist --no-coverage

- name: Run phpunit
if: matrix.php == '7.3'
run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover build/clover.xml

- name: Upload coverage results to Coveralls
Expand Down
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
[![Latest Stable Version](https://poser.pugx.org/phpoffice/common/v/stable.png)](https://packagist.org/packages/phpoffice/common)
[![PHPOffice\Common](https://github.com/PHPOffice/Common/actions/workflows/php.yml/badge.svg)](https://github.com/PHPOffice/Common/actions/workflows/php.yml)
[![Coverage Status](https://coveralls.io/repos/github/PHPOffice/Common/badge.svg?branch=develop)](https://coveralls.io/github/PHPOffice/Common?branch=develop)
[![Total Downloads](https://poser.pugx.org/phpoffice/common/downloads.png)](https://packagist.org/packages/phpoffice/common)
[![License](https://poser.pugx.org/phpoffice/common/license.png)](https://packagist.org/packages/phpoffice/common)
[![Join the chat at https://gitter.im/PHPOffice/Common](https://img.shields.io/badge/GITTER-join%20chat-green.svg)](https://gitter.im/PHPOffice/Common)
[![Latest Stable Version](https://poser.pugx.org/phpoffice/common/v)](https://packagist.org/packages/phpoffice/common)
[![Coverage Status](https://coveralls.io/repos/github/PHPOffice/Common/badge.svg?branch=master)](https://coveralls.io/github/PHPOffice/Common?branch=master)
[![Total Downloads](https://poser.pugx.org/phpoffice/common/downloads)](https://packagist.org/packages/phpoffice/common)
[![License](https://poser.pugx.org/phpoffice/common/license)](https://packagist.org/packages/phpoffice/common)

Branch Master : [![PHPOffice\Common](https://github.com/PHPOffice/Common/actions/workflows/php.yml/badge.svg?branch=master)](https://github.com/PHPOffice/Common/actions/workflows/php.yml)

PHPOffice Common is a library written in pure PHP that provides a set of components for PHPOffice librairies.

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.9
1.0.3
15 changes: 13 additions & 2 deletions src/Common/Adapter/Zip/PclZipAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,26 @@ public function close()
return $this;
}

public function addFromString($localname, $contents)
public function addFromString(string $localname, string $contents, bool $withCompression = true)
{
$pathData = pathinfo($localname);

$hFile = fopen($this->tmpDir . '/' . $pathData['basename'], 'wb');
fwrite($hFile, $contents);
fclose($hFile);

$res = $this->oPclZip->add($this->tmpDir . '/' . $pathData['basename'], PCLZIP_OPT_REMOVE_PATH, $this->tmpDir, PCLZIP_OPT_ADD_PATH, $pathData['dirname']);
$params = [
$this->tmpDir . '/' . $pathData['basename'],
PCLZIP_OPT_REMOVE_PATH,
$this->tmpDir,
PCLZIP_OPT_ADD_PATH,
$pathData['dirname'],
];
if (!$withCompression) {
$params[] = PCLZIP_OPT_NO_COMPRESSION;
}

$res = $this->oPclZip->add(...$params);
if ($res == 0) {
throw new \Exception('Error zipping files : ' . $this->oPclZip->errorInfo(true));
}
Expand Down
5 changes: 4 additions & 1 deletion src/Common/Adapter/Zip/ZipArchiveAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,14 @@ public function close()
return $this;
}

public function addFromString($localname, $contents)
public function addFromString(string $localname, string $contents, bool $withCompression = true)
{
if ($this->oZipArchive->addFromString($localname, $contents) === false) {
throw new \Exception('Error zipping files : ' . $localname);
}
if (!$withCompression) {
$this->oZipArchive->setCompressionName($localname, \ZipArchive::CM_STORE);
}

return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Adapter/Zip/ZipInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ public function close();
*
* @throws \Exception
*/
public function addFromString($localname, $contents);
public function addFromString(string $localname, string $contents, bool $withCompression = true);
}
2 changes: 1 addition & 1 deletion src/Common/Microsoft/PasswordEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class PasswordEncoder
*
* @return string
*/
public static function hashPassword(string $password, string $algorithmName = self::ALGORITHM_SHA_1, string $salt = null, int $spinCount = 10000)
public static function hashPassword(string $password, string $algorithmName = self::ALGORITHM_SHA_1, ?string $salt = null, int $spinCount = 10000)
{
$origEncoding = mb_internal_encoding();
mb_internal_encoding('UTF-8');
Expand Down
12 changes: 6 additions & 6 deletions src/Common/XMLReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getDomFromString(string $content)
*
* @return \DOMNodeList<\DOMElement>
*/
public function getElements(string $path, \DOMElement $contextNode = null)
public function getElements(string $path, ?\DOMElement $contextNode = null)
{
if ($this->dom === null) {
return new \DOMNodeList();
Expand Down Expand Up @@ -154,7 +154,7 @@ public function registerNamespace($prefix, $namespaceURI)
*
* @return \DOMElement|null
*/
public function getElement($path, \DOMElement $contextNode = null): ?\DOMElement
public function getElement($path, ?\DOMElement $contextNode = null): ?\DOMElement
{
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
Expand All @@ -173,7 +173,7 @@ public function getElement($path, \DOMElement $contextNode = null): ?\DOMElement
*
* @return string|null
*/
public function getAttribute($attribute, \DOMElement $contextNode = null, $path = null)
public function getAttribute($attribute, ?\DOMElement $contextNode = null, ?string $path = null)
{
$return = null;
if ($path !== null) {
Expand All @@ -200,7 +200,7 @@ public function getAttribute($attribute, \DOMElement $contextNode = null, $path
*
* @return string|null
*/
public function getValue($path, \DOMElement $contextNode = null)
public function getValue($path, ?\DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);
if ($elements->length > 0) {
Expand All @@ -218,7 +218,7 @@ public function getValue($path, \DOMElement $contextNode = null)
*
* @return int
*/
public function countElements($path, \DOMElement $contextNode = null)
public function countElements($path, ?\DOMElement $contextNode = null)
{
$elements = $this->getElements($path, $contextNode);

Expand All @@ -233,7 +233,7 @@ public function countElements($path, \DOMElement $contextNode = null)
*
* @return bool
*/
public function elementExists($path, \DOMElement $contextNode = null)
public function elementExists($path, ?\DOMElement $contextNode = null)
{
return $this->getElements($path, $contextNode)->length > 0;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Common/XMLWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class XMLWriter extends \XMLWriter
* @param string $pTemporaryStorageDir Temporary storage folder
* @param bool $compatibility
*/
public function __construct($pTemporaryStorage = self::STORAGE_MEMORY, $pTemporaryStorageDir = null, $compatibility = false)
public function __construct(int $pTemporaryStorage = self::STORAGE_MEMORY, ?string $pTemporaryStorageDir = null, bool $compatibility = false)
{
// Open temporary storage
if ($pTemporaryStorage == self::STORAGE_MEMORY) {
Expand Down Expand Up @@ -121,7 +121,7 @@ public function getData()
*
* @return void
*/
public function writeElementBlock(string $element, $attributes, string $value = null)
public function writeElementBlock(string $element, $attributes, ?string $value = null)
{
$this->startElement($element);
if (!is_array($attributes)) {
Expand All @@ -143,7 +143,7 @@ public function writeElementBlock(string $element, $attributes, string $value =
*
* @return void
*/
public function writeElementIf(bool $condition, string $element, string $attribute = null, $value = null)
public function writeElementIf(bool $condition, string $element, ?string $attribute = null, $value = null)
{
if ($condition) {
if (is_null($attribute)) {
Expand Down
36 changes: 31 additions & 5 deletions tests/Common/Tests/Adapter/Zip/AbstractZipAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ abstract class AbstractZipAdapter extends \PHPUnit\Framework\TestCase
/**
* Returns a new instance of the adapter to test
*
* @return \PhpOffice\Common\Adapter\Zip\ZipInterface
* @return ZipInterface
*/
abstract protected function createAdapter(): ZipInterface;

Expand Down Expand Up @@ -50,17 +50,43 @@ public function testClose(): void
$this->assertSame($adapter, $adapter->close());
}

public function testAddFromString(): void
public function testAddFromStringWithCompression(): void
{
$expectedPath = 'file.test';
$expectedContent = 'Content';
$expectedPath = 'file.png';
$expectedContent = file_get_contents(
PHPOFFICE_COMMON_TESTS_BASE_DIR
. DIRECTORY_SEPARATOR . 'resources'
. DIRECTORY_SEPARATOR . 'images'
. DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png'
);

$adapter = $this->createAdapter();
$adapter->open($this->zipTest);
$this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent));
$this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent, true));
$adapter->close();

$this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath));
$this->assertTrue(TestHelperZip::assertFileIsCompressed($this->zipTest, $expectedPath));
$this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent));
}

public function testAddFromStringWithNoCompression(): void
{
$expectedPath = 'file.png';
$expectedContent = file_get_contents(
PHPOFFICE_COMMON_TESTS_BASE_DIR
. DIRECTORY_SEPARATOR . 'resources'
. DIRECTORY_SEPARATOR . 'images'
. DIRECTORY_SEPARATOR . 'PHPPowerPointLogo.png'
);

$adapter = $this->createAdapter();
$adapter->open($this->zipTest);
$this->assertSame($adapter, $adapter->addFromString($expectedPath, $expectedContent, false));
$adapter->close();

$this->assertTrue(TestHelperZip::assertFileExists($this->zipTest, $expectedPath));
$this->assertFalse(TestHelperZip::assertFileIsCompressed($this->zipTest, $expectedPath));
$this->assertTrue(TestHelperZip::assertFileContent($this->zipTest, $expectedPath, $expectedContent));
}
}
12 changes: 12 additions & 0 deletions tests/Common/Tests/_includes/TestHelperZip.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,16 @@ public static function assertFileContent(string $fileZip, string $path, string $

return true;
}

public static function assertFileIsCompressed(string $fileZip, string $path): bool
{
$oZip = new \ZipArchive();
$oZip->open($fileZip);
$stat = $oZip->statName($path);

// size: uncompressed
// comp_size: compressed

return $stat['size'] > $stat['comp_size'];
}
}
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@
});

require_once __DIR__ . '/../src/Common/Autoloader.php';
\PhpOffice\Common\Autoloader::register();
PhpOffice\Common\Autoloader::register();

0 comments on commit 05bb7bd

Please sign in to comment.