⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 encoding_gzip.py

📁 一个java写的proxy的例子
💻 PY
字号:
# gunzip.py, amitp@cs.stanford.edu, March 2000a## Implements the minimal amount of work needed to ungzip an input stream## Based on gzip.py in the standard Python distribution,# but exports a proxy4 encoding interface:#      decode(string) => return as much of the string as can be decoded#      flush()        => return everything else# TEST CASE:#    http://tv.excite.com/gridimport zlibfrom string import findclass GunzipStream:    # Flags in the gzip header    FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16        def __init__(self):        self.decompressor = zlib.decompressobj(-zlib.MAX_WBITS)        self.buffer = ''        self.header_seen = 0        self.closed = 0    def attempt_header_read(self):        "Try to parse the header from buffer, and if we can, set flag"        if len(self.buffer) < 10: # Incomplete fixed part of header            return ''                magic = self.buffer[:2]        if magic != '\037\213':            raise zlib.error, 'not gzip format'                    method = ord(self.buffer[2])        if method != 8:            raise zlib.error, 'unknown compression method'                flag = ord(self.buffer[3])        # Skip until byte 10        string = self.buffer[10:]                if flag & self.FEXTRA:            # Read & discard the extra field, if present            if len(string) < 2: return '' # Incomplete            xlen=ord(string[0])            xlen=xlen+256*ord(string[1])            if len(string) < 2+xlen: return '' # Incomplete            string = string[2+xlen:]                    if flag & self.FNAME:            # Read and discard a null-terminated string containing the filename            i = find(string, '\000')            if i < 0: return '' # Incomplete            string = string[i+1:]                    if flag & self.FCOMMENT:            # Read and discard a null-terminated string containing a comment            i = find(string, '\000')            if i < 0: return '' # Incomplete            string = string[i+1:]                    if flag & self.FHCRC:            # Read & discard the 16-bit header CRC            if len(string) < 2: return '' # Incomplete            string = string[2:]        # We actually got through the header        self.buffer = string        self.header_seen = 1    def decode(self, string):        if not self.header_seen:            # Try to parse the header            self.buffer = self.buffer + string            string = ''            self.attempt_header_read()            if self.header_seen:                # Put the rest of the buffer back into the string,                # for zlib use                string = self.buffer                self.buffer = ''            else:                # We haven't finished parsing the header                return ''                    # We have seen the header, so we can move on to zlib        return self.decompressor.decompress(string)    def flush(self):        if not self.header_seen:            # We still haven't finished parsing the header .. oh well            return ''        else:            return self.decompressor.flush()    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -