欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP自帶ZIP壓縮、解壓縮類ZipArchiv使用指南

 更新時(shí)間:2015年03月03日 15:26:05   投稿:hebedich  
這篇文章主要介紹了PHP自帶ZIP壓縮、解壓縮類ZipArchiv使用指南,十分詳細(xì),需要的朋友可以參考下

要使用該P(yáng)HP擴(kuò)展類,需要(PHP 5 >= 5.2.0, PECL zip >= 1.1.0),部分方法需要 PHP 5.2.+,且php.ini配置支持zip
對于win系統(tǒng),直接去掉php_zip.dll 擴(kuò)展的注釋,然后重啟http服務(wù)(IIS或Apache)即可
Linux還沒有試驗(yàn),理論上差別不會很大

 功能:
1、解壓縮zip文件
2、將文件壓縮成zip文件
3、追加文件到zip文件
4、將文件夾打包成zip文件(需要循環(huán)添加文件與創(chuàng)建空文件夾)
5、刪除壓縮文件中的條目

--------------------- ZipArchive對象常用方法介紹 ---------------------

測試約定:
 測試文件為text.zip,該壓縮文件包含了三個(gè)被壓縮的文件(hello.txt、word.txt、ooxx.jpg),如下所示

復(fù)制代碼 代碼如下:

text.zip
     hello.txt
     word.txt
     ooxx.jpg

