linux如何将文件添加到zip文件中的特定文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9289734/
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 04:35:09  来源:igfitidea点击:

linux how to add a file to a specific folder within a zip file

linuxzip

提问by asfman

i know how to add a file to the root folder within a zip file:

我知道如何将文件添加到 zip 文件中的根文件夹:

zip -g xxx.apk yyy.txt

zip -g xxx.apk yyy.txt

but i've no idea how to specify a particular folder within a zip file

但我不知道如何在 zip 文件中指定特定文件夹

采纳答案by jcollado

If you need to add the file to the same folder as in the original directory hierarchy, then you just need to add the full path to it:

如果您需要将文件添加到与原始目录层次结构相同的文件夹中,那么您只需要向其添加完整路径:

zip -g xxx.zip folder/file

Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.

否则,最简单的方法可能是在临时目录中的 zip 文件中创建您需要的相同布局。

回答by Ignacio Vazquez-Abrams

Info-ZIP cannot do this. You will need to write a script or program in a language that has lower-level access to zip files.

Info-ZIP 无法做到这一点。您将需要使用对 zip 文件具有较低级别访问权限的语言编写脚本或程序。

回答by that other guy

To elaborate on @Ignacio Vazquez-Abrams answer from a year ago, you can use a lower level library, such as the one that comes with Python:

要详细说明一年前@Ignacio Vazquez-Abrams 的回答,您可以使用较低级别的库,例如 Python 附带的库:

#!/bin/bash
python -c '
import zipfile as zf, sys
z=zf.ZipFile(sys.argv[1], "a")
z.write(sys.argv[2], sys.argv[3])
z.close()
' myfile.zip source/dir/file.txt dir/in/zip/file.txt

This will open myfile.zipand add source/dir/file.txtfrom the file system as dir/in/zip/file.txtin the zip file.

这将打开myfile.zipsource/dir/file.txt从文件系统添加,就像dir/in/zip/file.txt在 zip 文件中一样。

回答by test30

I have expended a little @"that other guy" solution

我花了一点@“那个其他人”的解决方案

Go to console, press ctrl+x,ctrl+e, paste there

转到控制台,按 ctrl+x,ctrl+e,粘贴到那里

( cat <<-'EOF'
#!/bin/bash
if [ $# -lt 3 ]; then
echo my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml
exit
fi

python -c '
import zipfile as zf, sys
z=zf.ZipFile(sys.argv[1], "a")
z.write(sys.argv[2], sys.argv[3])
z.close()'   
EOF
) > /tmp/zip-extend && chmod +x /tmp/zip-extend

then run /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml

然后运行 /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml

Example:

例子:

cd /tmp
touch first_file.txt
zip my_zip.zip first_file.txt
unzip -l my_zip.zip
mkdir -p your/existing
touch your/existing/file_to_add.xml
/tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml
unzip -l my_zip.zip
cd -

Result:

结果:

Archive:  my_zip.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2013-12-17 15:24   first_file.txt
        0  2013-12-17 15:24   directory_in_zip/file_to_add.xml
---------                     -------
        0                     2 files