【问题标题】:stdin into zip command, how do I specify a file name? [duplicate]stdin 到 zip 命令,如何指定文件名? [复制]
【发布时间】:2015-07-09 11:14:12
【问题描述】:

我想用zip压缩stdin的内容,例如:

echo 'foo bar' | zip  > file.zip

这样可以,但是解压的时候解压出来的文件名是-

我想知道如何为标准输入指定文件名?

【问题讨论】:

  • 查看stackoverflow.com/questions/2019603/… 了解相同的问题
  • 不一样,他们假设在数据输入之前他们不知道文件名。也许 Mehdi(这是我的用例)知道他们想要文件是什么当他写 bash 行时调用。本质上是echo 'foo bar' > FILENAME && zip file.zip FILENAME && rm FILENAME的更高效版本

标签: linux zip stdin


【解决方案1】:

echo 'foo bar' | zip -@ /path/to/zipfile.zip 应该这样做,同时保持文件名的完整性

【讨论】:

  • 这不能回答问题。 OP想要压缩字符串'foo bar'这压缩了一个名为'foo bar'的文件
  • 这就是工作,+1
【解决方案2】:

你可以做的是正常输入文件,then rename it in the zip

echo 'foo bar' | zip  > file.zip
printf "@ -\n@=filename.txt\n" | zipnote -w file.zip

【讨论】:

  • 这并不理想,因为您不能在流设置中使用它(如果 zip 本身正在其他地方流式传输)
  • 我认为这实际上是不可能的,zip 没有提供强制命名的选项。如果您可以创建“文件”,一种方法可能是将您的数据命令通过管道传输到命名管道,然后将该命名管道添加到您的流式 zip(这将压缩该名称的新文件)
【解决方案3】:

将 echo 输出写入所需的文件名,然后使用 -m 标志一步压缩并删除原始文件。

echo 'foo bar' > filename.txt && zip -m filename.txt.zip filename.txt

【讨论】:

  • 但是你不是在这里吹。您首先要存储到光盘。
【解决方案4】:

使用 fifo(命名管道)而不是直接管道 stdin!

mkfifo text.txt               # create fifo
echo 'foo bar' > text.txt &   # pipe data to fifo, in background
zip --fifo file.zip text.txt  # note the `--fifo` argument to zip
rm text.txt                   # cleanup

结果:

$ unzip -l file.zip
Archive:  file.zip
Length      Date    Time    Name
---------  ---------- -----   ----
8          2020-11-29 12:30   text.txt
---------                     -------
8                             1 file

请注意,fifo 是一个命名管道。就像匿名管道 - 著名的 | - 没有数据存储在您的硬盘上,它直接从写入器流式传输到读取器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-28
    • 1970-01-01
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    相关资源
    最近更新 更多