创建一个新的 zip 文件
$zip = new ZipArchive;
if ($zip->open('test_new.zip', ZipArchive::CREATE) === TRUE)
{
// Add files to the zip file
$zip->addFile('test.txt');
$zip->addFile('test.pdf');
// Add random.txt file to zip and rename it to newfile.txt
$zip->addFile('random.txt', 'newfile.txt');
// Add a file new.txt file to zip using the text specified
$zip->addFromString('new.txt', 'text to be added to the new.txt file');
// All files are added, so close the zip file.
$zip->close();
}
覆盖现有的 zip 文件
(使用 OVERWRITE 时,文件必须已经存在,如果不存在,则操作失败。)
$zip = new ZipArchive;
if ($zip->open('test_overwrite.zip', ZipArchive::OVERWRITE) === TRUE)
{
// Add file to the zip file
$zip->addFile('test.txt');
$zip->addFile('test.pdf');
// All files are added, so close the zip file.
$zip->close();
}
创建一个新的 zip 文件并将文件添加到文件夹中
$zip = new ZipArchive;
if ($zip->open('test_folder.zip', ZipArchive::CREATE) === TRUE)
{
// Add files to the zip file inside demo_folder
$zip->addFile('text.txt', 'demo_folder/test.txt');
$zip->addFile('test.pdf', 'demo_folder/test.pdf');
// Add random.txt file to zip and rename it to newfile.txt and store in demo_folder
$zip->addFile('random.txt', 'demo_folder/newfile.txt');
// Add a file demo_folder/new.txt file to zip using the text specified
$zip->addFromString('demo_folder/new.txt', 'text to be added to the new.txt file');
// All files are added, so close the zip file.
$zip->close();
}
创建一个新的 zip 文件并将文件移动到不同的文件夹中
$zip = new ZipArchive;
if ($zip->open('test_folder_change.zip', ZipArchive::CREATE) === TRUE)
{
// Add files to the zip file
$zip->addFile('text.txt', 'demo_folder/test.txt');
$zip->addFile('test.pdf', 'demo_folder1/test.pdf');
// All files are added, so close the zip file.
$zip->close();
}
使用目录中的所有文件创建一个 zip 文件
$zip = new ZipArchive;
if ($zip->open('test_dir.zip', ZipArchive::OVERWRITE) === TRUE)
{
if ($handle = opendir('demo_folder'))
{
// Add all files inside the directory
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != ".." && !is_dir('demo_folder/' . $entry))
{
$zip->addFile('demo_folder/' . $entry);
}
}
closedir($handle);
}
$zip->close();
}
将多个文件和目录添加到一个 zip 文件
$zip = new ZipArchive;
if ($zip->open('test_files_dirs.zip', ZipArchive::OVERWRITE) === TRUE)
{
// Add directory1
if ($handle = opendir('demo_folder/directory1/'))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
$zip->addFile('demo_folder/directory1/' . $entry);
}
}
closedir($handle);
}
// Add directory2
if ($handle = opendir('demo_folder/directory2/'))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
$zip->addFile('demo_folder/directory2/' . $entry);
}
}
closedir($handle);
}
// Add directory3
if ($handle = opendir('demo_folder/directory3/'))
{
while (false !== ($entry = readdir($handle)))
{
if ($entry != "." && $entry != "..")
{
$zip->addFile('demo_folder/directory3/' . $entry);
}
}
closedir($handle);
}
// Add more files
$zip->addFile('demo_folder/index.txt');
$zip->close();
}
资料来源:https ://www.virendrachandak.com/techtalk/how-to-create-a-zip-file-using-php/