7z的命令行可从存档内的特定文件夹中提取特定文件 [英] Command line for 7z to extract specific files from specific folders inside an archive
问题描述
我正在编写一个Python脚本,在这里我需要调用7z来提取存档中目录中保存的某些文件,而不提取完整的存档。存档包含多个目录,我需要从特定目录中提取一些文件。
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.
比方说, abc.7z是一个包含多个目录的存档,即 temp, windows, system, data。我只想从系统目录中提取一些.exe文件,因为这些.exe文件也可能存在于其他目录中,但是我只对系统中的文件感兴趣。
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".
我只需要7z命令;我将弄清楚如何从脚本中调用7z。谢谢您的帮助。
All I need is the 7z command; I'll figure out how to call 7z from the script. Thanks for your help.
推荐答案
您可以在命令行末尾指定文件/文件夹过滤器。检查此,以获取有关7zip命令行命令和选项的更多详细信息。
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]
例如 7z.exe x abc.7z -aoa -oC:\临时系统
您还可以指定以下文件系统文件夹中的特定类型。例如 system\ * .exe
将提取 system $内的所有
.exe
文件。 c $ c>目录。
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
选项用于覆盖模式。
要从Python调用它,可以使用 subprocess
模块。
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)
这篇关于7z的命令行可从存档内的特定文件夹中提取特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!