Unzip in Current Directory While Preserving File Structure

unzip in current directory while preserving file structure

unzip myfile.zip extracts files in the working directory by keeping path names from the zip file.

So if you get a subdirectory myfile it means it is part of the relative path of compressed files. Check it by listing the zip content

unzip -l myfile.zip

So you can unzip the file from the directory above, or, from the target directory unzip with -d option, where -d is the directory above

cd myfile
unzip myfile.zip -d ..

Extract zip contents into directory with same name as zip file, retain directory structure

Reading the help of the 7z-command by just typing "C:\Path To\7-Zip\7z.exe" gets the help with all possible arguments. Here we find the following interesting ones:

 e : Extract files from archive (without using directory names)

and

x : eXtract files with full paths

Trial and error shows that the latter is the one fitting your desired behaviour without bigger effort :)

After the comment by @BadmintonCat here is the addition that will create a folder to zip everything into (use as batch script with the file as argument):

@echo off

SET "filename=%~1"
SET dirName=%filename:~0,-4%

7z x -o"%dirName%" "%filename%"

From the help: -o{Directory} : set Output directory. 7z will create the directory if it does not already exist.

Unzipping directory structure with python

The extract and extractall methods are great if you're on Python 2.6. I have to use Python 2.5 for now, so I just need to create the directories if they don't exist. You can get a listing of directories with the namelist() method. The directories will always end with a forward slash (even on Windows) e.g.,

import os, zipfile

z = zipfile.ZipFile('myfile.zip')
for f in z.namelist():
if f.endswith('/'):
os.makedirs(f)

You probably don't want to do it exactly like that (i.e., you'd probably want to extract the contents of the zip file as you iterate over the namelist), but you get the idea.

Unzip All Files In A Directory

This works in bash, according to this link:

unzip \*.zip

Recreate directory structure when extracting from zip file

Here is some solution: How should I extract compressed folders in java?

Create zip file and ignore directory structure

You can use -j.

-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).


Related Topics



Leave a reply



Submit