10

I have a ZIP file in which there is a top directory where all the files are stored:

Release/
Release/file
Release/subdirectory/file
Release/subdirectory/file2
Release/subdirectory/file3

I want to extract everything under Release, preserving the directory structure, but when I run this:

unzip archive.zip Release/* -d /tmp

It creates the top Release folder:

/tmp/Release/
/tmp/Release/file
/tmp/Release/subdirectory/file
/tmp/Release/subdirectory/file2
/tmp/Release/subdirectory/file3

How I can extract everything inside Release without creating a Release folder, like this:

/tmp/
/tmp/file
/tmp/subdirectory/file
/tmp/subdirectory/file2
/tmp/subdirectory/file3
  • Try this unzip archive.zip && mv Release/* . – George Udosen Mar 12 '17 at 0:48
  • @George this still creates a Release folder – jsta Jun 8 '17 at 17:47
5

In your case, try in target folder:

ln -s Release . && unzip <YourArchive>.zip

Than you need to remove the link you've created:

rm Release
2

The j flag should prevent folder creation unzip -j archive.zip -d .

From the man page:

-j 

junk paths. The archive's directory structure is not recreated; 
all files are deposited in the extraction directory (by default, the
current one).
  • 6
    I think this is close, but the OP was looking to only skip creation of the top level directory and preserve the remaining directory structure. the -j option dumps all files into the current directory without regard to the directory structure in the archive. – Charles Green Jun 8 '17 at 19:00
1

Python script for flattening extracted tree

The script written bellow extracts zip file and moves files contained within topmost directory out of it to the current working directory. This quick script is tailored to suit this particular question where there is one single topmost directory that contains all the files, although with a few edits can be made suitable for more general cases.

#!/usr/bin/env python3
import sys
import os
from zipfile import PyZipFile
for zip_file in sys.argv[1:]:
    pzf = PyZipFile(zip_file)
    namelist=pzf.namelist()
    top_dir = namelist[0]
    pzf.extractall(members=namelist[1:])
    for item in namelist[1:]:
        rename_args = [item,os.path.basename(item)]
        print(rename_args)
        os.rename(*rename_args)
    os.rmdir(top_dir)

Test run

Here's an example of how the script is supposed to work. Everything extracted to current working directory, but source file can be in differet directory altogether. The test is performed on the zip archive of my personal github repository.

$ ls                                                                                   
flatten_zip.py*  master.zip
$ ./flatten_zip.py master.zip                                                          
['utc-time-indicator-master/.gitignore', '.gitignore']
['utc-time-indicator-master/LICENSE', 'LICENSE']
['utc-time-indicator-master/utc-time-indicator', 'utc-time-indicator']
['utc-time-indicator-master/utc_indicator.png', 'utc_indicator.png']
$ ls
flatten_zip.py*  LICENSE  master.zip  utc_indicator.png  utc-time-indicator

Test with source file being in different location

$ mkdir test_unzip
$ cd test_unzip
$ ../flatten_zip.py  ../master.zip                                                     
['utc-time-indicator-master/.gitignore', '.gitignore']
['utc-time-indicator-master/LICENSE', 'LICENSE']
['utc-time-indicator-master/utc-time-indicator', 'utc-time-indicator']
['utc-time-indicator-master/utc_indicator.png', 'utc_indicator.png']
$ ls
LICENSE  utc_indicator.png  utc-time-indicator

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.