php-zip
php-zip copied to clipboard
setPassword corrupting files
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();
Attach the created zip-archive and report the version of the library, PHP, OS.
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.
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
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!
We're having the same issue here. Anyone know a workaround for this?
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:
- zipfile-aes-128.zip -> Error 0x80004005: Unspecified error
- zipfile-aes-192.zip -> Error 0x80004005: Unspecified error
- zipfile-aes-256.zip -> Error 0x80004005: Unspecified error
- zipfile-pkware.zip -> Successfully
This file was created with 7-ZIP
- 7zip-aes256.zip -> Error 0x80004005: Unspecified error
- 7zip-zipcrypto.zip -> Successfully
It seems that Windows (10) supports only the traditional PKWARE encryption and ZipCrypto, but not AES-*.
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();