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