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
How to Make Libusb Library Visible to Another Program
Finding The Longest Word in a Text File
Vagrant, Shared Folder: Take Advantage of Inotify Over Nfs
Update .Bashrc from Provisioning Shell Script with Vagrant
See What Process Last Touched a File
Grep -F on Files in a Zipped Folder
How to Detect Availability of Gui in Bash/Shell
Apache 2.4.23 Undefined Reference to Crypto_Malloc_Init
Installing New Version of Python on Debian Linux Server
Better Way to Check Linux Version
Calculator in Assembly Language - Linux X86 & Nasm - Division
Mqtt Socket Error on Client <Unknown>
Where Is Linux Cfs Scheduler Code