Linux 如何压缩没有完整路径的文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11317928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:14:04  来源:igfitidea点击:

How to zip folder without full path

phplinuxzipdirectory

提问by

firstly I don't which is the correct stackExchange site to post this question if the question is for other stack-site please remove my question.

首先,我不知道哪个是发布此问题的正确 stackExchange 站点,如果问题是针对其他堆栈站点的,请删除我的问题。

Now let talk about the question:This situation is I have a file which is located in: /home/user/public_html/folder-one/folder-two/folder-three/file.phpand the file must create archive of folder /folder-onewith all files and sub-folders of /folder-one. I create the archive with system function (exec(), shell_exec() or system()) and it works perfect. My code is:

现在让我们谈谈这个问题:这种情况是我有一个文件位于:/home/user/public_html/folder-one/folder-two/folder-three/file.php并且该文件必须创建文件夹的存档,/folder-one其中包含/folder-one. 我用系统函数(exec()、shell_exec() 或 system())创建了存档,它工作得很好。我的代码是:

<?php
$output = 'zip -rq my-zip.zip /home/user/public_html/folder-one -x missthis/\*';
shell_exec($output);
?>

But when I download and open the archive the archive include the sub-folders as /home; /user; /public_htmlbut this folders are totally unneeded and I wanna know how to create zip without them.

但是当我下载并打开档案时,档案包括子文件夹为/home/user; /public_html但是这个文件夹完全不需要,我想知道如何在没有它们的情况下创建 zip。

When I try with something like this $output = 'zip -rq my-zip.zip ../../../folder-one -x missthis/\*';but then when I open the archive (on Windows 7 based OS) the name of folder-one is ../folder-one

当我尝试使用这样的东西$output = 'zip -rq my-zip.zip ../../../folder-one -x missthis/\*';但是当我打开存档(在基于 Windows 7 的操作系统上)时,文件夹一的名称是../folder-one

Postscript: It will be better if somebody gives me correct$outputthe make zips on windows based hosting plans.

后记:如果有人给我更正基于 Windows 的托管计划上的 make zips会更好$output

Best regards, George!

最好的问候,乔治!

采纳答案by vkorchagin

By default, zip will store the full path relative to the current directory. So you have to cdinto public_htmlbefore running zip:

默认情况下,zip 将存储相对于当前目录的完整路径。所以你必须在运行之前cd进入:public_htmlzip

$output = 'cd /home/user/public_html; zip -rq my-zip.zip folder-one -x missthis/\*';
shell_exec($output);

回答by pgmank

There is a better way, without doing a cd. As stated herethere is an option to ignore full paths. This is -jor --junk-paths. So you could do:

有一种更好的方法,无需执行cd. 如前所述这里有忽略完整路径的选项。这是-j--junk-paths。所以你可以这样做:

<?php
$output = 'zip -jrq my-zip.zip /home/user/public_html/folder-one -x missthis/\*';
shell_exec($output);
?>