简体   繁体   English

Zip文件不可寻找

[英]Zip file not seekable

I'm trying to do some file manipulation on a zipped file. 我正在尝试对压缩文件进行一些文件操作。 I'm a little confused on why the file like object that .open is returning a file like that's not seekable. 我有点困惑为什么文件像对象.open返回一个不可寻找的文件。 Can any one shed some light on this? 任何人都可以对此有所了解吗?

This is the code I'm using. 这是我正在使用的代码。 How can I make file_like's seekable? 我怎样才能使file_like可以搜索?

zipped_archive = ZipFile(filepath, mode='r')
file_like = zipped_archive.open(file_name, mode='r')
file_like.seekable() # returns False

The ZipFile.open method returns a ZipExtFile object which does not implement seeking. ZipFile.open方法返回一个ZipExtFile对象,该对象实现搜索。 The default value for seekable in io.IOBase is False . io.IOBase可搜索的默认值为 False

I'm not sure why seeking was never implemented but I'd guess seeking by byte in a compressed file might be difficult for some reason. 我不确定为什么搜索从未实现但我猜测在压缩文件中按字节搜索可能由于某种原因很难。

在Python 3.7版( 问题 )中修复

A very helpful and simple work around that helped me is to add seekable to the file I used: 一个非常有用和简单的工作帮助我添加可搜索到我使用的文件:

def add_seekable_to_file(f):
    """
    If file f does not has seekable function -
    add seekable function that will always return true
    Args:
        f: the file
    Returns: the file f with seekable function

    """
    if not hasattr(f, "seekable"):
        # AFAICT all the filetypes that STF wraps can seek
        f.seekable = lambda: True

add_seekable_to_file(datazip.fp) add_seekable_to_file(datazip.fp)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM