I have a zip/tar.gz that contain zip files that contains another zip file.

For example:

1.zip contains 2.tar.gz and 3.zip.
3.zip contains 4.zip.

I want to extract only 4.zip from 1.zip, I don't want to extract all of files.

How can I do that?

  • 2
    Do you have zip archives or tar archives? You mention both on the first line. – Kusalananda Dec 23 '19 at 20:52
  • Zip that contain zip and tar.gz – yfr24493AzzrggAcom Dec 23 '19 at 20:55
  • 1
    Do you mean that you need to extract 4.zip without extracting 3.zip first? From your writing it seems that '4.zip` is compressed inside 3.zip, which is compressed inside 1.zip. 1.zip "is not aware" of 4.zip, so it wouldn't be actually possible to extract it directly. Is it what you are asking? – Zip Dec 23 '19 at 21:03
  • Yes,you right ... – yfr24493AzzrggAcom Dec 23 '19 at 21:08
  • Could you make it clear on your question that 3.zip cannot be extracted in the process? You mention that you do not want to extract everything, but extracting 3.zip first and then 4.zip would meet that requirement and doesn't seem to be what you're looking for. – Zip Dec 23 '19 at 21:17

This is harder than it might seem because unzip can’t extract from its standard input, so you can’t chain it with pipes. However, jar can extract ZIP archives from its standard input, as can funzip (with some limitations).

If 3.zip only contains 4.zip, or contains 4.zip as its first member, then funzip can be used:

unzip -p 1.zip 3.zip | funzip > 4.zip

This uses unzip to extract 3.zip’s contents from 1.zip, piping them to standard output, into funzip which extracts 4.zip’s contents, and writing them to 4.zip.

jar works regardless of the other contents of 3.zip:

unzip -p 1.zip 3.zip | jar x 4.zip

This feeds 3.zip’s contents into jar, extracting 4.zip.

funzip is shipped alongside unzip; jar is part of the Java Development Kit, e.g. openjdk-11-jdk-headless on Debian 10.

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