


Use ZipArchive to compress files. This is an extension class of php. This extension has been supported since php5.2. If you get an error when using it, check the extension=php_zip.dll in php.ini. Have you removed the semicolon before restarting Apache so that you can use this class library?
Example 1. Generate zip file
/ * Generate zip file*/
function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($ file)) {
$valid_files[] = $file; ) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) ! == true) {
return false; $zip->addFile($file,$file_info_arr['basename']);//Remove the hierarchical directory
}
//debug
//echo 'The zip archive contains ',$zip- >numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
define('ROOTPATH',dirname ( __FILE__ )); //Website path
$files_to_zip = array(
ROOTPATH.DIRECTORY_SEPARATOR.'PHP+jQuery+Cookbook.pdf',
ROOTPATH.DIRECTORY_SEPARATOR.'TurboListerZeroTemplate.csv'
);
//if true, good; if false, zip creation failed
$filename='my-archive.zip';
$result = create_zip($files_to_zip,$filename);
Example 2, compress all files under the folder
Copy code
The code is as follows :
/*
php zip压缩文件夹下面的所有文件
*/
class HZip
{
/**
* Add files and subdirectories to the zip file
* @param string $folder
* @param ZipArchive $zipFile
* @param int $exclusiveLength Number of text to be exclusived from the file path .
*/
private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir($folder);
while (false !== $f = readdir($handle)) {
if ($f != '.' && $f != '..') {
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $exclusiveLength);
if (is_file($filePath)) {
$zipFile->addFile($filePath, $localPath);
} elseif (is_dir($filePath)) {
// 添加子文件夹
$zipFile->addEmptyDir($localPath);
self::folderToZip($filePath, $zipFile, $exclusiveLength);
}
}
}
closedir($handle);
}
/**
* Zip a folder (include itself).
* Usage:
* HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
*
* @param string $sourcePath Path of directory to be zip.
* @param string $outZipPath Path of output zip file.
*/
public static function zipDir($sourcePath, $outZipPath)
{
$pathInfo = pathInfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$sourcePath=$parentPath.'/'.$dirName;//防止传递'folder' 文件夹产生bug
$z = new ZipArchive();
$z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件
$z->addEmptyDir($dirName);//建立文件夹
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
$z->close();
}
}
//使用方法
HZip::zipDir('yourlife', 'yourlife.zip');
?>
1.ZipArchive::addEmptyDir
添加一个新的文件目录
2.ZipArchive::addFile
将文件添加到指定zip压缩包中。
3.ZipArchive::addFromString
添加的文件同时将内容添加进去
4.ZipArchive::close
关闭ziparchive
5.ZipArchive::extractTo
将压缩包解压
6.ZipArchive::open
打开一个zip压缩包
7.ZipArchive::getStatusString
返回压缩时的状态内容,包括错误信息,压缩信息等等
8.ZipArchive::deleteIndex
删除压缩包中的某一个文件,如:deleteIndex(0)删除第一个文件
9.ZipArchive::deleteName
删除压缩包中的某一个文件名称,同时也将文件删除。

压缩HTML文件成ZIP可提高页面加载速度。方法包括:使用在线工具(如FileOptimizer、TinyPNG)使用命令行工具(如gzip、7-zip)使用Node.js脚本(使用zlib模块)

zip命令是Linux系统中一个非常有用的压缩工具。通过使用zip命令,您可以轻松地将文件和目录压缩成一个zip文件,并节省存储空间和方便传输。zip命令的基本语法为“zip [选项] [压缩文件名] [要压缩的文件或目录]”。

1.压缩文件夹为zip文件[root@cgls]#zip-rmydata.zipmydata2.把mydata.zip解压到mydatabak目录里面[root@cgls]#unzipmydata.zip-dmydatabak3.mydata01文件夹和mydata02.txt压缩成为mydata.zip[root@cgls]#zipmydata.zipmydata01mydata02.txt4.直接解压mydata.zip文件[root@cgls]#unzipmydata.zip5.查看myd

7z和zip都是无损压缩。7z是一种主流高效的压缩格式,它拥有极高的压缩比;ZIP文件格式是一种数据压缩和文档储存的文件格式。7z压缩比率大些,zip次之;zip格式比较常见支技泛围广,windows操作系统默认支持zip格式。

压缩文件是一种常见的操作,能够节省磁盘的空间以及网络传输的时间,而Java中提供了Zip函数用于实现文件的压缩。本文将通过详细的介绍和实例演示来展示如何使用Java中的Zip函数进行文件压缩。一、Zip函数介绍Zip函数是Java中提供的压缩和打包工具类库,使用该函数可以将文件或文件夹压缩成一个Zip格式的文件。Zip函数中主要使用了ZipOutputStr

HTML文件ZIP压缩可以通过Python的zipfile模块实现:创建ZIP文件对象。向ZIP文件添加HTML文件。关闭ZIP文件对象。

功能描述页面上传一个源码压缩包,后端将压缩包解压,并获取每个文件中的内容。相关源码(1)首先定义一个与解压文件对应的实体类。packagecom.sonar.data.vo;importlombok.Data;/***文件解析对象**@authorYuanqiang.Zhang*@since2022/7/12*/@DatapublicclassUnzipFileVo{/***类型:0-文件夹;1-文件*/privateIntegertype;/***文件路径(如:src/main/java/co

zip_close()函数用于关闭zip文件存档。zip由zip_open()函数打开。语法zip_close(zip_file)参数zip_file-这里提到用zip_open()打开的zip文件。这是我们要关闭的文件。返回zip_close()函数不返回任何内容。示例<?php $zip_file=zip_open("new.zip"); zip_read($zip_file);&n


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor
