Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line for 7z to extract specific files from specific folders inside an archive

I'm writing a Python script where I need to call 7z to extract some files kept inside a directory in an archive without extracting the complete archive. The archive contains multiple directories and I need to extract some files from a particular directory.

Let's say, "abc.7z" is an archive which contains multiple directories, i.e "temp", "windows", "system", "data". I want to extract some .exe files from directory "system" only because those .exe files might be present in other directories too but I am interested only in the files from "system".

All I need is the 7z command; I'll figure out how to call 7z from the script. Thanks for your help.

like image 640
Anurag Tiwary Avatar asked Aug 13 '15 09:08

Anurag Tiwary


2 Answers

You can specify a file/folder filter at the end of the command line. Check this for more details on 7zip command line commands and options.

7z.exe x [archive.7z] -o[output_dir] [filter]

e.g. 7z.exe x abc.7z -aoa -oC:\Temp system

You can also specify files of a particular type from system folder. For e.g. system\*.exe will extract all .exe files inside the system directory.

-aoa option is for overwrite mode.

To call it from Python you can use subprocess module. Something like:

import subprocess

cmd = []
cmd.append(r'C:\Program Files\7-Zip\7z.exe')
cmd.append('x')
cmd.append(archive)
cmd.append('-aoa')
cmd.append('-o{}'.format(dst_part))
cmd.append(file_folder_filter)
subprocess.call(cmd)
like image 166
sk11 Avatar answered Nov 06 '22 00:11

sk11


Steps to extract a specific directory from 7z zip:

Specific Directory: rootdir/firstson/second

Zip file : test.7z

Command to use :

7z x test.7z rootdir/firstson/second
like image 37
Arun Murthy Avatar answered Nov 06 '22 02:11

Arun Murthy