0

I have used below code to download zip using ZipArchive by generating a pdf file from string. Random password is generated which is provided to use to open it.

        $zip = new ZipArchive();            
        $file = sprintf('%s/%s', sys_get_temp_dir(), $zipFile);
        if ($zip->open($file, ZipArchive::CREATE) === TRUE) {
            $zip->addFromString($pdfFile, $stringPdf); //add file
            $zip->setEncryptionName($pdfFile, ZipArchive::EM_AES_256, $pwd);
            $zip->close();
            header('Content-Description: File Download');
            header('Content-Type: application/zip');
            header('Content-Disposition: attachment; filename="' . $zipFile . '"');
            header("Pragma: no-cache");
            header("Expires: 0");
            header('Content-Length: \' . filesize($file).\'');                
            ob_end_clean();
            flush();
            if (readfile($file) !== false) {
                unlink($file);
            }
        }

Th issue is the Zip with PDF is downloaded properly and also no error is generated in browser. Still not sure why the downloaded zip cannot be extracted. Not sure how to debug it or could be an issue. Would be great If anyone can advice.

Module Information as below is same on QA and Staging environment but still download zip is working on both but extracting just doesnt work on Staging. It just doesn't verify the password. What can be causing this environment issue, If any?

zip

Zip => enabled
Zip version => 1.19.2
Libzip headers version => 1.7.3
Libzip library version => 1.8.0
BZIP2 compression => Yes
XZ compression => Yes
ZSTD compression => No
AES-128 encryption => Yes
--

ZLib Support => enabled
Stream Wrapper => compress.zlib://
Stream Filter => zlib.inflate, zlib.deflate
Compiled Version => 1.2.7
Linked Version => 1.2.7

Directive => Local Value => Master Value
zlib.output_compression => Off => Off
zlib.output_compression_level => -1 => -1
Mihir Patel
  • 230
  • 4
  • 22
  • Have you tried it first without encryption? – Mark Adler Oct 06 '21 at 22:22
  • I have tried it on my development env and then on QA env and works perfectly fine but not on staging and production. I cannot access staging and production env. so cannot figure out the issue. – Mihir Patel Oct 06 '21 at 23:57
  • 1
    Open the zip file in a raw editor such as XVI32, or possibly notepad, and confirm that the first bytes are PK, and the last bytes aren’t text. – Chris Haas Oct 07 '21 at 03:56
  • 1
    I will repeat. If the problem you are getting is with the decryption, then simplify, and try again without encryption to see if you can get a zip over and extracted at all. Simplify and isolate. – Mark Adler Oct 07 '21 at 17:32
  • Finally It was the issue with the default archive utility of Mac. When tried to decrypt with another utility it worked. Not sure why that happens because quite a few users were having this problem. Thank you guys for your time. I will delete this question now. – Mihir Patel Oct 07 '21 at 20:51

0 Answers0