douzhaochan6468 2013-03-20 10:31
浏览 535

压缩不包括当前目录

I use the following for zipping

//http://ninad.pundaliks.in/blog/2011/05/recursively-zip-a-directory-with-php/

class FlxZipArchive extends ZipArchive {

    public function addDir($location, $name) {
        $this->addEmptyDir($name);     
        $this->addDirDo($location, $name);
     } // EO addDir

    private function addDirDo($location, $name) {
        $name .= '/';
        $location .= '/';

        // Read all Files in Dir
        $dir = opendir ($location);
        while ($file = readdir($dir))
        {
            if ($file == '.' || $file == '..') continue;

            // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
            $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
            $this->$do($location . $file, $name . $file);
        }
    } // EO addDirDo();
}


function zipIt($source, $target){
  $za = new FlxZipArchive;

  $res = $za->open($target, ZipArchive::CREATE);

  if($res === TRUE) {
      $za->addDir($source, basename($source));
      $za->close();
  }
  else
      echo 'Could not create a zip archive';

}

$the_folder = './Sales report';
$zip_file_name = './Sales report.docx';
//Don't forget to remove the trailing slash in folder
zipIt($the_folder,$zip_file_name,false);

The problem is that it includes the current directory,

Sales report/
    file1.html
    file2.html
    Sub_Dir/
        file19.html

But I just want

    file1.html
    file2.html
    Sub_Dir/
        file19.html

How to do that?

  • 写回答

2条回答 默认 最新

  • dongyong5912 2013-03-20 10:35
    关注

    Here's what I use:

    /**
     * Add a directory and its contents to the archive
     *
     * @param string $dir       The local filesystem path to the directory
     * @param string $localName The archive filesystem path to the directory
     *
     * @throws \RuntimeException When adding an object to the archive fails
     */
    public function addDir($dirPath, $localName = NULL)
    {
        if ($localName === NULL) {
            $localName = basename($dirPath);
        }
        if (!$this->addEmptyDir($localName)) {
            throw new \RuntimeException('Error adding directory '.$dirPath.' to archive');
        }
    
        $this->addDirContents($dirPath, $localName);
    }
    
    /**
     * Add the contents of a directory to the archive
     *
     * @param string $dir       The local filesystem path to the directory
     * @param string $localName The archive filesystem path to the directory
     *
     * @throws \RuntimeException When adding an object to the archive fails
     */
    public function addDirContents($dirPath, $localName = '')
    {
        $base = ltrim($localName.'/', '/');
    
        foreach (glob("$dirPath/*") as $file) {
            if (is_dir($file)) {
                $this->addDir($file, $base.basename($file));
            } else {
                if (!$this->addFile($file, $base.basename($file))) {
                    throw new \RuntimeException('Error adding file '.$file.' to archive');
                }
            }
        }
    }
    

    Like your code, these methods belong in a class that extends \ZipArchive. addDir() adds a directory and it's contents, addDirContents() just adds the contents without creating the parent directory in the archive (and therefore does what you want).

    I don't generally like to just give out free working code with no explanation but it just so happens I have exactly what you need already open in my editor.

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用