问题
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.
回答1:
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)
回答2:
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
来源:https://stackoverflow.com/questions/31984387/command-line-for-7z-to-extract-specific-files-from-specific-folders-inside-an-ar