【问题标题】:Extract ZIP file, including hidden files提取 ZIP 文件,包括隐藏文件
【发布时间】:2017-03-25 11:54:08
【问题描述】:

我正在使用 ZipArchive,我想提取 ZIP 文件。 我可以在 ZipArchive::extractTo: 的文档中看到评论:

注意,在 Linux(可能还有其他 *nix 平台)中,无法从 Zip 存档中提取隐藏文件(也就是文件名以“.”开头)。

非常重要的一点:我需要它在 *nix 和 Windows 平台上工作。

那么如何正确解压ZIP文件到目录,包括隐藏文件?

提前致谢, JK。

【问题讨论】:

    标签: php ziparchive php-zip-archive


    【解决方案1】:

    如果服务器允许,您可以使用 system()exec() 函数:

    system("unzip archive.zip");
    

    虽然有shell_exec()passthru()

    希望对您有所帮助。

    编辑

    由于 OP 在这里寻找纯 php 解决方案,因此用户实现了 php 手册 (Zip Functions) 的功能:

    <?php
    /**
    * Unzip the source_file in the destination dir
    *
    * @param   string      The path to the ZIP-file.
    * @param   string      The path where the zipfile should be unpacked, if false the directory of the zip-file is used
    * @param   boolean     Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
    * @param   boolean     Overwrite existing files (true) or not (false)
    * 
    * @return  boolean     Succesful or not
    */
    function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
    {
      if ($zip = zip_open($src_file))
      {
        if ($zip)
        {
          $splitter = ($create_zip_name_dir === true) ? "." : "/";
          if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
    
          // Create the directories to the destination dir if they don't already exist
          create_dirs($dest_dir);
    
          // For every file in the zip-packet
          while ($zip_entry = zip_read($zip))
          {
            // Now we're going to create the directories in the destination directories
    
            // If the file is not in the root dir
            $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
            if ($pos_last_slash !== false)
            {
              // Create the directory where the zip-entry should be saved (with a "/" at the end)
              create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
            }
    
            // Open the entry
            if (zip_entry_open($zip,$zip_entry,"r"))
            {
    
              // The name of the file to save on the disk
              $file_name = $dest_dir.zip_entry_name($zip_entry);
    
              // Check if the files should be overwritten or not
              if ($overwrite === true || $overwrite === false && !is_file($file_name))
              {
                // Get the content of the zip entry
                $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
    
                file_put_contents($file_name, $fstream );
                // Set the rights
                chmod($file_name, 0777);
                echo "save: ".$file_name."<br />";
              }
    
              // Close the entry
              zip_entry_close($zip_entry);
            }      
          }
          // Close the zip-file
          zip_close($zip);
        }
      }
      else
      {
        return false;
      }
    
      return true;
    }
    

    【讨论】:

    • 是的,这可行,但如果可能的话,我正在寻找纯 PHP 解决方案。非常重要的一点:我需要它在 *nix 和 Windows 平台上工作。
    • 好的,看看这里:php.net/manual/en/ref.zip.php 有一些用户实现的功能可以帮助你。
    • 也就是说,shell_exec() 应该适用于(unix 和 windows)系统。
    • 是的,我明白这一点。但是unzip 不会:)
    • 谢谢。 Zip Functions的cmets中的函数可以是一条路:)我试试。
    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 2011-01-25
    • 2016-03-08
    • 2017-03-16
    • 2015-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多