本文整理汇总了Python中zipfile.ZipFile.extractfile方法的典型用法代码示例。如果您正苦于以下问题:Python ZipFile.extractfile方法的具体用法?Python ZipFile.extractfile怎么用?Python ZipFile.extractfile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类zipfile.ZipFile
的用法示例。
在下文中一共展示了ZipFile.extractfile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dependencies_check
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extractfile [as 别名]
def dependencies_check(self, archive):
""" Check package dependencies
look for 'package_name'.egg-info/requires.txt
or
requirements.txt
"""
if archive.endswith(".zip"):
arch = ZipFile("repo/{}".format(archive)).namelist()
else:
arch = tarfile.open("repo/{}".format(archive))
unpack = self.__unpack(arch)
# Check if there is no dependencies. If none, return None
if unpack is None:
return
# Dependencies list
deps = []
if not archive.endswith(".zip"):
a_file = arch.extractfile(member=unpack)
else:
zip_file = ZipFile("repo/{}".format(archive))
# Remove [python>= version] version checks
a_file = zip_file.read(unpack)
a_file = re.sub(r'\[.*\]', '', a_file.decode('ascii')).split()
for dep in a_file:
if hasattr(dep, 'decode'):
dep = dep.decode('ascii')
if re.match(r"^\w", dep):
deps.append(dep)
download = DownloadPackage(dep, quiet=True, dependencies=True)
dep = download()
if dep is not None:
deps.extend(dep)
return deps
示例2: handle_file
# 需要导入模块: from zipfile import ZipFile [as 别名]
# 或者: from zipfile.ZipFile import extractfile [as 别名]
def handle_file(file_path, module_filter, only_detect, is_temp_file=False):
# todo modular archive handling
# todo PE overlay extraction
# todo PE resources extraction
# todo Installer extraction
if is_zipfile(file_path):
# extract each file and handle it
# todo consider adding archive password support
try:
z = ZipFile(file_path)
for n in z.namelist():
data = z.read(n)
new_path = write_file_to_temp_file(data)
for p, r in handle_file(new_path, module_filter, only_detect, is_temp_file=True):
result_path = ""
if is_temp_file:
result_path = n
else:
result_path = file_path + "," + n
if p is not None:
result_path += "," + p
yield result_path, r
remove(new_path)
except KeyboardInterrupt:
raise
except:
pass
elif tarfile.is_tarfile(file_path):
try:
with tarfile.open(file_path, 'r') as z:
for member in z.getmembers():
try:
data = z.extractfile(member).read()
n = member.name
new_path = write_file_to_temp_file(data)
for p, r in handle_file(new_path, module_filter, only_detect, is_temp_file=True):
result_path = ""
if is_temp_file:
result_path = n
else:
result_path = file_path + "," + n
if p is not None:
result_path += "," + p
yield result_path, r
remove(new_path)
except KeyboardInterrupt:
raise
except:
pass
except KeyboardInterrupt:
raise
except:
pass
elif is_rarfile(file_path):
try:
z = RarFile(file_path)
for n in z.namelist():
data = z.read(n)
new_path = write_file_to_temp_file(data)
for p, r in handle_file(new_path, module_filter, only_detect, is_temp_file=True):
result_path = ""
if is_temp_file:
result_path = n
else:
result_path = file_path + "," + n
if p is not None:
result_path += "," + p
yield result_path, r
remove(new_path)
except KeyboardInterrupt:
raise
except:
pass
else:
# assume we are dealing with a normal file
# todo Convert file handling to use file paths
if getsize(file_path) < 1024 * 1024 * 1024:
with open(file_path, mode='rb') as file_handle:
file_content = file_handle.read()
r = scan_file_data(file_content, module_filter, only_detect)
if r is not None:
if is_temp_file:
yield None, r
else:
yield file_path, r