简体   繁体   English

如果文件使用 setEncryptionName 加密,如何在 PHP 中提取 zip 存档

[英]How to extract zip archive in PHP if file encrypted using setEncryptionName

I have created zip with encryption using setEncryptionName, as follows:我创建了 zip 并使用 setEncryptionName 进行加密,如下所示:

if($zip->open($zip_destination_real,\ZipArchive::CREATE) === TRUE) 
{
  $zip->addFile($filePath_real,'xyz.txt');   
  $zip->setEncryptionName('xyz.txt', \ZipArchive::EM_AES_256, '12345');         
  $zip->close();

}

Now, how to extract this zip file?现在,如何提取这个 zip 文件? extractTo function is returning false. extractTo function 返回 false。

$r = $zip->extractTo($dir_real); $r = $zip->extractTo($dir_real); var_dump($r); var_dump($r);

I use php 7.2我使用 php 7.2

Even when I manually extract the folder it asks for password.I enter 12345 as set, but error pops up, saying Error occured while extracting files.即使我手动提取它要求输入密码的文件夹。我按设置输入 12345,但弹出错误,说提取文件时发生错误。

You didn't set password correctly.你没有正确设置密码。

Zip files with password:带密码的 Zip 文件:

# Creating new zip object
$zip = new ZipArchive();
if ($zip->open('file.zip', ZipArchive::CREATE) === TRUE) {

    # Setting password here
    $zip->setPassword('12345');

    # Adding some files to zip
    $zip->addFile('some-file.txt');
    $zip->setEncryptionName('some-file.txt', ZipArchive::EM_AES_256);

    # Closing instance of zip object
    $zip->close();

    exit("Done! Your zip is ready!")
} else {
    exit("Whoops:( Failed to create zip.");
}

And unzip like this:并像这样解压缩:

# Creating new ZipArchive instance
$zip = new ZipArchive();

# Open file to read
if ($zip->open('file.zip') === true) {

    # Enter your password
    $zip->setPassword('12345');

    # Extract files to some destination
    # dirname(__FILE__) sets destination to directory of current file
    $zip->extractTo(dirname(__FILE__));

    # Closing instance of zip object
    $zip->close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将zip://包装器用于单个名称未知的文件的存档 - Using the zip:// wrapper for an archive with a single file whose name is unknown 无法使用zip存档在php中创建有效的zip文件? - unable to create a valid zip file in php using zip archive? PHP 7.2.7:尝试调用类“ZipArchive”的名为“setEncryptionName”的未定义方法 - PHP 7.2.7: Attempted to call an undefined method named “setEncryptionName” of class “ZipArchive” 将CRON与ZIPArchive对象一起使用以创建zip文件-拒绝小名次 - using CRON with ZIPArchive object to create zip file— pemmisions denied 无法使用 remi-php72 存储库在 Centos 7.6.1810 上加载动态库“zip.so” - Unable to load dynamic library 'zip.so' on Centos 7.6.1810 using remi-php72 repo 使用 PHP ZipArchive 将文件作为变量 - use PHP ZipArchive with file as variable OpenCart 需要加载 ZIP 扩展 - macOS Big Sur - ZIP extension needs to be loaded for OpenCart - macOS Big Sur PHP ZipArchive addFile停止工作 - PHP ZipArchive addFile stopped working 更改使用php创建的我的zip文件的名称 - Change the name of my zip file created with php 如何将php zip添加到服务器,我在谷歌云平台上使用wordpress - How to add php zip to server, I'm using wordpress on google cloud platform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM