What is the purpose of using ZipArchive in PHP?

ZipArchive in PHP is used to create, open, and extract files from zip archives. It allows developers to easily compress and decompress files and directories, making it useful for tasks like file backup, file transfer, and reducing file size for storage or transmission. By using ZipArchive, developers can efficiently work with zip archives in their PHP applications.

// Create a new ZipArchive object
$zip = new ZipArchive;

// Open a zip file for reading
if ($zip->open('example.zip') === TRUE) {
    // Extract all files to a specified directory
    $zip->extractTo('extracted_files/');
    
    // Close the zip file
    $zip->close();
    echo 'Files extracted successfully';
} else {
    echo 'Failed to open the zip file';
}