压缩不包括当前目录

时间:2013-03-20 10:31:32

标签: php zip

我使用以下内容进行压缩

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

问题是它包含当前目录

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

但我只想要

    file1.html
    file2.html
    Sub_Dir/
        file19.html

怎么做?

2 个答案:

答案 0 :(得分:1)

以下是我使用的内容:

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

与您的代码一样,这些方法属于扩展\ZipArchive的类。 addDir()添加目录及其内容,addDirContents()只是添加内容而不在归档中创建父目录(因此可以执行您想要的操作)。

我一般不喜欢在没有解释的情况下发布免费工作代码,但事实恰恰相反,我已经在编辑器中打开了你需要的内容。

答案 1 :(得分:0)

我最终使用此代码,提供选择是否包含当前目录的选项:

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



class FlxZipArchive extends ZipArchive {

    public function addDir1($location, $name, $includeCurrDir) {
      if($includeCurrDir)
        $this->addEmptyDir($name);     
        $this->addDirDo1($location, $name, $includeCurrDir);
     } // EO addDir

    private function addDirDo1($location, $name, $includeCurrDir) {
      if($includeCurrDir)
        $name .= '/';
      else
        $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();

    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, $includeCurrDir){
  $za = new FlxZipArchive;

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

  if($res === TRUE) {
      $za->addDir1($source, basename($source),$includeCurrDir);
      $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);


?>