PHP zip ZipArchive extension

ZipArchive()[ Create Zip file using class in PHP ]

ZipArchive()Is a class used to perform ZIP operations in PHP. Using ZipArchive()the class, create a Zipfile.

<?php
$create_zip = new ZipArchive();
$file_name = "./New.zip";

if ($create_zip->open($file_name, ZipArchive::CREATE)!==TRUE) {
    
    
    exit("cannot open the zip file <$file_name>\n");
}
$current_dir=getcwd();
//Create files to add to the zip
$create_zip->addFromString("file1 ". time().".txt" , "#1 This is This is the test file number one.\n"); 
$create_zip->addFromString("file2 ". time().".txt", "#2 This is This is the test file number one.\n");
//add files to the zip
$create_zip->addFile($current_dir . "/too.php","/testfromfile.php");
echo "Number of files added: " . $create_zip->numFiles;
echo "<br>";
echo "Failed to add:" . $create_zip->status ;
$create_zip->close();
?>

The above code creates two text files with some content and adds them to a zip file.

Output:

Create Zip file in PHP

ZipArchive()Create Zipfiles using classes in PHP

Let's use PHP's ZipArchive()class to extract the zip file created in the first code.

<?php
$extract_zip = new ZipArchive;
$open_zip = $extract_zip->open('New.zip');
if ($open_zip === TRUE) {
    
    
	$extract_to = getcwd();
    $extract_zip->extractTo($extract_to); //extract to the current working directory.
    echo "Number of Files to be Extracted:" . $extract_zip->numFiles . "<br>";
	$extract_zip->close();
    echo 'Files Successfully Extracted!';
} 
else {
    
    
    echo 'Cannot Extract!';
}
?>

The code above will extract the file created in the first example New.zip.

Output:

Extract Zip files in PHP

How to use PHP Zipextension to get ZIPinformation about all member files of

The PHP ZIPextension can obtain ZIPinformation about all files within.

<?php
$zip_file = zip_open("New.zip");
if ($zip_file) {
    
    
    while ($zip_members = zip_read($zip_file)) {
    
    
        echo "Name of the file:               " . zip_entry_name($zip_members) . "<br>";
        echo "Original Size of the File:    " . zip_entry_filesize($zip_members) . "<br>";
        echo "Compressed Size of the File:    " . zip_entry_compressedsize($zip_members) . "<br>";
        echo "Method of Compression: " . zip_entry_compressionmethod($zip_members) . "<br>";

        if (zip_entry_open($zip_file, $zip_members, "r")) {
    
    
            echo "Content of the file:<br>";
            $buf = zip_entry_read($zip_members, zip_entry_filesize($zip_members));
            echo "$buf <br>";

            zip_entry_close($zip_members);
        }
        echo "<br>";
    }
    zip_close($zip_file);
}
?>

The above code uses the built-in ZIPfunction to obtain ZIPthe file information within.

Output:

Name of the file: file1 1644842283.txt
Original Size of the File: 45
Compressed Size of the File: 39
Method of Compression: deflated
Content of the file:
#1 This is test file number one.

Name of the file: file2 1644842283.txt
Original Size of the File: 45
Compressed Size of the File: 39
Method of Compression: deflated
Content of the file:
#2 This is test file number two.

[Create a Zip file using PHP]

The following example code will create a zip file tutorial.zipand add the files.

Within our htdocsdirectory we have Files to zipfolders containing two files. We will zip the directory Files to zip.

<?php
// Enter the name of directory
$pathdir = "Files to zip/";

//Create a name for the zip folder
$zipcreated = "Files to zip.zip";

// Create new zip class
$zip = new ZipArchive;

if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) {
    
    

    // Store the files in our `Files to zip` zip file.
    $dir = opendir($pathdir);

    while($file = readdir($dir)) {
    
    
        if(is_file($pathdir.$file)) {
    
    
            $zip -> addFile($pathdir.$file, $file);
}
}
}
?>

This code creates a new zip in our root directory, Files to zipas shown below.

Compressed image file

We use the directory Files to zipto create the zip folder Files to zip. Our directory has two files, insert.phpand Loader.php.

We should find the above files in our zip folder. let's see.

inside zip file

[Unzip Zip files using PHP]

Let's see how to unzip a zip file using PHP.

example:

<?php
// Create new zip class
$zip = new ZipArchive;

// Add zip filename which needs to unzip

$zip->open('Files to zip.zip');

// Extracts to current directory
$zip->extractTo('./Files to zip');

$zip->close();
?>

Output:

Unzip compressed files

We use code to Files to zip.zipunzip to Files to zipa folder.

Guess you like

Origin blog.csdn.net/weixin_50251467/article/details/131777416