Home  >  Article  >  Backend Development  >  How to merge multiple files into one compressed archive via PHP ZipArchive?

How to merge multiple files into one compressed archive via PHP ZipArchive?

WBOY
WBOYOriginal
2023-07-21 17:05:011502browse

How to merge multiple files into a compressed package through PHP ZipArchive?

Introduction:
During the development process, sometimes we need to package multiple files into a compressed package to facilitate downloading or transmission. PHP provides the ZipArchive class, which can easily implement this function. This article will introduce how to merge multiple files into a compressed package through the PHP ZipArchive class and provide corresponding code examples.

Step 1: Initialize the ZipArchive class
First, we need to initialize a ZipArchive object, which will be used to create and operate compressed packages.

$zip = new ZipArchive();

Step 2: Create a compressed package
Next, we need to create a new compressed package file and specify its name and path.

$zipName = 'archive.zip';
$zipPath = './path/to/zip/';
if ($zip->open($zipPath.$zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
    // 创建成功
} else {
    // 创建失败
}

Before opening the compressed package, we need to check whether the creation is successful. If the creation is successful, we can continue to add files to the compressed package; otherwise, we need to handle the creation failure.

Step 3: Add files to the compressed package
With an open compressed package, we can add files to the compressed package by calling the addFile() method.

$filePath = './path/to/file.txt';
$zipEntryName = 'file.txt';
$zip->addFile($filePath, $zipEntryName);

The addFile() method accepts two parameters: the path of the file to be added and the name in the compressed package. By repeatedly calling the addFile() method, we can add multiple files to the compressed package.

Step 4: Close the compressed package
After we add all files to the compressed package, we need to close and save the compressed package to ensure that all operations have been completed.

$zip->close();

Complete code example:

$zip = new ZipArchive();
$zipName = 'archive.zip';
$zipPath = './path/to/zip/';

if ($zip->open($zipPath.$zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
    $files = array(
        './path/to/file1.txt',
        './path/to/file2.txt',
        './path/to/file3.txt'
    );
    $zipEntryNames = array(
        'file1.txt',
        'file2.txt',
        'file3.txt'
    );

    for ($i = 0; $i < count($files); $i++) {
        $zip->addFile($files[$i], $zipEntryNames[$i]);
    }

    $zip->close();
    echo '压缩包创建成功!';
} else {
    echo '压缩包创建失败!';
}

Summary:
With the PHP ZipArchive class, we can easily merge multiple files into a compressed package. We can achieve this function by initializing the ZipArchive object, creating the archive, adding files to the archive and closing the archive. I hope this article helps you when dealing with file compression during development.

The above is the detailed content of How to merge multiple files into one compressed archive via PHP ZipArchive?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn