PHP內建ZIP壓縮、解壓縮類ZipArchiv使用指南_PHP教程

來源:互聯網
上載者:User

PHP內建ZIP壓縮、解壓縮類ZipArchiv使用指南


這篇文章主要介紹了PHP內建ZIP壓縮、解壓縮類ZipArchiv使用指南,十分詳細,需要的朋友可以參考下

要使用該PHP擴充類,需要(PHP 5 >= 5.2.0, PECL zip >= 1.1.0),部分方法需要 PHP 5.2.+,且php.ini配置支援zip
對於win系統,直接去掉php_zip.dll 擴充的注釋,然後重啟http服務(IIS或Apache)即可
Linux還沒有實驗,理論上差別不會很大

功能:
1、解壓縮zip檔案
2、將檔案壓縮成zip檔案
3、追加檔案到zip檔案
4、將檔案夾打包成zip檔案(需要迴圈添加檔案與建立空檔案夾)
5、刪除壓縮檔中的條目

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

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

代碼如下:


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

開啟zip檔案,以便進一步操作
ZipArchive::open
(PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
mixed ZipArchive::open ( string $filename [, int $flags ] )

第2個參數講解

ZIPARCHIVE::OVERWRITE 總是建立一個新的檔案,如果指定的zip檔案存在,則會覆蓋掉
ZIPARCHIVE::CREATE 如果指定的zip檔案不存在,則建立一個
ZIPARCHIVE::EXCL 如果指定的zip檔案存在,則會報錯
ZIPARCHIVE::CHECKCONS

傳回值:

如果傳回值等於下面的屬性,表示對應的錯誤 或者 返回TRUE
$res == ZipArchive::ER_EXISTS File already exists.(檔案已經存在)
$res == ZipArchive::ER_INCONS Zip archive inconsistent.(壓縮檔不一致)
$res == ZipArchive::ER_INVAL Invalid argument.(無效的參數)
$res == ZipArchive::ER_MEMORY Malloc failure.(記憶體錯誤?這個不確定)
$res == ZipArchive::ER_NOENT No such file.(沒有這樣的檔案)
$res == ZipArchive::ER_NOZIP Not a zip archive.(沒有一個壓縮檔)
$res == ZipArchive::ER_OPEN Can't open file.(不能開啟檔案)
$res == ZipArchive::ER_READ Read error.(讀取錯誤)
$res == ZipArchive::ER_SEEK Seek error.(尋找錯誤)

代碼如下:


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

根據壓縮檔內的清單索引,返回被壓縮檔的名稱

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

代碼如下:


$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();
?>

根據壓縮內的檔案名稱,擷取該檔案的文字資料流

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

代碼如下:


$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);
?>

根據壓縮檔內的索引(從0開始)修改壓縮檔內的檔案名稱

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

成功時返回 TRUE, 或者在失敗時返回 FALSE。

代碼如下:


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

根據壓縮檔內的檔案名稱,修改壓縮檔內的檔案名稱

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

代碼如下:


$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
//把壓縮檔內的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 ] )
參數:ZipArchive::FL_UNCHANGED
如果參數設定為 ZipArchive::FL_UNCHANGED, 返回原始的還沒有改變的注釋
例如,在處理該壓縮檔時,使用setArchiveComment()方法改變或設定注釋時
如果加上ZipArchive::FL_UNCHANGED這個參數,則表示擷取改變之前的注釋內容,否則擷取已經改變的注釋內容
類似的還有:
ZipArchive::getCommentIndex 根據壓縮檔內的檔案索引擷取【檔案注釋】
ZipArchive::getCommentName 根據壓縮檔內的檔案名稱擷取【檔案注釋】
注意:這裡的是檔案注釋,不是壓縮檔(zip)的注釋

設定或修改壓縮檔的注釋(zip的檔案注釋)
ZipArchive::setArchiveComment
(PHP 5 >= 5.2.0, PECL zip >= 1.4.0)
bool ZipArchive::setArchiveComment ( string $comment )

代碼如下:


$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';
}
?>

根據壓縮檔內的索引刪除壓縮檔內的檔案(也就是刪除檔案內的條目)

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

一、如何解壓縮一個zip檔案 extractTo()

代碼如下:


$zip = new ZipArchive();

一、如何建立壓縮檔? addFromString() addFile()

即是是把一個或多個檔案打包成一個zip檔案

1、只需要new一個ZipArchive對象
2、然後使用該對象的open方法建立一個zip檔案
3、接著使用addFile方法,將要打包的檔案寫入剛剛建立的zip檔案中
4、最後記得關閉該對象

代碼如下:


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

以上所述就是本文的全部內容了,希望能對大家有所協助。

http://www.bkjia.com/PHPjc/964003.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/964003.htmlTechArticlePHP內建ZIP壓縮、解壓縮類ZipArchiv使用指南 這篇文章主要介紹了PHP內建ZIP壓縮、解壓縮類ZipArchiv使用指南,十分詳細,需要的朋友可以參考下...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.