如何压缩文件夹然后将其解压缩到其他位置?

发布于 2024-08-04 22:39:14 字数 69 浏览 4 评论 0原文

我正在开发一个网站,该网站下载用户从我们的其他服务器上传的文件夹,但首先我们必须压缩该文件夹 - 如何压缩整个文件夹的内容?

I'm working on a site which downloads folders users upload from our other server, but first we have to zip that folder - how do I zip up a whole folders' contents?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

太阳公公是暖光 2024-08-11 22:39:14

查看 ZipArchive。如果您遇到特定问题,请编辑您的问题,我会尽力提供更多帮助。您可能没有安装 ZipArchive - 如果没有,请按照 安装说明

您需要的功能:

示例代码:

<?php

$writezip = new ZipArchive;
if ($writezip->open('test.zip') === TRUE) {
    if($zip->addEmptyDir('newDirectory')) {
        $writezip->addFile('/path/to/index.txt', 'newDirectory/newname.txt');
        $writezip->close();
        echo 'ok';
    }
    else {
        echo 'failed to create directory';
    }
}
else {
    echo 'failed';
}

$readzip = new ZipArchive;
if ($readzip->open('test.zip') === TRUE) {
    $readzip->extractTo('/my/destination/dir/');
    $readzip->close();
    echo 'ok';
}
else {
    echo 'failed';
}
?>

Take a look at the documentation for ZipArchive. If you've got a particular problem with it, edit your question and I'll try to be more helpful. You may not have ZipArchive installed - if not, follow the installation instructions.

Functions that you'll need:

  • open: Opens the zip archive (you'll want to use the flag ZIPARCHIVE::CREATE).
  • addEmptyDir: Creates an empty directory in the zip archive that you can add files to.
  • addFile: Adds a file on your filesystem to the archive. You'll presumably want to run through the folder you wish to add files from calling this function.
  • close: Close the zip archive when you're done writing to it.
  • extractTo: Get the files out of the zip archive to put them somewhere

Sample code:

<?php

$writezip = new ZipArchive;
if ($writezip->open('test.zip') === TRUE) {
    if($zip->addEmptyDir('newDirectory')) {
        $writezip->addFile('/path/to/index.txt', 'newDirectory/newname.txt');
        $writezip->close();
        echo 'ok';
    }
    else {
        echo 'failed to create directory';
    }
}
else {
    echo 'failed';
}

$readzip = new ZipArchive;
if ($readzip->open('test.zip') === TRUE) {
    $readzip->extractTo('/my/destination/dir/');
    $readzip->close();
    echo 'ok';
}
else {
    echo 'failed';
}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文