betway必威手机版 > betway必威手机版 > 压缩文件进行操作,php生成zip压缩文件两个实例

原标题:压缩文件进行操作,php生成zip压缩文件两个实例

浏览次数:200 时间:2019-10-13

TbsZip 是三个 PHP 的类用来读写 Zip 压缩文件包,该类不要求 PHP 扩大可能是有时文件。TbsZip 能够对减少文书档案中的文件实行读、写、修改和删除操作。

注意:

<?

ZipArchive来压缩文件。这一个是php的恢宏类,自php5.2版本之后就早就扶助那个扩充,要是您在应用的时候出现错误,查看下php.ini里面包车型客车extension=php_zip.dll后边的支行有未有去掉,然后再重启Apache那样技能使用那个类库。

/* Some code examples for TbsZip
Skrol29, 2010-09-03
*/

例1

include_once('tbszip.php'); // load the TbsZip library
$zip = new clsTbsZip(); // create a new instance of the TbsZip class

生成zip 压缩文件

$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
// --------------------------------------------------

<?php
/* 生成zip 压缩文件 */
function create_zip($files = array(),$destination = '',$overwrite = false) {
    //if the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }
    //vars
    $valid_files = array();
    //if files were passed in...
    if(is_array($files)) {
        //cycle through each file
        foreach($files as $file) {
            //make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    //if we have good files...
    if(count($valid_files)) {
        //create the archive
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $file_info_arr= pathinfo($file);
            $zip->addFile($file,$file_info_arr['basename']);//去掉层级目录
        }
        //debug
        //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
 
        //close the zip -- done!
        $zip->close();
 
        //check to make sure the file exists
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}
 
define('ROOTPATH',dirname ( __FILE__ )); //网址路线
 
$files_to_zip = array(
    ROOTPATH.DIRECTORY_SEPARATOR.'PHP jQuery Cookbook.pdf',
    ROOTPATH.DIRECTORY_SEPARATOR.'TurboListerZeroTemplate.csv'
);
//if true, good; if false, zip creation failed
$filename='my-archive.zip';
$result = create_zip($files_to_zip,$filename);

// check if a file is existing in the archive, the name must precise subfolders if any
$ok = $zip->FileExists('subfolder/help.html');

例2

// 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');

<?php
/*
php zip压缩文件夹上边的享有文件
*/
class HZip
{
  /**
   * 添Gavin件和子目录的文书到zip文件
   * @param string $folder
   * @param ZipArchive $zipFile
   * @param int $exclusiveLength Number of text to be exclusived from the file path.
   */
  private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
    $handle = opendir($folder);
    while (false !== $f = readdir($handle)) {
      if ($f != '.' && $f != '..') {
        $filePath = "$folder/$f";
        // Remove prefix from file path before add to zip.
        $localPath = substr($filePath, $exclusiveLength);
        if (is_file($filePath)) {
          $zipFile->addFile($filePath, $localPath);
        } elseif (is_dir($filePath)) {
          // 增添子文件夹
          $zipFile->addEmptyDir($localPath);
          self::folderToZip($filePath, $zipFile, $exclusiveLength);
        }
      }
    }
    closedir($handle);
  }
 
  /**
   * Zip a folder (include itself).
   * Usage:
   *   HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
   *
   * @param string $sourcePath Path of directory to be zip.
   * @param string $outZipPath Path of output zip file.
   */
  public static function zipDir($sourcePath, $outZipPath)
  {
    $pathInfo = pathInfo($sourcePath);
    $parentPath = $pathInfo['dirname'];
    $dirName = $pathInfo['basename'];
    $sourcePath=$parentPath.'/'.$dirName;//幸免传递'folder' 文件夹产生bug
    $z = new ZipArchive();
    $z->open($outZipPath, ZIPARCHIVE::CREATE);//建立zip文件
    $z->addEmptyDir($dirName);//创建文件夹
    self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
    $z->close();
  }
}
 
//使用格局
HZip::zipDir('yourlife', 'yourlife.zip');
?>

if ($ok) $zip->FileExists('subfolder/help.html');

/******** ziparchive 可选参数 *******/ /*
1.ZipArchive::addEmptyDir

// -----------------------------
// 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

2.ZipArchive::addFile

// 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

将文件加多到钦定zip压缩包中。

本文由betway必威手机版发布于betway必威手机版,转载请注明出处:压缩文件进行操作,php生成zip压缩文件两个实例

关键词:

上一篇:spark笔记总结,Spark快速大数据分析之RDD基础

下一篇:没有了