PHP zip ZipArchive 扩展

[在 PHP 中使用 ZipArchive() 类创建 Zip 文件]

ZipArchive() 是用于在 PHP 中执行 ZIP 操作的类。使用 ZipArchive() 类,创建一个 Zip 文件。

<?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();
?>

上面的代码创建了两个包含一些内容的文本文件,并将它们添加到一个 zip 文件中。

输出:

在 PHP 中创建 Zip 文件

在 PHP 中使用 ZipArchive() 类创建 Zip 文件

让我们使用 PHP 的 ZipArchive() 类提取在第一个代码中创建的 zip 文件。

<?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!';
}
?>

上面的代码将提取在第一个示例中创建的 New.zip 文件。

输出:

在 PHP 中提取 Zip 文件

如何使用 PHP Zip 扩展来获取 ZIP 的所有成员文件的信息

PHP ZIP 扩展可以获取 ZIP 内所有文件的信息。

<?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);
}
?>

上面的代码使用内置的 ZIP 函数来获取 ZIP 内的文件信息。

输出:

扫描二维码关注公众号,回复: 17006498 查看本文章
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.

[使用 PHP 创建一个 Zip 文件]

下面的示例代码将创建一个 zip 文件 tutorial.zip,并添加文件。

在我们的 htdocs 目录中,我们有 Files to zip 文件夹,其中包含两个文件。我们将压缩目录 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);
}
}
}
?>

此代码在我们的根目录中创建一个新的 zip,Files to zip,如下所示。

压缩图像的文件

我们使用目录 Files to zip 创建 zip 文件夹 Files to zip。我们的目录有两个文件,insert.phpLoader.php

我们应该在我们的 zip 文件夹中找到上述文件。让我们来看看。

zip 文件里面

[使用 PHP 解压缩 Zip 文件]

让我们看看如何使用 PHP 解压缩 zip 文件。

例子:

<?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();
?>

输出:

解压压缩文件

我们使用代码将 Files to zip.zip 解压缩到 Files to zip 文件夹。

猜你喜欢

转载自blog.csdn.net/weixin_50251467/article/details/131777416
今日推荐