PHP解压缩zip文件代码实现
在具体网站编程项目中,有时候要使用PHP脚本代码的方式来控制压缩和解压zip文件。下面是一个简单的php的zip解压缩实现代码,需要的朋友可以参考下仔细的研究了一下。
PHP安装后自带 zip 扩展 ,首先我们需要开启它,把 php.ini 中的 extension=php_zip.dll 前面的分号去掉就,接着重启web服务器即可。
php的zip解压缩实现代码如下:
<?php</p><p>//需开启配置 php_zip.dll</p>//phpinfo();<br /><p>header("Content-type:text/html;charset=utf-8");</p><p>function get_zip_originalsize($filename, $path) {</p> //先判断待解压的文件是否存在<br /> if(!file_exists($filename)){<br /> die("文件 $filename 不存在!");<br /> } <br /> $starttime = explode(' ',microtime()); //解压开始的时间<br /><br /> //将文件名和路径转成windows系统默认的gb2312编码,否则将会读取不到<br /> $filename = iconv("utf-8","gb2312",$filename);<br /> $path = iconv("utf-8","gb2312",$path);<br /> //打开压缩包<br /> $resource = zip_open($filename);<br /> $i = 1;<br /> //遍历读取压缩包里面的一个个文件<br /> while ($dir_resource = zip_read($resource)) {<br /> //如果能打开则继续<br /> if (zip_entry_open($resource,$dir_resource)) {<br /> //获取当前项目的名称,即压缩包里面当前对应的文件名<br /> $file_name = $path.zip_entry_name($dir_resource);<br /> //以最后一个“/”分割,再用字符串截取出路径部分<br /> $file_path = substr($file_name,0,strrpos($file_name, "/"));<br /> //如果路径不存在,则创建一个目录,true表示可以创建多级目录<br /> if(!is_dir($file_path)){<br /> mkdir($file_path,0777,true);<br /> }<br /> //如果不是目录,则写入文件<br /> if(!is_dir($file_name)){<br /> //读取这个文件<br /> $file_size = zip_entry_filesize($dir_resource);<br /> //最大读取6M,如果文件过大,跳过解压,继续下一个<br /> if($file_size<(1024*1024*6)){<br /> $file_content = zip_entry_read($dir_resource,$file_size);<br /> file_put_contents($file_name,$file_content);<br /> }else{<br /> echo "<p> ".$i++." 此文件已被跳过,原因:文件过大, -> ".iconv("gb2312","utf-8",$file_name)." </p>";<br /> }<br /> }<br /> //关闭当前<br /> zip_entry_close($dir_resource);<br /> }<br /> }<br /> //关闭压缩包<br /> zip_close($resource); <br /> $endtime = explode(' ',microtime()); //解压结束的时间<br /> $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]);<br /> $thistime = round($thistime,3); //保留3为小数<br /> echo "<p>解压完毕!,本次解压花费:$thistime 秒。</p>";<br /><p>}</p><p>$size = get_zip_originalsize('test.zip','./');</p><p>?></p>
测试解压了一个300多KB的小文件,花了0.115秒,测试解压了一个30多MB的(网页文件,小文件比较多),花了20多秒。

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