打開zip文件,以便進(jìn)一步操作
ZipArchive::open
 (PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
 mixed ZipArchive::open ( string $filename [, int $flags ] )

第2個(gè)參數(shù)講解

ZIPARCHIVE::OVERWRITE    總是創(chuàng)建一個(gè)新的文件,如果指定的zip文件存在,則會覆蓋掉
ZIPARCHIVE::CREATE        如果指定的zip文件不存在,則新建一個(gè)
ZIPARCHIVE::EXCL        如果指定的zip文件存在,則會報(bào)錯(cuò)   
ZIPARCHIVE::CHECKCONS

返回值: 

 如果返回值等于下面的屬性,表示對應(yīng)的錯(cuò)誤 或者 返回TRUE
 $res == ZipArchive::ER_EXISTS    File already exists.(文件已經(jīng)存在)
$res == ZipArchive::ER_INCONS    Zip archive inconsistent.(壓縮文件不一致)
$res == ZipArchive::ER_INVAL    Invalid argument.(無效的參數(shù))
$res == ZipArchive::ER_MEMORY    Malloc failure.(內(nèi)存錯(cuò)誤?這個(gè)不確定)
$res == ZipArchive::ER_NOENT    No such file.(沒有這樣的文件)
$res == ZipArchive::ER_NOZIP    Not a zip archive.(沒有一個(gè)壓縮文件)
$res == ZipArchive::ER_OPEN        Can't open file.(不能打開文件)
$res == ZipArchive::ER_READ        Read error.(讀取錯(cuò)誤)
$res == ZipArchive::ER_SEEK        Seek error.(查找錯(cuò)誤)

復(fù)制代碼 代碼如下:

<?php
 $zip = new ZipArchive;
 $res = $zip->open('test.zip');
 if ($res === TRUE) {
     echo 'ok';
     //解壓縮到test文件夾
    $zip->extractTo('test');
     $zip->close();
 } else {
     echo 'failed, code:' . $res;
 }
 ?>

根據(jù)壓縮文件內(nèi)的列表索引,返回被壓縮文件的名稱

ZipArchive::getNameIndex
 string ZipArchive::getNameIndex ( int $index [, int $flags ] )

復(fù)制代碼 代碼如下:

 <?php
 $zip = new ZipArchive();
 $res = $zip->open('test.zip');
 if ($res === TRUE) {
     var_dump($zip->getNameIndex(0)); // hello.txt
     var_dump($zip->getNameIndex(1)); // word.txt
     var_dump($zip->getNameIndex(2)); // ooxx.jpg
 } else {
     echo 'failed, code:' . $res;
 }
 $zip->close();
 ?>

根據(jù)壓縮內(nèi)的文件名稱,獲取該文件的文本流

ZipArchive::getStream
 resource ZipArchive::getStream ( string $name )

復(fù)制代碼 代碼如下:

 <?php
 $zip = new ZipArchive();
 $res = $zip->open('test.zip');
 if ($res === TRUE) {
     $stream = $zip->getStream('hello.txt');
 } else {
     echo 'failed, code:' . $res;
 }
 $zip->close();
 $str = stream_get_contents($stream); //這里注意獲取到的文本編碼
var_dump($str);
 ?>

根據(jù)壓縮文件內(nèi)的索引(從0開始)修改壓縮文件內(nèi)的文件名

ZipArchive::renameIndex
 bool ZipArchive::renameIndex ( int $index , string $newname )
 (PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

成功時(shí)返回 TRUE, 或者在失敗時(shí)返回 FALSE。

復(fù)制代碼 代碼如下:

<?php
 $zip = new ZipArchive;
 $res = $zip->open('test.zip');
 if ($res === TRUE) {
     //把壓縮文件內(nèi)第一個(gè)文件修改成newname.txt
     $zip->renameIndex(0,'newname.txt');
     $zip->close();
 } else {
     echo 'failed, code:' . $res;
 }
 ?>

根據(jù)壓縮文件內(nèi)的文件名,修改壓縮文件內(nèi)的文件名

ZipArchive::renameName
 (PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

復(fù)制代碼 代碼如下:

 <?php
 $zip = new ZipArchive;
 $res = $zip->open('test.zip');
 if ($res === TRUE) {
     //把壓縮文件內(nèi)的word.txt修改成newword.txt
     $zip->renameName('word.txt','newword.txt');
     $zip->close();
 } else {
     echo 'failed, code:' . $res;
 }
 ?>

獲取壓縮文件的注釋(zip的文件注釋)

ZipArchive::getArchiveComment
 (PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
 string ZipArchive::getArchiveComment ([ int $flags ] )
參數(shù):ZipArchive::FL_UNCHANGED
如果參數(shù)設(shè)置為 ZipArchive::FL_UNCHANGED, 返回原始的還沒有改變的注釋
 例如,在處理該壓縮文件時(shí),使用setArchiveComment()方法改變或設(shè)置注釋時(shí)
 如果加上ZipArchive::FL_UNCHANGED這個(gè)參數(shù),則表示獲取改變之前的注釋內(nèi)容,否則獲取已經(jīng)改變的注釋內(nèi)容
 類似的還有:
ZipArchive::getCommentIndex 根據(jù)壓縮文件內(nèi)的文件索引獲取【文件注釋】
ZipArchive::getCommentName    根據(jù)壓縮文件內(nèi)的文件名稱獲取【文件注釋】
 注意:這里的是文件注釋,不是壓縮文件(zip)的注釋

 設(shè)置或修改壓縮文件的注釋(zip的文件注釋)
ZipArchive::setArchiveComment
 (PHP 5 >= 5.2.0, PECL zip >= 1.4.0)
 bool ZipArchive::setArchiveComment ( string $comment )

復(fù)制代碼 代碼如下:

 <?php
 $zip = new ZipArchive;
 $res = $zip->open('test.zip', ZipArchive::CREATE);
 if ($res === TRUE) {
     //$zip->addFromString('test.txt', 'file content goes here');
     $zip->setArchiveComment('new archive comment');
     $zip->close();
     echo 'ok';
 } else {
     echo 'failed';
 }
 ?>

根據(jù)壓縮文件內(nèi)的索引刪除壓縮文件內(nèi)的文件(也就是刪除檔案內(nèi)的條目)

ZipArchive::deleteIndex
 (PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

一、如何解壓縮一個(gè)zip文件 extractTo()

復(fù)制代碼 代碼如下:

 $zip = new ZipArchive();

一、如何創(chuàng)建壓縮文件? addFromString() addFile()

即是是把一個(gè)或多個(gè)文件打包成一個(gè)zip文件

1、只需要new一個(gè)ZipArchive對象
2、然后使用該對象的open方法創(chuàng)建一個(gè)zip文件
3、接著使用addFile方法,將要打包的文件寫入剛剛創(chuàng)建的zip文件中
4、最后記得關(guān)閉該對象

復(fù)制代碼 代碼如下:

<?php
 //建立一個(gè)新的ZipArchive的對象
$zip = new ZipArchive;
 $res = $zip->open('test.zip');
 //如果打開成功
if ($res === TRUE) {
 //如果打開失敗
} else {
     //輸出出錯(cuò)的代碼
    echo 'failed, code:' . $res;
 }
 $zip->close();

以上所述就是本文的全部內(nèi)容了,希望能對大家有所幫助。

相關(guān)文章

最新評論