Guido van Rossum | 4b8c6ea | 2000-02-04 15:39:30 +0000 | [diff] [blame] | 1 | """Functions that read and write gzipped files. |
| 2 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 3 | The user of the file doesn't have to worry about the compression, |
| 4 | but random access is not allowed.""" |
| 5 | |
| 6 | # based on Andrew Kuchling's minigzip.py distributed with the zlib module |
| 7 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 8 | import time |
| 9 | import string |
| 10 | import zlib |
Jeremy Hylton | c19f997 | 1999-03-23 23:05:34 +0000 | [diff] [blame] | 11 | import struct |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 12 | import __builtin__ |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 14 | FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 |
| 15 | |
| 16 | READ, WRITE = 1, 2 |
| 17 | |
| 18 | def write32(output, value): |
Jeremy Hylton | c19f997 | 1999-03-23 23:05:34 +0000 | [diff] [blame] | 19 | output.write(struct.pack("<l", value)) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | 95bdd0b | 1999-04-12 14:34:16 +0000 | [diff] [blame] | 21 | def write32u(output, value): |
| 22 | output.write(struct.pack("<L", value)) |
| 23 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 24 | def read32(input): |
Jeremy Hylton | c19f997 | 1999-03-23 23:05:34 +0000 | [diff] [blame] | 25 | return struct.unpack("<l", input.read(4))[0] |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 26 | |
Fred Drake | fa1591c | 1999-04-05 18:37:59 +0000 | [diff] [blame] | 27 | def open(filename, mode="rb", compresslevel=9): |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 28 | return GzipFile(filename, mode, compresslevel) |
| 29 | |
| 30 | class GzipFile: |
| 31 | |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 32 | myfileobj = None |
| 33 | |
| 34 | def __init__(self, filename=None, mode=None, |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 35 | compresslevel=9, fileobj=None): |
| 36 | if fileobj is None: |
Fred Drake | 9bb76d1 | 1999-04-05 18:33:40 +0000 | [diff] [blame] | 37 | fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb') |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 38 | if filename is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 39 | if hasattr(fileobj, 'name'): filename = fileobj.name |
| 40 | else: filename = '' |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 41 | if mode is None: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 42 | if hasattr(fileobj, 'mode'): mode = fileobj.mode |
Fred Drake | 9bb76d1 | 1999-04-05 18:33:40 +0000 | [diff] [blame] | 43 | else: mode = 'rb' |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 45 | if mode[0:1] == 'r': |
| 46 | self.mode = READ |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 47 | # Set flag indicating start of a new member |
| 48 | self._new_member = 1 |
| 49 | self.extrabuf = "" |
| 50 | self.extrasize = 0 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 51 | self.filename = filename |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 52 | |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 53 | elif mode[0:1] == 'w' or mode[0:1] == 'a': |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 54 | self.mode = WRITE |
| 55 | self._init_write(filename) |
| 56 | self.compress = zlib.compressobj(compresslevel, |
| 57 | zlib.DEFLATED, |
| 58 | -zlib.MAX_WBITS, |
| 59 | zlib.DEF_MEM_LEVEL, |
| 60 | 0) |
| 61 | else: |
| 62 | raise ValueError, "Mode " + mode + " not supported" |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 64 | self.fileobj = fileobj |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 65 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 66 | if self.mode == WRITE: |
| 67 | self._write_gzip_header() |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 68 | |
| 69 | def __repr__(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 70 | s = repr(self.fileobj) |
| 71 | return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>' |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 72 | |
| 73 | def _init_write(self, filename): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 74 | if filename[-3:] != '.gz': |
| 75 | filename = filename + '.gz' |
| 76 | self.filename = filename |
| 77 | self.crc = zlib.crc32("") |
| 78 | self.size = 0 |
| 79 | self.writebuf = [] |
| 80 | self.bufsize = 0 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 81 | |
| 82 | def _write_gzip_header(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 83 | self.fileobj.write('\037\213') # magic header |
| 84 | self.fileobj.write('\010') # compression method |
| 85 | fname = self.filename[:-3] |
| 86 | flags = 0 |
| 87 | if fname: |
| 88 | flags = FNAME |
| 89 | self.fileobj.write(chr(flags)) |
Guido van Rossum | 95bdd0b | 1999-04-12 14:34:16 +0000 | [diff] [blame] | 90 | write32u(self.fileobj, long(time.time())) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 91 | self.fileobj.write('\002') |
| 92 | self.fileobj.write('\377') |
| 93 | if fname: |
| 94 | self.fileobj.write(fname + '\000') |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 95 | |
| 96 | def _init_read(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 97 | self.crc = zlib.crc32("") |
| 98 | self.size = 0 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 99 | |
| 100 | def _read_gzip_header(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 101 | magic = self.fileobj.read(2) |
| 102 | if magic != '\037\213': |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 103 | raise IOError, 'Not a gzipped file' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 104 | method = ord( self.fileobj.read(1) ) |
| 105 | if method != 8: |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 106 | raise IOError, 'Unknown compression method' |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 107 | flag = ord( self.fileobj.read(1) ) |
| 108 | # modtime = self.fileobj.read(4) |
| 109 | # extraflag = self.fileobj.read(1) |
| 110 | # os = self.fileobj.read(1) |
| 111 | self.fileobj.read(6) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 112 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 113 | if flag & FEXTRA: |
| 114 | # Read & discard the extra field, if present |
| 115 | xlen=ord(self.fileobj.read(1)) |
| 116 | xlen=xlen+256*ord(self.fileobj.read(1)) |
| 117 | self.fileobj.read(xlen) |
| 118 | if flag & FNAME: |
| 119 | # Read and discard a null-terminated string containing the filename |
| 120 | while (1): |
| 121 | s=self.fileobj.read(1) |
| 122 | if not s or s=='\000': break |
| 123 | if flag & FCOMMENT: |
| 124 | # Read and discard a null-terminated string containing a comment |
| 125 | while (1): |
| 126 | s=self.fileobj.read(1) |
| 127 | if not s or s=='\000': break |
| 128 | if flag & FHCRC: |
| 129 | self.fileobj.read(2) # Read & discard the 16-bit header CRC |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 130 | |
| 131 | |
| 132 | def write(self,data): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 133 | if self.fileobj is None: |
| 134 | raise ValueError, "write() on closed GzipFile object" |
| 135 | if len(data) > 0: |
| 136 | self.size = self.size + len(data) |
| 137 | self.crc = zlib.crc32(data, self.crc) |
| 138 | self.fileobj.write( self.compress.compress(data) ) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 139 | |
| 140 | def writelines(self,lines): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 141 | self.write(string.join(lines)) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 5606801 | 2000-02-02 16:51:06 +0000 | [diff] [blame] | 143 | def read(self, size=-1): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 144 | if self.extrasize <= 0 and self.fileobj is None: |
| 145 | return '' |
Guido van Rossum | b16a3b8 | 1998-01-27 19:29:45 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 147 | readsize = 1024 |
Guido van Rossum | 5606801 | 2000-02-02 16:51:06 +0000 | [diff] [blame] | 148 | if size < 0: # get the whole thing |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 149 | try: |
| 150 | while 1: |
| 151 | self._read(readsize) |
| 152 | readsize = readsize * 2 |
| 153 | except EOFError: |
| 154 | size = self.extrasize |
| 155 | else: # just get some more of it |
| 156 | try: |
| 157 | while size > self.extrasize: |
| 158 | self._read(readsize) |
| 159 | readsize = readsize * 2 |
| 160 | except EOFError: |
Guido van Rossum | 84c6fc9 | 1998-08-03 15:41:39 +0000 | [diff] [blame] | 161 | if size > self.extrasize: |
| 162 | size = self.extrasize |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 163 | |
| 164 | chunk = self.extrabuf[:size] |
| 165 | self.extrabuf = self.extrabuf[size:] |
| 166 | self.extrasize = self.extrasize - size |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 167 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 168 | return chunk |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | b16a3b8 | 1998-01-27 19:29:45 +0000 | [diff] [blame] | 170 | def _unread(self, buf): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 171 | self.extrabuf = buf + self.extrabuf |
Guido van Rossum | 84c6fc9 | 1998-08-03 15:41:39 +0000 | [diff] [blame] | 172 | self.extrasize = len(buf) + self.extrasize |
Guido van Rossum | b16a3b8 | 1998-01-27 19:29:45 +0000 | [diff] [blame] | 173 | |
| 174 | def _read(self, size=1024): |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 175 | if self.fileobj is None: raise EOFError, "Reached EOF" |
| 176 | |
| 177 | if self._new_member: |
| 178 | # If the _new_member flag is set, we have to |
| 179 | # |
| 180 | # First, check if we're at the end of the file; |
| 181 | # if so, it's time to stop; no more members to read. |
| 182 | pos = self.fileobj.tell() # Save current position |
| 183 | self.fileobj.seek(0, 2) # Seek to end of file |
| 184 | if pos == self.fileobj.tell(): |
| 185 | self.fileobj = None |
Andrew M. Kuchling | 2d813e5 | 1999-09-06 16:34:51 +0000 | [diff] [blame] | 186 | raise EOFError, "Reached EOF" |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 187 | else: |
| 188 | self.fileobj.seek( pos ) # Return to original position |
| 189 | |
| 190 | self._init_read() |
| 191 | self._read_gzip_header() |
| 192 | self.decompress = zlib.decompressobj(-zlib.MAX_WBITS) |
| 193 | self._new_member = 0 |
| 194 | |
| 195 | # Read a chunk of data from the file |
| 196 | buf = self.fileobj.read(size) |
| 197 | |
| 198 | # If the EOF has been reached, flush the decompression object |
| 199 | # and mark this object as finished. |
| 200 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 201 | if buf == "": |
| 202 | uncompress = self.decompress.flush() |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 203 | self._read_eof() |
| 204 | self.fileobj = None |
| 205 | self._add_read_data( uncompress ) |
| 206 | raise EOFError, 'Reached EOF' |
| 207 | |
| 208 | uncompress = self.decompress.decompress(buf) |
| 209 | self._add_read_data( uncompress ) |
| 210 | |
| 211 | if self.decompress.unused_data != "": |
| 212 | # Ending case: we've come to the end of a member in the file, |
| 213 | # so seek back to the start of the unused data, finish up |
| 214 | # this member, and read a new gzip header. |
| 215 | # (The number of bytes to seek back is the length of the unused |
| 216 | # data, minus 8 because _read_eof() will rewind a further 8 bytes) |
| 217 | self.fileobj.seek( -len(self.decompress.unused_data)+8, 1) |
| 218 | |
| 219 | # Check the CRC and file size, and set the flag so we read |
| 220 | # a new member on the next call |
| 221 | self._read_eof() |
| 222 | self._new_member = 1 |
| 223 | |
| 224 | def _add_read_data(self, data): |
| 225 | self.crc = zlib.crc32(data, self.crc) |
| 226 | self.extrabuf = self.extrabuf + data |
| 227 | self.extrasize = self.extrasize + len(data) |
| 228 | self.size = self.size + len(data) |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 229 | |
| 230 | def _read_eof(self): |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 231 | # We've read to the end of the file, so we have to rewind in order |
| 232 | # to reread the 8 bytes containing the CRC and the file size. |
| 233 | # We check the that the computed CRC and size of the |
| 234 | # uncompressed data matches the stored values. |
| 235 | self.fileobj.seek(-8, 1) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 236 | crc32 = read32(self.fileobj) |
| 237 | isize = read32(self.fileobj) |
Guido van Rossum | 95bdd0b | 1999-04-12 14:34:16 +0000 | [diff] [blame] | 238 | if crc32%0x100000000L != self.crc%0x100000000L: |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 239 | raise ValueError, "CRC check failed" |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 240 | elif isize != self.size: |
Andrew M. Kuchling | f4f119c | 1999-03-25 21:49:14 +0000 | [diff] [blame] | 241 | raise ValueError, "Incorrect length of data produced" |
| 242 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 243 | def close(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 244 | if self.mode == WRITE: |
| 245 | self.fileobj.write(self.compress.flush()) |
| 246 | write32(self.fileobj, self.crc) |
| 247 | write32(self.fileobj, self.size) |
| 248 | self.fileobj = None |
| 249 | elif self.mode == READ: |
| 250 | self.fileobj = None |
| 251 | if self.myfileobj: |
| 252 | self.myfileobj.close() |
| 253 | self.myfileobj = None |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 254 | |
Andrew M. Kuchling | 916fcc3 | 1999-08-10 13:19:30 +0000 | [diff] [blame] | 255 | def __del__(self): |
| 256 | if (self.myfileobj is not None or |
| 257 | self.fileobj is not None): |
| 258 | self.close() |
| 259 | |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 260 | def flush(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 261 | self.fileobj.flush() |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 262 | |
| 263 | def seek(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 264 | raise IOError, 'Random access not allowed in gzip files' |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 265 | |
| 266 | def tell(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 267 | raise IOError, 'I won\'t tell() you for gzip files' |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 268 | |
| 269 | def isatty(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 270 | return 0 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 271 | |
| 272 | def readline(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 273 | bufs = [] |
| 274 | readsize = 100 |
| 275 | while 1: |
| 276 | c = self.read(readsize) |
| 277 | i = string.find(c, '\n') |
| 278 | if i >= 0 or c == '': |
Jeremy Hylton | ee918cb | 1998-05-13 21:49:58 +0000 | [diff] [blame] | 279 | bufs.append(c[:i+1]) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 280 | self._unread(c[i+1:]) |
| 281 | return string.join(bufs, '') |
| 282 | bufs.append(c) |
| 283 | readsize = readsize * 2 |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 284 | |
Guido van Rossum | 5606801 | 2000-02-02 16:51:06 +0000 | [diff] [blame] | 285 | def readlines(self, ignored=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 286 | buf = self.read() |
Guido van Rossum | 84c6fc9 | 1998-08-03 15:41:39 +0000 | [diff] [blame] | 287 | lines = string.split(buf, '\n') |
| 288 | for i in range(len(lines)-1): |
| 289 | lines[i] = lines[i] + '\n' |
| 290 | if lines and not lines[-1]: |
| 291 | del lines[-1] |
| 292 | return lines |
Guido van Rossum | 1526219 | 1997-04-30 16:04:57 +0000 | [diff] [blame] | 293 | |
Guido van Rossum | 68de379 | 1997-07-19 20:22:23 +0000 | [diff] [blame] | 294 | def writelines(self, L): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 295 | for line in L: |
| 296 | self.write(line) |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 297 | |
| 298 | |
| 299 | def _test(): |
| 300 | # Act like gzip; with -d, act like gunzip. |
| 301 | # The input file is not deleted, however, nor are any other gzip |
| 302 | # options or features supported. |
| 303 | import sys |
| 304 | args = sys.argv[1:] |
| 305 | decompress = args and args[0] == "-d" |
| 306 | if decompress: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 307 | args = args[1:] |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 308 | if not args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 309 | args = ["-"] |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 310 | for arg in args: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 311 | if decompress: |
| 312 | if arg == "-": |
| 313 | f = GzipFile(filename="", mode="rb", fileobj=sys.stdin) |
| 314 | g = sys.stdout |
| 315 | else: |
| 316 | if arg[-3:] != ".gz": |
| 317 | print "filename doesn't end in .gz:", `arg` |
| 318 | continue |
| 319 | f = open(arg, "rb") |
| 320 | g = __builtin__.open(arg[:-3], "wb") |
| 321 | else: |
| 322 | if arg == "-": |
| 323 | f = sys.stdin |
| 324 | g = GzipFile(filename="", mode="wb", fileobj=sys.stdout) |
| 325 | else: |
| 326 | f = __builtin__.open(arg, "rb") |
| 327 | g = open(arg + ".gz", "wb") |
| 328 | while 1: |
| 329 | chunk = f.read(1024) |
| 330 | if not chunk: |
| 331 | break |
| 332 | g.write(chunk) |
| 333 | if g is not sys.stdout: |
| 334 | g.close() |
| 335 | if f is not sys.stdin: |
| 336 | f.close() |
Guido van Rossum | 51ca6e3 | 1997-12-30 20:09:08 +0000 | [diff] [blame] | 337 | |
| 338 | if __name__ == '__main__': |
| 339 | _test() |