php-zip icon indicating copy to clipboard operation
php-zip copied to clipboard

setPassword corrupting files

Open bradley-varol opened this issue 6 years ago • 6 comments

Description
setPassword corrupts my CSV files

How to reproduce

$zipFile = new ZipFile;

$zipFile->addDir($dir); // directory with several very small .csv files

$zipFile->setPassword('password');

$zipFile->saveAsFile('path.zip')->close();

bradley-varol avatar Nov 16 '18 17:11 bradley-varol

Attach the created zip-archive and report the version of the library, PHP, OS.

Ne-Lexa avatar Nov 16 '18 17:11 Ne-Lexa

I won't be able to attach the created zip. It was the most recent version, PHP7.1 and Windows 10. I also had issues getting PHP's ZipArchive to work on W10.

bradley-varol avatar Nov 16 '18 18:11 bradley-varol

Have you solved this problem? My version is version php5.6. After setting the password, it will prompt that the file is damaged when decompressing

529834149 avatar Sep 14 '20 07:09 529834149

Sorry @529834149 but I no longer work on the project that I encountered this on, and I don't remember the solution. I think I used a different package!

bradley-varol avatar Sep 14 '20 16:09 bradley-varol

We're having the same issue here. Anyone know a workaround for this?

mbardelmeijer avatar Aug 22 '21 16:08 mbardelmeijer

I have tried to reproduce this issue on Windows 10.

When I extract the file with 7-ZIP the password-protected ZIP file can be extracted. But when I use the Windows 10 built-in function to extract ZIP archives from the (right-click) context, an unexpected error occurs.

This error usually occurs when a ZIP file is encrypted with a password, but Windows cannot detect that it is an encrypted file.

Error 0x80004005: Unspecified error

Then I tried to encrypt the ZIP files with different encryption methods and decrypt it with the Windows built-in function.

Here are the results:

This file was created with 7-ZIP

It seems that Windows (10) supports only the traditional PKWARE encryption and ZipCrypto, but not AES-*.

Read more

So, to make it work on Windows, just pass ZipEncryptionMethod::PKWARE as the second parameter.

Working example:

use PhpZip\Constants\ZipEncryptionMethod;
use PhpZip\ZipFile;
// ...

$zipFile = new ZipFile();
$zipFile->addDir(__DIR__ . '/csv');
$zipFile->setPassword('password', ZipEncryptionMethod::PKWARE);

$zipFile->saveAsFile(__DIR__ . '/zipfile-pkware.zip')->close();

odan avatar Aug 22 '21 19:08 odan