当前位置: 首页>>代码示例>>Python>>正文


Python ZipFile.extractfile方法代码示例

本文整理汇总了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
开发者ID:pasternak,项目名称:pouch,代码行数:37,代码来源:download.py

示例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
开发者ID:Cyber-Forensic,项目名称:bamfdetect,代码行数:87,代码来源:__init__.py


注:本文中的zipfile.ZipFile.extractfile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。