Bash - 如何解压缩管道 zip 文件(来自“wget -qO-”)

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

Bash - how to unzip a piped zip file (from "wget -qO-")

bashwgetunzipxargs

提问by Roger

Any ideas on how to unzip a piped zip file like this:

关于如何解压缩这样的管道 zip 文件的任何想法:

wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip

I wished to unzip the file to a directory, like we used to do with a normal file:

我希望将文件解压缩到一个目录中,就像我们以前处理普通文件一样:

wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip | unzip -d ~/Desktop

采纳答案by leon

wget -q -O tmp.zip http://downloads.wordpress.org/plugin/akismet.2.5.3.zip && unzip tmp.zip && rm tmp.zip

回答by ruario

The ZIP file format includes a directory (index) at the end of the archive. This directory says where, within the archive each file is located and thus allows for quick, random access, without reading the entire archive.

ZIP 文件格式在存档末尾包含一个目录(索引)。这个目录说明了每个文件在存档中的位置,因此可以快速、随机地访问,而无需读取整个存档。

This would appear to pose a problem when attempting to read a ZIP archive through a pipe, in that the index is not accessed until the very end and so individual members cannot be correctly extracted until after the file has been entirely read and is no longer available. As such it appears unsurprising that most ZIP decompressors simply fail when the archive is supplied through a pipe.

当尝试通过管道读取 ZIP 存档时,这似乎会造成问题,因为直到最后才访问索引,因此在文件完全读取并且不再可用之前无法正确提取单个成员. 因此,当通过管道提供存档时,大多数 ZIP 解压缩器只会失败,这似乎并不奇怪。

The directory at the end of the archive is not the onlylocation where file meta information is stored in the archive. In addition, individual entries also include this information in a local file header, for redundancy purposes.

存档末尾的目录不是文件元信息存储在存档中的唯一位置。此外,出于冗余目的,各个条目还在本地文件头中包含此信息。

Although not every ZIP decompressor will use local file headers when the index is unavailable, the tar and cpio front ends to libarchive (a.k.a. bsdtar and bsdcpio) can and willdo so when reading through a pipe, meaning that the following is possible:

虽然不是每个 ZIP 解压器在索引不可用时都使用本地文件头,但 libarchive(又名 bsdtar 和 bsdcpio)的 tar 和 cpio 前端在通过管道读取时可以并且将会这样做,这意味着以下是可能的:

wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip | bsdtar -xvf- -C ~/Desktop

回答by Ian Robertson

While the following will not work in bash, it will work in zsh. Since many zsh users may end up here, it may still be useful:

虽然以下内容在 bash 中不起作用,但它在 zsh 中起作用。由于许多 zsh 用户可能最终会在这里,它可能仍然有用:

unzip =( wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip )

回答by lanzalibre

just use zcat

只需使用 zcat

wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip | zcat >> myfile.txt
  • This will only extract first file. You will see this error message "gzip: stdin has more than one entry--rest ignored" after the first file is extracted.
  • 这只会提取第一个文件。提取第一个文件后,您将看到此错误消息“gzip:stdin 有多个条目--rest 被忽略”。

回答by Saftever

BusyBox's unzipcan take stdin and extract all the files.

BusyBoxunzip可以使用标准输入并提取所有文件。

wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip | busybox unzip -

The dash after unzipis to use stdin as input.

后面的破折号unzip是使用 stdin 作为输入。

You can even,

你甚至可以,

cat file.zip | busybox unzip -

But that's just redundant of unzip file.zip.

但这只是多余的unzip file.zip

If your distro uses BusyBox by default (e.g. Alpine), just run unzip -.

如果您的发行版默认使用 BusyBox(例如 Alpine),只需运行unzip -.

回答by Sam Cantrell

I'd take a look at funzip (http://www.info-zip.org/mans/funzip.html). The man page for it notes,

我会看看 funzip (http://www.info-zip.org/mans/funzip.html)。它的手册页指出,

...filter for extracting from a ZIP archive in a pipe

Sorry I don't have an example, but it looks like it does come with the Linux unzip utility.

抱歉,我没有示例,但看起来它确实与 Linux 解压缩实用程序一起提供。