1

I have an encrypted file like file.zip.gpg.

Decryption is done using gpg --output file.zip --decrypt file.zip.gpg and unzipping the archive contents using 7z(a) x file.zip afterwards.

The first command obviously creates a (temporary) file. I would like to know if both commands could be combined into a single command (pipe?) to prevent the creation of the temporary file on the disk.

I tried

gpg --decrypt file.zip.gpg | 7z x -si

but the 7z outputs appears before gpg asks for the passphrase: Extracting archive: ERROR: Can not open the file as archive.

  • 2
    Why not use the encryption built into 7-zip? – harrymc Dec 12 '19 at 21:07
  • Unfortunately I have no influence on the file type. – Marcello90 Dec 15 '19 at 16:29
  • Maybe this: gpg --decrypt file.zip.gpg -o /dev/stdout | 7z x -si. Another try is 7z x -si <(gpg --decrypt file.zip.gpg). – harrymc Dec 15 '19 at 17:46
3
+50

You did not tag , but the filename contains .zip, so I assume after decrypting it's really a zip file.

See this answer:

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, […]

In my Debian 9 the solution with bsdtar (from libarchive-tools package) works:

gpg --decrypt file.zip.gpg | bsdtar -xvf-

Other answers provide other solutions. Some of them do use a temporary file in disguise (e.g. =( … ) in zsh), some of them don't (e.g. busybox unzip -, but this one doesn't work for me).


If the format was 7z instead of zip, then the problem would be similar. Compare this thread:

Trying to pipe through 7za

I'm trying to run 7za in a pipeline within a C++ program. […]

Same thing if I try to do it on the command line.

7za e -an -si -so < filename.7z >/dev/null
ERROR:
Can not open the file as archive

E_NOTIMPL

The answer from Igor Pavlov (the creator of 7-zip) is:

7-zip needs "seek" operation for 7z archives. So you can't use -si to extract from 7z archive.

This was dated 2016-03-30. My tests and the changelog indicate the statement is still valid in 2019. Moreover bsdtar in my Debian cannot help, it yields Error seeking in stdin. I guess the 7z format really needs a seekable file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.