【问题标题】:Creating and extracting files from zip archives从 zip 档案中创建和提取文件
【发布时间】:2010-10-01 01:52:43
【问题描述】:

是否有用于在 php 中创建/提取 zip 文件的库?

ZipArchive 类工作不规律,这在 php.net 上有所提及: (对于我检查的每个功能)

ZipArchive::addEmptyDir (没有可用的版本信息,可能只在 CVS 中)

【问题讨论】:

    标签: php zip archive


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      PHP 通过 GZip 提供原生 Zip 支持(默认情况下可能未启用):
      http://php.net/zlib

      你也可以使用这个类(Drupal 不是这个实现的一部分):
      http://drupal.org/node/83253

      【讨论】:

        【解决方案3】:

        same module which includes ZipArchive 还包括许多允许程序访问 zip 文件的功能。此功能在 PHP 4(自 4.0.7 起)和 PHP 5(自 5.2.0 起)中可用。

        $zip = zip_open("foo.zip");
        $files = [];
        
        while ($entry = zip_read($zip)) {
            zip_entry_open($zip, $entry);
            $files[zip_entry_name($entry)] = zip_entry_read($entry, zip_entry_filesize($entry));
            zip_entry_close($entry);
        }
        
        zip_close($zip);
        

        【讨论】:

          【解决方案4】:

          好的,我检查了 Irmantas 发布的 http://pear.php.net/package/Archive_Zip
          但它是这样说的:
          “此包不再维护,已被取代。包已移至频道 pecl.php.net,包 zip。” 然后我搜索了 pear.php.net 并偶然发现: http://pear.php.net/package/File_Archive

          File_Archive 虽然没有一套非常直观的方法。 但我想要制作 tar 文件的简单功能,并且 从 tar 文件中提取文件。

          按照 sn-ps 实现: 从 tar 文件中提取文件 ->

          <?php
          require_once "File/Archive.php";
          $tmp = 'output';
          $t1 = 'check.tar';
          File_Archive::setOption('tmpDirectory','tmp');
          $r = File_Archive::extract(
              File_Archive::read($t1.'/'),
              File_Archive::toFiles($tmp)
          );
          ?>
          

          将文件添加到 tar 文件 ->

          <?php
          require_once "Archive.php";
          $dir = "../../mysql/data/blackStone/";
          $files[0] = "";
          $i = 0;
                    if ($dh = opendir($dir)) {
                        while (($file = readdir($dh)) !== false ) {
                        if( $file != "." && $file != ".." )
                          $files[$i++] = $dir.$file;
                    }
                }
          
          File_Archive::extract(
              $files,
              File_Archive::toArchive(
                  'check.tar',
                  File_Archive::toOutput()
              )
          );
          
          ?>
          

          【讨论】:

            【解决方案5】:

            您可以从 PHP 调用到 .NET 程序集。 如果您不介意,那么您可以使用 DotNetZip - 它可靠且完全稳定。

            DotNetZip on CodePlex

            示例代码:

            <?php
            try
            {
              $fname = "zip-generated-from-php-" . date('Y-m-d-His') . ".zip";
              $zipOutput = "c:\\temp\\" . $fname;
              # Use COM interop to get to Ionic Zip. (Windows only)
              $zip = new COM("Ionic.Zip.ZipFile");
              $zip->Name = $zipOutput;
              $dirToZip= "c:\\temp\\psh";
              # Encryption:  3=AES256, 2=AES128, 1=PKZIP, 0=None.
              $zip->Encryption = 3;
              $zip->Password = "AES-Encryption-Is-Secure";
              $zip->AddDirectory($dirToZip);
              $zip->Save();
              $zip->Dispose();
            
              if (file_exists($zipOutput))
              {
                header('Cache-Control: no-cache, must-revalidate');
                header('Content-Type: application/x-zip'); 
                header('Content-Disposition: attachment; filename=' . $fname);
                header('Content-Length: ' . filesize($zipOutput));
                readfile($zipOutput);
                unlink($zipOutput);
              }
              else 
              {
                echo '<html>';
                echo '  <head>';
                echo '  <title>Calling DotNetZip from PHP through COM</title>';
                echo '  <link rel="stylesheet" href="basic.css"/>';
                echo '  </head>';
                echo '<body>';
                echo '<h2>Whoops!</h2>' . "<br/>\n";
                echo '<p>The file was not successfully generated.</p>';
                echo '</body>';
                echo '</html>';
              } 
            }
            catch (Exception $e) 
            {
                echo '<html>';
                echo '  <head>';
                echo '  <title>Calling DotNetZip from PHP through COM</title>';
                echo '  <link rel="stylesheet" href="basic.css"/>';
                echo '  </head>';
                echo '<body>';
                echo '<h2>Whoops!</h2>' . "<br/>\n";
                echo '<p>The file was not successfully generated.</p>';
                echo '<p>Caught exception: ',  $e->getMessage(), '</p>', "\n";
                echo '<pre>';
                echo $e->getTraceAsString(), "\n";
                echo '</pre>';
                echo '</body>';
                echo '</html>';
            }
            
            ?>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2012-10-11
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-12-18
              相关资源
              最近更新 更多