Php 不包含当前目录的压缩

Php 不包含当前目录的压缩,php,zip,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

我用下面的方法来拉拉链

//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
如何做到这一点?

以下是我使用的方法:

/**
 * 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()
只添加内容,而不在存档中创建父目录(因此执行您想要的操作)

我通常不喜欢只给出免费的工作代码而不做任何解释,但碰巧我已经在我的编辑器中打开了您所需要的内容。

以下是我使用的内容:

/**
 * 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()
只添加内容,而不在存档中创建父目录(因此执行您想要的操作)


我通常不喜欢只给出免费的工作代码而不做任何解释,但碰巧我已经在我的编辑器中打开了您需要的内容。

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

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


?>

最后,我使用此代码提供一个选项,用于选择是否包含当前目录:

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


?>

但是我需要包含子目录,就像示例中包含的sub_Dir一样,您的代码是否支持此功能?@william007是的,它会递归地将目录内容添加到存档中。如果您调用
addDir()
您将获得第一个示例布局,如果您调用
addDirContents()
您将获得第二个(所需)布局,但我需要包含子目录,正如示例中包含的子目录一样,您的代码是否支持此功能?@william007是的,它将递归地将目录内容添加到存档中。如果调用
addDir()
将获得第一个示例布局,如果调用
addDirContents()
将获得第二个(所需)布局