PhpSpreadsheet icon indicating copy to clipboard operation
PhpSpreadsheet copied to clipboard

ZipStream\ZipStream::__construct(): Argument #1 ($comment) must be of type string, null given, called in \vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\ZipStream2.php on line 19

Open TwanaAssad opened this issue 1 year ago • 17 comments

This is:

- [

// Create new Spreadsheet object
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();

// add code that show the issue here...

If this is an issue with reading a specific spreadsheet file, then it may be appropriate to provide a sample file that demonstrates the problem; but please keep it as small as possible, and sanitize any confidential information before uploading.

What features do you think are causing the issue

  • [ ] Reader
  • [ ] Writer
  • [ ] Styles
  • [ ] Data Validations
  • [ ] Formula Calculations
  • [ ] Charts
  • [ ] AutoFilter
  • [ ] Form Elements

Does an issue affect all spreadsheet file formats? If not, which formats are affected?

Which versions of PhpSpreadsheet and PHP are affected?

TwanaAssad avatar Jun 15 '23 09:06 TwanaAssad

@TwanaAssad Is it possible that you have both ZipStream v2 and v3 installed at the same time?

If I had to guess, ZipStream\Option\Archive (from v2) exists and ZipStream\ZipStream resolves to v3 where comment is the argument in position 1.

Try cleanly reinstalling all your dependencies.

maennchen avatar Jun 15 '23 12:06 maennchen

I have the same problem in Laravel, I tried:

composer clear-cache composer install But still same problem, the PhpSpreadsheet stopped working totally, it's a disaster for my proyect...

bitterfountain avatar Jun 17 '23 01:06 bitterfountain

I found a bad solution but I needed urgently, changing vendor class: file: ZipStream0.php

Code:

namespace PhpOffice\PhpSpreadsheet\Writer;

use ZipStream\Option\Archive;
use ZipStream\ZipStream;

class ZipStream0
{
    /**
     * @param resource $fileHandle
     */
    public static function newZipStream($fileHandle): ZipStream
    {
        /* the problem is in ZipStream2
        return class_exists(Archive::class) ? ZipStream2::newZipStream($fileHandle) : ZipStream3::newZipStream($fileHandle);
        */
        return ZipStream3::newZipStream($fileHandle);
    }
}

bitterfountain avatar Jun 17 '23 02:06 bitterfountain

If you have ZipStream2 installed, Archive::class should exist. If you have ZipStream3 installed, Archive::class should not exist. From your description, it sounds like you have ZipStream3 but also have Archive::class. So let's try to verify that to start.

Question 1 - Does directory vendor/maennchen/zipstream-php/src/Option/Archive exist on your system?

Question 2 - Does vendor/maennchen/zipstream-php/src/ZipStream.php have code that looks like:

public function __construct(?string $name = null, ?ArchiveOptions $opt = null)

Or does it look like:

    public function __construct(
        private OperationMode $operationMode = OperationMode::NORMAL,
        private readonly string $comment = '',
        $outputStream = null,
        private readonly CompressionMethod $defaultCompressionMethod = CompressionMethod::DEFLATE,
        private readonly int $defaultDeflateLevel = 6,
        private readonly bool $enableZip64 = true,
        private readonly bool $defaultEnableZeroHeader = true,
        private bool $sendHttpHeaders = true,
        ?Closure $httpHeaderCallback = null,
        private readonly ?string $outputName = null,
        private readonly string $contentDisposition = 'attachment',
        private readonly string $contentType = 'application/x-zip',
        private bool $flushOutput = false,
    ) {

oleibman avatar Jun 17 '23 14:06 oleibman

Question 1: vendor/maennchen/zipstream-php/src/Option/Archive Do not exist in my vendor

Question 2: Yes, code it's similar to the second you said:

public function __construct(
        private readonly string $comment = '',
        $outputStream = null,
        private readonly CompressionMethod $defaultCompressionMethod = CompressionMethod::DEFLATE,
        private readonly int $defaultDeflateLevel = 6,
        private readonly bool $enableZip64 = true,
        private readonly bool $defaultEnableZeroHeader = true,
        private bool $sendHttpHeaders = true,
        ?Closure $httpHeaderCallback = null,
        private readonly ?string $outputName = null,
        private readonly string $contentDisposition = 'attachment',
        private readonly string $contentType = 'application/x-zip',
        private bool $flushOutput = false,
    ) {
        $this->outputStream = self::normalizeStream($outputStream);
        $this->httpHeaderCallback = $httpHeaderCallback ?? header(...);
    }

bitterfountain avatar Jun 17 '23 17:06 bitterfountain

I had the same problem with Codeigniter on Ubuntu server. I solved this way. I removed Spreadsheet with Composer. I cleared the cache with Composer. I deleted all the vendor folder. I installed Spreadsheet with composer. I uploaded all new vendor folder. it worked.

Php 8.2 CI 4.3.5

I hope it can help

daveaki avatar Jun 17 '23 19:06 daveaki

THX daveaki! It worked perfectly!

bitterfountain avatar Jun 18 '23 11:06 bitterfountain

Thank you @daveaki for helping to unravel this mystery.

oleibman avatar Jun 19 '23 00:06 oleibman

@daveaki thanks, you helped me .

deloz avatar Jul 05 '23 04:07 deloz

recent versions of zipstream require an operation mode as first argument

public function __construct( private OperationMode $operationMode = OperationMode::NORMAL,

before it was

public function __construct(?string $name = null, ?ArchiveOptions $opt = null) {

so i guess that's the issue

lekoala avatar Jul 05 '23 12:07 lekoala

Thanks so much @deloz , I also had the same issue and your steps worked. php 8.0.2

danzisky avatar Jul 27 '23 08:07 danzisky

Adding this dependency to my composer.json did workaround the issue:

		"maennchen/zipstream-php": "3.0.*",

I guess the quick fix for this issue is to just adjust the dependency version of maennchen/zipstream-php in this repository in a similar fashion since minor version updates of that dependency seemingly are not backwards compatible.

DaDummy avatar Aug 15 '23 12:08 DaDummy

For php 8.2 compatibility it appears

		"myclabs/php-enum": "^1.8",

also needs to be added.

DaDummy avatar Aug 15 '23 12:08 DaDummy

@DaDummy ZipStream v3 uses native enumns and does not need any dependencies.

Also the guarantee of stability in function arguments is only on named arguments, not the order since there are so many.

maennchen avatar Mar 26 '24 10:03 maennchen

Adding this dependency to my composer.json did workaround the issue:

		"maennchen/zipstream-php": "3.0.*",

I guess the quick fix for this issue is to just adjust the dependency version of maennchen/zipstream-php in this repository in a similar fashion since minor version updates of that dependency seemingly are not backwards compatible.

This solved it for me thank you :-)

nbtafelberg avatar Apr 13 '24 07:04 nbtafelberg

Another case for this error is if any other part of your code is requiring zipstream in version 2. In my case, I am using this library in a wordpress plugin. However, another wordpress plugin is using zipstream 2 which resulted in this error (zipstream2 being used instead of zipstream3).

hubtraum avatar Jun 12 '24 07:06 hubtraum

i fixed by require-ing from v3 to v2 and now with zipstream 2 is working now.

p3x-robot avatar Jul 16 '24 06:07 p3x-robot