Python: Creating a streaming gzip'd file-like?

前端 未结 5 1515
面向向阳花
面向向阳花 2020-12-24 07:06

I\'m trying to figure out the best way to compress a stream with Python\'s zlib.

I\'ve got a file-like input stream (input, below) and an o

5条回答
  •  Happy的楠姐
    2020-12-24 07:59

    The gzip module supports compressing to a file-like object, pass a fileobj parameter to GzipFile, as well as a filename. The filename you pass in doesn't need to exist, but the gzip header has a filename field which needs to be filled out.

    Update

    This answer does not work. Example:

    # tmp/try-gzip.py 
    import sys
    import gzip
    
    fd=gzip.GzipFile(fileobj=sys.stdin)
    sys.stdout.write(fd.read())
    

    output:

    ===> cat .bash_history  | python tmp/try-gzip.py  > tmp/history.gzip
    Traceback (most recent call last):
      File "tmp/try-gzip.py", line 7, in 
        sys.stdout.write(fd.read())
      File "/usr/lib/python2.7/gzip.py", line 254, in read
        self._read(readsize)
      File "/usr/lib/python2.7/gzip.py", line 288, in _read
        pos = self.fileobj.tell()   # Save current position
    IOError: [Errno 29] Illegal seek
    

提交回复
热议问题