You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP使用ZipArchive解压AES256加密Zip失败,ZipCrypto可成功求助

问题分析与解决方案

Hey there! Let's break down why your PHP code is failing to extract that AES-256 encrypted zip, even though ZipCrypto can handle it just fine.

The Root Cause

PHP's built-in ZipArchive class has a key limitation here: its setPassword() method only supports the legacy ZipCrypto encryption algorithm, not AES-256. Since your zip file uses AES-256 (a more secure modern standard), the ZipArchive extension bundled with your Windows XAMPP setup can't decrypt it—hence the misleading "wrong password?" error (even though your password is totally correct!).

Windows versions of PHP typically don't include AES support in their Zip extension by default, so this is a super common gotcha for new PHP devs working with encrypted zips.


Solution 1: Use a Third-Party PHP Library with AES Support

The easiest way to fix this without messing with system configurations is to use a dedicated library that supports AES-256 zip extraction. A great, lightweight option is chillerlan/php-zip.

  1. First, install the library via Composer (if you don't have Composer set up, grab it from getcomposer.org):
composer require chillerlan/php-zip
  1. Replace your existing code with this:
use chillerlan\Zip\Zip;
use chillerlan\Zip\ZipException;

try {
    // Initialize the zip handler
    $zip = new Zip('jajal.zip');
    // Set your encryption password
    $zip->setPassword('t3mp3-g3mbus');
    // Extract to your target directory
    $zip->extractTo('img/');
    
    echo 'Extraction successful!';
} catch(ZipException $e) {
    echo 'Extraction failed: ' . $e->getMessage();
}

Solution 2: Use 7-Zip via Command Line

If you don't want to use Composer, you can leverage the 7-Zip tool (which natively supports AES-256 zips) by calling it from PHP using the exec() function.

  1. Download and install 7-Zip from the official site (note your installation path—usually C:\Program Files\7-Zip\7z.exe).

  2. Update your PHP code like this:

$zipFile = 'jajal.zip';
$extractDir = 'img/';
$password = 't3mp3-g3mbus';
// Path to 7z.exe (wrap in quotes to handle spaces in the path)
$7zipPath = '"C:\Program Files\7-Zip\7z.exe"';

// Build the 7-Zip extraction command
// -x = extract, -y = overwrite without prompting, -p = password, -o = output directory
$command = "$7zipPath x -y -p\"$password\" \"$zipFile\" -o\"$extractDir\"";

// Execute the command and capture output/return status
exec($command, $output, $returnVar);

if ($returnVar === 0) {
    echo 'Extraction successful!';
} else {
    // Print error details if extraction fails
    echo 'Extraction failed: ' . implode(PHP_EOL, $output);
}

Note: Make sure exec() isn't disabled in your php.ini file (check the disable_functions directive). Also, double-check that all file paths are correctly quoted to avoid issues with spaces.


Bonus: Why Not Just Recompile ZipArchive?

While it's technically possible to recompile PHP's Zip extension with AES support, it's a tedious process on Windows—especially for a new developer. The solutions above are way more straightforward and reliable for most use cases.

内容的提问来源于stack exchange,提问作者Ponco Abdi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:52:44