20

Assume you started with an uncompressed directory A, with the following structure:

$ tree A
A
└── inner_dir
    ├── file1.txt
    └── file2.txt

1 directory, 2 files

Now assume you've been given two zip files 1.zip and 2.zip derived from A. 1.zip contains only file1.txt, and 2.zip contains only file2.txt:

# -l flag simply lists file contents

$ unzip -l 1.zip 
Archive:  1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2017-09-30 21:49   A/
        0  2017-09-30 22:27   A/inner_dir/
        0  2017-09-30 21:49   A/inner_dir/file1.txt
---------                     -------
        0                     3 files

$ unzip -l 2.zip 
Archive:  2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2017-09-30 21:49   A/
        0  2017-09-30 22:27   A/inner_dir/
        0  2017-09-30 22:27   A/inner_dir/file2.txt
---------                     -------
        0                     3 files

What command can I use to programmatically reconstitute the original unzipped directory A from the combination of 1.zip and 2.zip?

Context: when exporting a large directory from Google Drive, the contents are split across a set of 2 or more zip files. Each zip file contains some subset of the files, which when taken collectively, reconstitute the original directory. Which files are contains in which zip appears to be more or less random. I'm looking for a solution which generalizes to n > 1 zip files, not just the special case of exactly 2 files.

2 Answers 2

26

Move the set of files you want to unzip to a dedicated directory, and then use unzip:

$ unzip '*.zip' -d combined

Archive:  1.zip
   creating: combined/A/
   creating: combined/A/inner_dir/
 extracting: combined/A/inner_dir/file1.txt  

Archive:  2.zip
 extracting: combined/A/inner_dir/file2.txt  

2 archives were successfully processed.

Check:

$ tree combined
combined
└── A
    └── inner_dir
        ├── file1.txt
        └── file2.txt

2 directories, 2 files
2
  • 2
    I struggled a lot before finding this "obvious" solution. It's obvious because once unzip creates a folder, it doesn't recreate it when extracting the next archive. Doing so, the extraction is incremental. Thank you
    – pietrop
    Commented Mar 22, 2020 at 15:42
  • Skips files with accented characters in file name. Any work around for this?
    – lcharbon
    Commented Aug 16, 2022 at 17:29
1

@lcharbon your problem is with the way zip handles non-standard encoding.

You can solve it by using p7zip, that handles that better.

Here's the analog command using p7zip:

7z x '*.zip' -ocombined

You can install p7zip on mac using brew brew install p7zip or your distro's package managers on linux.

2
  • 1
    Hi Pedro you have made a claim about zip, you should provide a reference for it do yhat others csn tell it is true. Commented Sep 22, 2023 at 20:57
  • This did the trick for me! The other answer did not work with my Mac. Commented Sep 12 at 22:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .