国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
php zip文件內(nèi)容比較類,
Home php教程 php手冊(cè) php zip文件內(nèi)容比較類,

php zip文件內(nèi)容比較類,

Jun 13, 2016 am 09:24 AM
php zip content accomplish document Compare usage of kind

php zip文件內(nèi)容比較類,

一個(gè)php實(shí)現(xiàn)的zip文件內(nèi)容比較類及其用法,可實(shí)現(xiàn)比較兩個(gè)zip文件的內(nèi)容,返回新增、刪除、及相同的文件列表。

php實(shí)現(xiàn)的zip文件內(nèi)容比較類。是一個(gè)非常實(shí)用的PHP類文件。
該php zip文件比較類主要實(shí)現(xiàn)比較兩個(gè)zip文件的內(nèi)容,返回新增,刪除,及相同的文件列表。暫時(shí)只支持單層。
需求:上傳一個(gè)zip文件,zip內(nèi)有很多圖片文件。需要對(duì)圖片文件進(jìn)行一系列很耗時(shí)的處理。當(dāng)用戶再更新zip文件時(shí)。判斷zip內(nèi)文件是否一致,只處理不同的文件。這樣可以節(jié)省資源與時(shí)間,因此需要編寫一個(gè)能夠比較zip內(nèi)文件的類。
ZipCompare.class.php類文件如下:

<?<span>php 
</span><span>/*</span><span>* Zip Compare class 比較兩個(gè)zip文件的內(nèi)容,返回新增,刪除,及相同的文件列表,暫時(shí)只支持單層 
* Date: 2014-05-18 
* Author: fdipzone 
* Ver: 1.0 
* www.jbxue.com
* Func: 
* public compare 比較zip文件內(nèi)容 
* private getInfo 獲取zip內(nèi)文件列表 
* private parse 分析兩個(gè)zip的文件內(nèi)容 
* private check 檢查zip文件是否正確 
* private check_handler 檢查服務(wù)器是否有安裝unzip 
</span><span>*/</span> 

