PHP使用 TbsZip 對 Zip 壓縮檔進行操作

來源:互聯網
上載者:User
使用 TbsZip 對 Zip 壓縮檔進行操作

TbsZip 是一個 PHP 的類用來讀寫 Zip 壓縮檔包,該類無需 PHP 擴充或者是臨時檔案。

TbsZip 可以對壓縮文檔中的檔案進行讀、寫、修改和刪除操作。

/* Some code examples for TbsZipSkrol29, 2010-09-03*/include_once('tbszip.php'); // load the TbsZip library$zip = new clsTbsZip(); // create a new instance of the TbsZip class$zip->CreateNew(); // start a new empty archive for adding files// or$zip->Open('an_existing_archive.zip'); // open an existing archive for reading and/or modifying// --------------------------------------------------// Reading information and data in the opened archive// --------------------------------------------------// check if a file is existing in the archive, the name must precise subfolders if any$ok = $zip->FileExists('subfolder/help.html');// count the files stored in the archive$file_nbr = count($zip->CdFileLst);// retrieve the content of an compressed file in the archive$text1 = $zip->FileRead('readme.txt');// retrieve the content of an compressed file in a subfolder of the archive$text2 = $zip->FileRead('subfolder/readme.txt');if ($ok) $zip->FileExists('subfolder/help.html');// -----------------------------// Modifying data in the archive// -----------------------------// add a file in the archive$zip->FileAdd('newfile.txt', $data, TBSZIP_STRING); // add the file by giving the content$zip->FileAdd('newpic1.png', './images/localpic1.png', TBSZIP_FILE);        // add the file by copying a local file$zip->FileAdd('newpic2.png', './images/localpic2.png', TBSZIP_FILE, false); // add the uncompressed file by copying a local file// delete an existing file in the archive$zip->FileReplace('newfile.txt', $data, TBSZIP_STRING); // replace the file by giving the content$zip->FileReplace('newpic1.png', './images/localpic1.png', TBSZIP_FILE);        // replace the file by copying a local file$zip->FileReplace('newpic2.png', './images/localpic2.png', TBSZIP_FILE, false); // replace the uncompressed file by copying a local file$zip->FileReplace('newpic3.png', false);                                        // delete the file in the archive// cancel the last modification on the file (add/replace/delete)$zip->FileCancelModif('newpic2.png');// ----------------------// Applying modifications// ----------------------$zip->Flush(TBSZIP_FILE, './save/new_archive.zip'); // apply modifications as a new local file// apply modifications as an HTTP downloaded file$zip->Flush(TBSZIP_DOWNLOAD, 'download.zip');$zip->Flush(TBSZIP_DOWNLOAD, 'download.zip', 'application/zip'); // with a specific Content-Type// apply modifications as a downloaded file with your customized HTTP headersheader("Content-type: application/force-download");header("Content-Disposition: attachment; filename=download.zip");header("Expires: Fri, 01 Jan 2010 05:00:00 GMT");$zip->Flush(TBSZIP_DOWNLOAD+TBSZIP_NOHEADER);// -----------------// Close the archive// -----------------$zip->Close(); // stop to work with the opened archive. Modifications are not applied to the opened archive, use Flush() to commit   
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.