• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP使用 TbsZip 对 Zip 压缩文件进行操作

php 搞代码 4年前 (2022-01-23) 17次浏览 已收录 0个评论

使用 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_STR%本文@来源gao@!dai!ma.com搞$$代^@码!网搞代gaodaima码ING); // 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   

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP使用 TbsZip 对 Zip 压缩文件进行操作
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址