<span>class</span> ZipCompare{ <span>//</span><span> class start </span>

<span>/*</span><span>* 比較zip文件內(nèi)容,列出不相同的部分 
* @param String $zipfile1 zip文件1 
* @param String $zipfile2 zip文件2 
* @return Array 
</span><span>*/</span> 
<span>public</span> <span>function</span> compare(<span>$zipfile1</span>, <span>$zipfile2</span><span>){ 

</span><span>//</span><span> 檢查是否有安裝unzip </span>
<span>if</span>(!<span>$this</span>-><span>check_handler()){ 
</span><span>throw</span> <span>new</span> <span>Exception</span>('unzip not install'<span>); 
} 

</span><span>//</span><span> 檢查zip文件 </span>
<span>if</span>(!<span>$this</span>->check(<span>$zipfile1</span>) || !<span>$this</span>->check(<span>$zipfile2</span><span>)){ 
</span><span>throw</span> <span>new</span> <span>Exception</span>('zipfile not exists or error'<span>); 
} 

</span><span>//</span><span> 獲取zip內(nèi)文件列表 </span>
<span>$zipinfo1</span> = <span>$this</span>->getInfo(<span>$zipfile1</span><span>); 
</span><span>$zipinfo2</span> = <span>$this</span>->getInfo(<span>$zipfile2</span><span>); 

</span><span>//</span><span> 分析兩個(gè)zip的文件內(nèi)容,返回相同及不同的文件列表 </span>
<span>return</span> <span>$this</span>->parse(<span>$zipinfo1</span>, <span>$zipinfo2</span><span>); 
} 

</span><span>/*</span><span>* 獲取zip內(nèi)文件列表 
* @param String $zipfile zip文件 
* @return Array zip內(nèi)文件列表 
</span><span>*/</span> 
<span>private</span> <span>function</span> getInfo(<span>$zipfile</span><span>){ 

</span><span>//</span><span> unzip -v fields </span>
<span>$fields</span> = <span>array</span>('Length','Method','Size','Cmpr','Date','Time','CRC-32','Name'<span>); 

</span><span>//</span><span> zip verbose </span>
<span>$verbose</span> = <span>shell_exec</span>(<span>sprintf</span>("unzip -v %s | sed '\$d' | sed '\$d' | sed -n '4,\$p'", <span>$zipfile</span><span>)); 

</span><span>//</span><span> zip info </span>
<span>$zipinfo</span> = <span>array</span><span>(); 

</span><span>$filelist</span> = <span>explode</span>("\n", <span>$verbose</span><span>); 

</span><span>if</span>(<span>$filelist</span><span>){ 
</span><span>foreach</span>(<span>$filelist</span> <span>as</span> <span>$rowdata</span><span>){ 
</span><span>if</span>(<span>$rowdata</span>==''<span>){ 
</span><span>continue</span><span>; 
} 
</span><span>$rowdata</span> = <span>preg_replace</span>('/[ ]{2,}/', ' ', <span>$rowdata</span>); <span>//</span><span> 將兩個(gè)或以上空格替換為一個(gè) </span>
<span>$tmp</span> = <span>array_slice</span>(<span>explode</span>(' ', <span>$rowdata</span>), 1); <span>//</span><span> 去掉第一個(gè)空格 </span>

<span>$file</span> = <span>array_combine</span>(<span>$fields</span>, <span>$tmp</span><span>); 

</span><span>$zipinfo</span>[<span>$file</span>['Name']] = <span>$file</span>['Length'].'_'.<span>$file</span>['CRC-32']; <span>//</span><span> 文件名,長(zhǎng)度,CRC32,用于校驗(yàn) </span>
<span>} 
} 
</span><span>return</span> <span>$zipinfo</span><span>; 
} 

</span><span>/*</span><span>* 分析兩個(gè)zip文件內(nèi)容 
* @param String $zipinfo1 
* @param String $zipinfo2 
* @return Array 
</span><span>*/</span> 
<span>private</span> <span>function</span> parse(<span>$zipinfo1</span>, <span>$zipinfo2</span><span>){ 

</span><span>$result</span> = <span>array</span><span>( 
</span>'add' => <span>array</span>(), <span>//</span><span> 新增 </span>
'del' => <span>array</span>(), <span>//</span><span> 缺少 </span>
'match' => <span>array</span>() <span>//</span><span> 匹配 </span>
<span>); 

</span><span>if</span>(<span>$zipinfo1</span> && <span>$zipinfo2</span><span>){ 

</span><span>//</span><span> 在zip1但不在zip2的文件 </span>
<span>$result</span>['add'] = <span>array_values</span>(<span>array_diff</span>(<span>array_keys</span>(<span>$zipinfo1</span>), <span>array_keys</span>(<span>$zipinfo2</span><span>))); 

</span><span>//</span><span> 在zip2但不在zip1的文件 </span>
<span>$result</span>['del'] = <span>array_values</span>(<span>array_diff</span>(<span>array_keys</span>(<span>$zipinfo2</span>), <span>array_keys</span>(<span>$zipinfo1</span><span>))); 

</span><span>//</span><span> 同時(shí)在zip1與zip2的文件 </span>
<span>$match_file</span> = <span>array_values</span>(<span>array_diff</span>(<span>array_keys</span>(<span>$zipinfo1</span>), <span>$result</span>['add'<span>])); 

</span><span>//</span><span> 檢查相同文件名的文件內(nèi)容是否匹配 </span>
<span>for</span>(<span>$i</span>=0,<span>$len</span>=<span>count</span>(<span>$match_file</span>); <span>$i</span><<span>$len</span>; <span>$i</span>++<span>){ 

</span><span>if</span>(<span>$zipinfo1</span>[<span>$match_file</span>[<span>$i</span>]]==<span>$zipinfo2</span>[<span>$match_file</span>[<span>$i</span>]]){ <span>//</span><span> match </span>
<span>array_push</span>(<span>$result</span>['match'], <span>$match_file</span>[<span>$i</span><span>]); 
}</span><span>else</span>{ <span>//</span><span> not match, change to add </span>
<span>array_push</span>(<span>$result</span>['add'], <span>$match_file</span>[<span>$i</span><span>]); 
} 
} 
} 
</span><span>return</span> <span>$result</span><span>; 
} 

</span><span>/*</span><span>* 檢查zip文件是否正確 
* @param String $zipfile zip文件 
* @return boolean 
</span><span>*/</span> 
<span>private</span> <span>function</span> check(<span>$zipfile</span><span>){ 
</span><span>//</span><span> 文件存在且能解壓 </span>
<span>return</span> <span>file_exists</span>(<span>$zipfile</span>) && <span>shell_exec</span>(<span>sprintf</span>('unzip -v %s | wc -l', <span>$zipfile</span>))>1<span>; 
} 

</span><span>/*</span><span>* 檢查服務(wù)器是否有安裝unzip 
* @return boolean 
</span><span>*/</span> 
<span>private</span> <span>function</span><span> check_handler(){ 
</span><span>return</span> <span>strstr</span>(<span>shell_exec</span>('unzip -v'), 'version')!=''<span>; 
} 
} </span><span>//</span><span> class end </span>
?>

demo示例程序如下:

<?<span>php 
</span><span>require</span> "ZipCompare.class.php"<span>;

