admin管理员组文章数量:1130349
<?php
// 创建一个新的ZIP文件
$zip = new ZipArchive();
$dir = '文件夹';
$zipFilePath = 'D:\要压缩的文件路径\first.zip';
// 如果压缩包创建成功
if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
// 设置密码
$password = '1212'; // 设置你的密码
$zip->setPassword($password);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($dir) + 1);
echo $relativePath.PHP_EOL;
// Add current file to archive
$zip->addFile($filePath, $relativePath);
$zip->setEncryptionName($relativePath, ZipArchive::EM_AES_256);
}
}
$zip->setPassword($password);
// 关闭ZIP文件
$zip->close();
echo "压缩包创建成功并设置了密码!";
} else {
echo "创建压缩包失败!";
}
<?php
// 创建一个新的ZIP文件
$zip = new ZipArchive();
$dir = '文件夹';
$zipFilePath = 'D:\要压缩的文件路径\first.zip';
// 如果压缩包创建成功
if ($zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
// 设置密码
$password = '1212'; // 设置你的密码
$zip->setPassword($password);
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($dir) + 1);
echo $relativePath.PHP_EOL;
// Add current file to archive
$zip->addFile($filePath, $relativePath);
$zip->setEncryptionName($relativePath, ZipArchive::EM_AES_256);
}
}
$zip->setPassword($password);
// 关闭ZIP文件
$zip->close();
echo "压缩包创建成功并设置了密码!";
} else {
echo "创建压缩包失败!";
}
本文标签: 文件夹密码phpZipArchive
版权声明:本文标题:php ZipArchive 压缩文件夹,并设置密码 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/jiaocheng/1763790922a2963848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论