0

I have a python script that does AES decryption of an encrypted zip archive 'myzip.enc' I'm trying to use the output of that decryption and use it as stdin for "unzip" command.

Here is my code:

decrypt = subprocess.Popen(['openssl', 'enc', '-d', '-aes-256-cbc', '-md', 'sha256', '-in', '{}'.format(inputFile), '-pass', 'pass:{}'.format(passw_hash)], stdout=subprocess.PIPE) decompress = subprocess.Popen(['unzip', '-j', '-d', path_dict], stdin=decrypt.stdout) 

inputFile is my encrypted archive 'myzip.enc' passw_hash is the AES password path_dict is a folder path where to extract the decrypted zip

I'm getting this in my terminal:

Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir] Default action is to extract files in list, except those in xlist, to exdir; file[.zip] may be a wildcard. -Z => ZipInfo mode ("unzip -Z" for usage). -p extract files to pipe, no messages -l list files (short format) -f freshen existing files, create none -t test compressed archive data -u update files, create if necessary -z display archive comment only -v list verbosely/show version info -T timestamp archive to latest -x exclude files that follow (in xlist) -d extract files into exdir modifiers: -n never overwrite existing files -q quiet mode (-qq => quieter) -o overwrite files WITHOUT prompting -a auto-convert any text files -j junk paths (do not make directories) -aa treat ALL files as text -U use escapes for all non-ASCII Unicode -UU ignore any Unicode fields -C match filenames case-insensitively -L make (some) names lowercase -X restore UID/GID info -V retain VMS version numbers -K keep setuid/setgid/tacky permissions -M pipe through "more" pager See "unzip -hh" or unzip.txt for more help. Examples: unzip data1 -x joe => extract all files except joe from zipfile data1.zip unzip -p foo | more => send contents of foo.zip via pipe into program more unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer 

Is there something wrong in my unzip command? Thanks.


Edit: It seems from Here that it is impossible to PIPE an output of zip archive to the unzip command due to the fact that unzip needs to read some info from the physical file.

My workaround ended up being this code which works:

 output = open('{}.zip'.format(inputFile), "wb") decrypt = subprocess.Popen(['openssl', 'enc', '-d', '-aes-256-cbc', '-md', 'sha256', '-in', '{}'.format(inputFile), '-pass', 'pass:{}'.format(passw_hash)], stdout=output) decompress = subprocess.Popen(['unzip', '{}.zip'.format(inputFile), '-d', path_dict[0]]) 

Is there a way to unzip and delete the zip archive on the same time or add an rm to the decompress line ?

Thanks.

12
  • 1
    Does this answer your question? Bash - how to unzip a piped zip file (from "wget -qO-") Commented Nov 14, 2021 at 16:32
  • I guess that unzip wants a file parameter. Try specifying - to indicate that you want it to read from stdin. Commented Nov 14, 2021 at 16:32
  • @Manfred That doesn't work. Commented Nov 14, 2021 at 16:33
  • Also, is it a requirement that you use zip and not tar? If you can use tar instead, then it's trivial. Commented Nov 14, 2021 at 16:34
  • Hi, yes I need unzip that is why the archive is zip. Commented Nov 14, 2021 at 16:36

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.