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

发布于 2024-11-30 08:20:36 字数 275 浏览 2 评论 0原文

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

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

丢了幸福的猪 2024-12-07 08:20:36

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

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

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

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

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

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.

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.

The directory at the end of the archive is not the only location 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 will do so when reading through a pipe, meaning that the following is possible:

wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip | bsdtar -xvf- -C ~/Desktop
执笏见 2024-12-07 08:20:36

BusyBox 的 unzip 可以采用标准输入并提取所有文件。

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

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

你甚至可以,

cat file.zip | busybox unzip -

但这只是unzip file.zip的多余。

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

BusyBox's unzip can take stdin and extract all the files.

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

The dash after unzip is to use stdin as input.

You can even,

cat file.zip | busybox unzip -

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

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

深居我梦 2024-12-07 08:20:36

只需使用 zcat

wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip | zcat >> myfile.txt
  • 这只会提取第一个文件。提取第一个文件后,您将看到此错误消息“gzip:stdin 有多个条目 - 忽略其余部分”。

just use 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.
長街聽風 2024-12-07 08:20:36

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

% unzip =( wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip )
Archive:  /tmp/zshLCod6x
   creating: akismet/
  inflating: akismet/admin.php       
  inflating: akismet/akismet.css     
  inflating: akismet/akismet.gif     
  inflating: akismet/akismet.js      
  inflating: akismet/akismet.php     
  inflating: akismet/legacy.php      
  inflating: akismet/readme.txt      
  inflating: akismet/widget.php      
% 

正如您所注意到的,临时下载的 zip 文件已被立即删除:

% ls /tmp/zshLCod6x
ls: cannot access '/tmp/zshLCod6x': No such file or directory
% 

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:

% unzip =( wget -qO- http://downloads.wordpress.org/plugin/akismet.2.5.3.zip )
Archive:  /tmp/zshLCod6x
   creating: akismet/
  inflating: akismet/admin.php       
  inflating: akismet/akismet.css     
  inflating: akismet/akismet.gif     
  inflating: akismet/akismet.js      
  inflating: akismet/akismet.php     
  inflating: akismet/legacy.php      
  inflating: akismet/readme.txt      
  inflating: akismet/widget.php      
% 

As you can notice the temporary downloaded zip file has been deleted straight away :

% ls /tmp/zshLCod6x
ls: cannot access '/tmp/zshLCod6x': No such file or directory
% 
泅人 2024-12-07 08:20:36
wget -q -O tmp.zip http://downloads.wordpress.org/plugin/akismet.2.5.3.zip && unzip tmp.zip && rm tmp.zip
wget -q -O tmp.zip http://downloads.wordpress.org/plugin/akismet.2.5.3.zip && unzip tmp.zip && rm tmp.zip
揽清风入怀 2024-12-07 08:20:36

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

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

抱歉,我没有示例,但看起来它确实附带了 Linux 解压缩实用程序。

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

...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.

红颜悴 2024-12-07 08:20:36

重新发布我的答案

我编写了一个Python(2.x)脚本来对ZIP档案进行流式提取,你可以从这里获取它: https://raw.githubusercontent.com/pts/unzip_scan/master/unzip_scan.py 。用法:cat file.zip | sh unzip_scan.py - 。

Reposting my answer:

I wrote a Python (2.x) script to do streaming extraction of ZIP archives, you can get it from here: https://raw.githubusercontent.com/pts/unzip_scan/master/unzip_scan.py . Usage: cat file.zip | sh unzip_scan.py -.

拥有 2024-12-07 08:20:36

另一种解决方案是,如果您已经有了 unzip,那么您还应该拥有来自同一个 [unzip] 包的 funzip

该实用程序用于从管道/标准输入读取。然而,它似乎非常原始,并且显然只能从存档中提取第一个文件(根据联机帮助页)。

无论如何,为了问题上下文的完整性,这是我发现的一种使用 funzip 来实现单文件 .zip 存档的方法:

curl -sL https://<url_to_my_archive>.zip | funzip - > <my_extracted_file>

替换 包含您的值。

Another solution if you already have unzip, you should also have funzip which comes from the same [unzip] package.

This utility is made for reading from pipes/stdin. However it seems very primitive and can apparently extract only the first file from the archive (as per the manpage).

Anyway for the sake of completeness on the context of the question, here is a way I found to do it with funzip for a single-file .zip archive:

curl -sL https://<url_to_my_archive>.zip | funzip - > <my_extracted_file>

replace <url_to_my_archive> and <my_extracted_file> with your values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文