</span><span>$obj</span> = <span>new</span><span> ZipCompare(); 
</span><span>$result</span> = <span>$obj</span>->compare('test1.zip','test2.zip'<span>);

</span><span>print_r</span>(<span>$result</span><span>);

</span>?>

執(zhí)行后輸出:

Array
(
[add] => Array
(
[0] => 9.jpg
)

[del] => Array
(
[0] => 5.jpg
[1] => 6.jpg
[2] => 7.jpg
[3] => 8.jpg
)

[match] => Array
(
[0] => 1.jpg
[1] => 10.jpg
[2] => 11.jpg
[3] => 12.jpg
[4] => 13.jpg
[5] => 14.jpg
[6] => 15.jpg
[7] => 16.jpg
[8] => 17.jpg
[9] => 18.jpg
[10] => 2.jpg
[11] => 3.jpg
[12] => 4.jpg
)
)

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to access a character in a string by index in PHP How to access a character in a string by index in PHP Jul 12, 2025 am 03:15 AM

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

How Do Generators Work in PHP? How Do Generators Work in PHP? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

How to prevent session hijacking in PHP? How to prevent session hijacking in PHP? Jul 11, 2025 am 03:15 AM

To prevent session hijacking in PHP, the following measures need to be taken: 1. Use HTTPS to encrypt the transmission and set session.cookie_secure=1 in php.ini; 2. Set the security cookie attributes, including httponly, secure and samesite; 3. Call session_regenerate_id(true) when the user logs in or permissions change to change to change the SessionID; 4. Limit the Session life cycle, reasonably configure gc_maxlifetime and record the user's activity time; 5. Prohibit exposing the SessionID to the URL, and set session.use_only

PHP get the first N characters of a string PHP get the first N characters of a string Jul 11, 2025 am 03:17 AM

You can use substr() or mb_substr() to get the first N characters in PHP. The specific steps are as follows: 1. Use substr($string,0,N) to intercept the first N characters, which is suitable for ASCII characters and is simple and efficient; 2. When processing multi-byte characters (such as Chinese), mb_substr($string,0,N,'UTF-8'), and ensure that mbstring extension is enabled; 3. If the string contains HTML or whitespace characters, you should first use strip_tags() to remove the tags and trim() to clean the spaces, and then intercept them to ensure the results are clean.

PHP get the last N characters of a string PHP get the last N characters of a string Jul 11, 2025 am 03:17 AM

There are two main ways to get the last N characters of a string in PHP: 1. Use the substr() function to intercept through the negative starting position, which is suitable for single-byte characters; 2. Use the mb_substr() function to support multilingual and UTF-8 encoding to avoid truncating non-English characters; 3. Optionally determine whether the string length is sufficient to handle boundary situations; 4. It is not recommended to use strrev() substr() combination method because it is not safe and inefficient for multi-byte characters.

How to URL encode a string in PHP with urlencode How to URL encode a string in PHP with urlencode Jul 11, 2025 am 03:22 AM

The urlencode() function is used to encode strings into URL-safe formats, where non-alphanumeric characters (except -, _, and .) are replaced with a percent sign followed by a two-digit hexadecimal number. For example, spaces are converted to signs, exclamation marks are converted to!, and Chinese characters are converted to their UTF-8 encoding form. When using, only the parameter values ??should be encoded, not the entire URL, to avoid damaging the URL structure. For other parts of the URL, such as path segments, the rawurlencode() function should be used, which converts the space to . When processing array parameters, you can use http_build_query() to automatically encode, or manually call urlencode() on each value to ensure safe transfer of data. just

How to set and get session variables in PHP? How to set and get session variables in PHP? Jul 12, 2025 am 03:10 AM

To set and get session variables in PHP, you must first always call session_start() at the top of the script to start the session. 1. When setting session variables, use $_SESSION hyperglobal array to assign values ??to specific keys, such as $_SESSION['username']='john_doe'; it can store strings, numbers, arrays and even objects, but avoid storing too much data to avoid affecting performance. 2. When obtaining session variables, you need to call session_start() first, and then access the $_SESSION array through the key, such as echo$_SESSION['username']; it is recommended to use isset() to check whether the variable exists to avoid errors

How to prevent SQL injection in PHP How to prevent SQL injection in PHP Jul 12, 2025 am 03:02 AM

Key methods to prevent SQL injection in PHP include: 1. Use preprocessing statements (such as PDO or MySQLi) to separate SQL code and data; 2. Turn off simulated preprocessing mode to ensure true preprocessing; 3. Filter and verify user input, such as using is_numeric() and filter_var(); 4. Avoid directly splicing SQL strings and use parameter binding instead; 5. Turn off error display in the production environment and record error logs. These measures comprehensively prevent the risk of SQL injection from mechanisms and details.

See all articles