maennchen/ZipStream-PHP

Zip multiple excel files

srodrigues37 opened this issue · 9 comments

Description of the problem

I make a symfony page to download a zip that contains multiple excel file generated with PhpSpreadsheet
When i open the zip downloaded first excel is ok but for the others excel says that they have a problem

Example code

I generate mulitple excels files with PhpSpreadsheet and return the content of the file

$writer = new Xlsx($this->spreadsheet);
$tmpfile = tempnam(self::FILE_DIRECTORY, $filename);
$writer->save($tmpfile);
$excelOutput = file_get_contents($tmpfile);
//...
return $excelOutput

And I retrun as a response a zip file with all of them

private function getZipResponse($outputs, $names)
    {
        $zipName = 'Documents-'.time().".zip";
        return  new StreamedResponse(function() use ($outputs, $names, $zipName) 
        {           
            $opt = new ArchiveOptions();
            $opt->setContentDisposition("attachment;filename=" . $zipName . "");
            $opt->setContentType('application/octet-stream');
            $opt->setZeroHeader(true);
            $opt->setSendHttpHeaders(true);

            $zip = new ZipStream($zipName, $opt);

            foreach (array_combine($names,$outputs) as $name => $content) {
                $date = date('Ymd');
                $fileName = $date . preg_replace('/[^a-zA-Z0-9_-]+/', '-', strtolower($name)) . ".xlsx";
                $zip->addFile($fileName, $content);
            }
            $zip->finish();
        });
    }

$content like this:
"PK����%\�P$�H�q����[Content_Types].xml���N�0�E�|E�-Jܲ@�5���*Q>�ēƪ_򸯿g��@��R��8Qr�\�x4����!�r6g�l������.r�1�I�X�QX)����� ��oF��LHl1gU���s,*0�3��қ��#"=��� .....

Informations

  • ZipStream-PHP version: 2.1
  • PHP version: 7.4.0

Please include any supplemental information you deem relevant to this issue.

Hello,

Can you try and add $opt->setFlushOutput(true)?

Also, for the broken files, can you head | tail them to see if there is something in there that shouldn't?

Yes i tried same issue. Each file works alone one file by zip ok multiple file first ok not the others.
I try with addfromstream addfromfile same issue

Are you sure it's an issue with ZipStream? Did you check the integrity of the files before getting zipped?

I’m sure, i can download file directly without zipstream or zipped, if i download one by one A B and C all ok, if i use zipstream and add file A and B, A is ok B is corrupted if i add B and C B is ok and C is corrupted. If I add A B and C A is ok Band C is corrupted.

It’s vers very Strange.

I use Windows zip to unzip.

Windows unzip is very unreliable. Can you try with 7zip please?

Same issue with 7zip

After some investigation it appears that the files is changed during the process:
image
here a sample of 3 files:
1 the excel generated not zip
2 the same excel zip in first postion
3 the same exel file zip in second position.

Is this issue only present with excel files? Can you try zipping different files and see if you have the same issue?

Fix it issue on generation of PhpSpreadsheet that have not been desalocated.
For information PhpSpreadsheet use ZipStream as internal zipper for excel file.