据我所知,你不能同时使用zip 命令,我的意思是你不能同时指定文件名和管道内容。您可以通过管道传输内容,生成的文件是-,或者您可以使用-@ 管道传输文件名。
这并不意味着使用其他技术不可能做到这一点。我概述了以下其中之一。这确实意味着您必须安装 PHP 并加载 zip 扩展。
可能有很多其他方法可以做到这一点。但这是我所知道的最简单的。哦,它也是单行的。
这是一个使用 PHP 的工作示例
echo contents | php -r '$z = new ZipArchive();$z->open($argv[1],ZipArchive::CREATE);$z->addFromString($argv[2],file_get_contents("php://stdin"));$z->close();' test.zip testfile
要在 Windows 上运行,只需交换单引号和双引号。或者只是将脚本放在一个文件中。
“test.zip”是生成的 Zip 文件,“testfile”是通过管道传输到命令中的内容的名称。
来自unzip -l test.zip的结果
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
6 01-07-2010 12:56 testfile
--------- -------
6 1 file
这是一个使用 python 的工作示例
echo contents | python -c "import sys
import zipfile
z = zipfile.ZipFile(sys.argv[1],'w')
z.writestr(sys.argv[2],sys.stdin.read())
z.close()
" test5.zip testfile2
unzip -l的结果
Archive: test5.zip
Length Date Time Name
-------- ---- ---- ----
9 01-07-10 13:43 testfile2
-------- -------
9 1 file
unzip -p的结果
contents