
Zip in PHP
Zip in PHP
PHP provides functionality to work with zip files using the ZipArchive class, which allows you to create, read, and modify ZIP archive files. This class is part of the Zip extension in PHP, which is commonly used for compressing or extracting files.
1. Creating a ZIP Archive
You can create a new ZIP file and add files to it using the ZipArchive
class.
Example:
<?php$zip = new ZipArchive(); // Create an instance of ZipArchive class$filename = "example.zip";// Open the ZIP file (create it if it doesn't exist)if ($zip->open($filename, ZipArchive::CREATE) === TRUE) { // Add files to the archive $zip->addFile("file1.txt"); $zip->addFile("file2.txt"); // Close the archive $zip->close(); echo "ZIP file created successfully!";} else { echo "Failed to create the ZIP file.";}?>
ZipArchive::CREATE
: Used to create a new archive or open an existing archive for modification.addFile($filename)
: Adds a file to the ZIP archive.close()
: Closes the ZIP file and saves it.
2. Adding Files with a Specific Name in the Archive
You can specify a different name for the file within the archive (it won't use the original filename).
Example:
<?php$zip = new ZipArchive();$filename = "example.zip";if ($zip->open($filename, ZipArchive::CREATE) === TRUE) { // Add a file with a different name inside the archive $zip->addFile("file1.txt", "newfile1.txt"); $zip->close(); echo "File added with a different name in the ZIP file!";} else { echo "Failed to create the ZIP file.";}?>
In this example, the file file1.txt
will be added to the ZIP archive as newfile1.txt
.
3. Extracting Files from a ZIP Archive
You can extract all or specific files from a ZIP archive using the extractTo()
method.
Example:
<?php$zip = new ZipArchive();$filename = "example.zip";// Open the archive for readingif ($zip->open($filename) === TRUE) { // Extract all files to a specific directory $zip->extractTo("extracted_files/"); $zip->close(); echo "Files extracted successfully!";} else { echo "Failed to open the ZIP file.";}?>
extractTo($path)
: Extracts the contents of the ZIP archive to the specified directory.If you want to extract specific files, you can pass an array of filenames as a second argument to
extractTo()
.
4. Listing the Contents of a ZIP Archive
You can list the files inside a ZIP archive using the statIndex()
method.
Example:
<?php$zip = new ZipArchive();$filename = "example.zip";// Open the archive for readingif ($zip->open($filename) === TRUE) { // List files in the ZIP archive for ($i = 0; $i < $zip->numFiles; $i++) { echo "File: " . $zip->getNameIndex($i) . "<br>"; } $zip->close();} else { echo "Failed to open the ZIP file.";}?>
getNameIndex($index)
: Returns the name of the file at the specified index.numFiles
: Returns the total number of files in the archive.
5. Removing Files from a ZIP Archive
You can remove files from an existing ZIP archive using the deleteName()
method.
Example:
<?php$zip = new ZipArchive();$filename = "example.zip";// Open the archive for reading and modificationif ($zip->open($filename) === TRUE) { // Delete a file from the archive if ($zip->deleteName("file2.txt")) { echo "File deleted successfully!"; } else { echo "Failed to delete the file."; } $zip->close();} else { echo "Failed to open the ZIP file.";}?>
deleteName($filename)
: Deletes a file inside the ZIP archive.
6. Checking for Existing Files in the Archive
You can check if a specific file exists in the archive using the locateName()
method.
Example:
<?php$zip = new ZipArchive();$filename = "example.zip";// Open the archive for readingif ($zip->open($filename) === TRUE) { if ($zip->locateName("file1.txt") !== false) { echo "file1.txt exists in the archive!"; } else { echo "file1.txt does not exist in the archive."; } $zip->close();} else { echo "Failed to open the ZIP file.";}?>
locateName($filename)
: Locates a file in the ZIP archive. Returns the index if the file exists orfalse
if it doesn't.
7. Password Protecting a ZIP Archive
PHP's ZipArchive
does not directly support setting a password for a ZIP file, but you can still create password-protected ZIP files using third-party libraries or external utilities like 7zip or WinRAR through system commands.
Example using a system command (using 7zip or WinRAR):
<?php$filename = 'example.zip';$command = '7z a -pYourPassword ' . $filename . ' file1.txt file2.txt';exec($command);?>
This example uses 7zip to create a password-protected ZIP file.
8. Summary of Common Methods in ZipArchive
Method | Description |
---|---|
open($filename) | Opens a ZIP file for reading or creating. |
addFile($filename) | Adds a file to the ZIP archive. |
addFromString($name, $data) | Adds a string as a file in the archive. |
extractTo($path) | Extracts the ZIP archive contents to the specified directory. |
getNameIndex($index) | Gets the name of the file at the specified index. |
deleteName($filename) | Deletes a file from the ZIP archive. |
locateName($filename) | Locates a file inside the archive. |
close() | Closes the ZIP archive. |
Conclusion
The ZipArchive class in PHP provides a flexible and powerful way to work with ZIP archives. You can create, read, modify, and extract files from ZIP archives, making it a handy tool for tasks such as file compression, backups, and handling multiple files.
If you have any questions or need specific examples, feel free to ask!