【 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();
?>
上記のコードは、コンテンツを含む 2 つのテキスト ファイルを作成し、それらを 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
取得する方法ZIP
PHPZIP
拡張機能は、その中のすべてのファイルに関する情報を取得できます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
出力:
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
、ファイルを追加します。
ディレクトリ内には2 つのファイルを含むフォルダー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);
}
}
}
?>
Files to zip
このコードは、次に示すように、ルート ディレクトリに新しい zip を作成します。
このディレクトリを使用してFiles to zip
zip フォルダーを作成しますFiles to zip
。このディレクトリには 2 つのファイルinsert.php
とLoader.php
.
上記のファイルは 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