blob: 9e198c75d500d26a5f0f2cfbba4010325b55db20 [file] [log] [blame]
Guido van Rossum4b8c6ea2000-02-04 15:39:30 +00001"""Functions that read and write gzipped files.
2
Guido van Rossum54f22ed2000-02-04 15:10:34 +00003The user of the file doesn't have to worry about the compression,
4but random access is not allowed."""
5
6# based on Andrew Kuchling's minigzip.py distributed with the zlib module
7
Eric S. Raymondee5e61d2001-02-09 09:10:35 +00008import struct, sys, time
Guido van Rossum15262191997-04-30 16:04:57 +00009import zlib
Guido van Rossum68de3791997-07-19 20:22:23 +000010import __builtin__
Guido van Rossum15262191997-04-30 16:04:57 +000011
Skip Montanaro2dd42762001-01-23 15:35:05 +000012__all__ = ["GzipFile","open"]
13
Guido van Rossum15262191997-04-30 16:04:57 +000014FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
15
16READ, WRITE = 1, 2
17
18def write32(output, value):
Jeremy Hyltonc19f9971999-03-23 23:05:34 +000019 output.write(struct.pack("<l", value))
Tim Peters07e99cb2001-01-14 23:47:14 +000020
Guido van Rossum95bdd0b1999-04-12 14:34:16 +000021def write32u(output, value):
Jack Jansen2d0589b2001-02-21 10:39:35 +000022 if value < 0:
23 value = value + 0x100000000L
Guido van Rossum95bdd0b1999-04-12 14:34:16 +000024 output.write(struct.pack("<L", value))
25
Guido van Rossum15262191997-04-30 16:04:57 +000026def read32(input):
Jeremy Hyltonc19f9971999-03-23 23:05:34 +000027 return struct.unpack("<l", input.read(4))[0]
Guido van Rossum15262191997-04-30 16:04:57 +000028
Fred Drakefa1591c1999-04-05 18:37:59 +000029def open(filename, mode="rb", compresslevel=9):
Guido van Rossum15262191997-04-30 16:04:57 +000030 return GzipFile(filename, mode, compresslevel)
31
32class GzipFile:
33
Guido van Rossum68de3791997-07-19 20:22:23 +000034 myfileobj = None
35
Tim Peters07e99cb2001-01-14 23:47:14 +000036 def __init__(self, filename=None, mode=None,
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000037 compresslevel=9, fileobj=None):
Skip Montanaro12424bc2002-05-23 01:43:05 +000038 # guarantee the file is opened in binary mode on platforms
39 # that care about that sort of thing
40 if mode and 'b' not in mode:
41 mode += 'b'
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000042 if fileobj is None:
Fred Drake9bb76d11999-04-05 18:33:40 +000043 fileobj = self.myfileobj = __builtin__.open(filename, mode or 'rb')
Guido van Rossum68de3791997-07-19 20:22:23 +000044 if filename is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000045 if hasattr(fileobj, 'name'): filename = fileobj.name
46 else: filename = ''
Guido van Rossum68de3791997-07-19 20:22:23 +000047 if mode is None:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000048 if hasattr(fileobj, 'mode'): mode = fileobj.mode
Fred Drake9bb76d11999-04-05 18:33:40 +000049 else: mode = 'rb'
Guido van Rossum68de3791997-07-19 20:22:23 +000050
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000051 if mode[0:1] == 'r':
52 self.mode = READ
Tim Peters07e99cb2001-01-14 23:47:14 +000053 # Set flag indicating start of a new member
Guido van Rossum8ca162f2002-04-07 06:36:23 +000054 self._new_member = True
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +000055 self.extrabuf = ""
56 self.extrasize = 0
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000057 self.filename = filename
Guido van Rossum15262191997-04-30 16:04:57 +000058
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +000059 elif mode[0:1] == 'w' or mode[0:1] == 'a':
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000060 self.mode = WRITE
61 self._init_write(filename)
62 self.compress = zlib.compressobj(compresslevel,
Tim Peters07e99cb2001-01-14 23:47:14 +000063 zlib.DEFLATED,
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000064 -zlib.MAX_WBITS,
65 zlib.DEF_MEM_LEVEL,
66 0)
67 else:
Martin v. Löwisdb044892002-03-11 06:46:52 +000068 raise IOError, "Mode " + mode + " not supported"
Guido van Rossum15262191997-04-30 16:04:57 +000069
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000070 self.fileobj = fileobj
Martin v. Löwis8cc965c2001-08-09 07:21:56 +000071 self.offset = 0
Guido van Rossum15262191997-04-30 16:04:57 +000072
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000073 if self.mode == WRITE:
74 self._write_gzip_header()
Guido van Rossum15262191997-04-30 16:04:57 +000075
76 def __repr__(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000077 s = repr(self.fileobj)
78 return '<gzip ' + s[1:-1] + ' ' + hex(id(self)) + '>'
Guido van Rossum15262191997-04-30 16:04:57 +000079
80 def _init_write(self, filename):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000081 if filename[-3:] != '.gz':
82 filename = filename + '.gz'
83 self.filename = filename
84 self.crc = zlib.crc32("")
85 self.size = 0
86 self.writebuf = []
87 self.bufsize = 0
Guido van Rossum15262191997-04-30 16:04:57 +000088
89 def _write_gzip_header(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000090 self.fileobj.write('\037\213') # magic header
91 self.fileobj.write('\010') # compression method
92 fname = self.filename[:-3]
93 flags = 0
94 if fname:
95 flags = FNAME
96 self.fileobj.write(chr(flags))
Guido van Rossum95bdd0b1999-04-12 14:34:16 +000097 write32u(self.fileobj, long(time.time()))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +000098 self.fileobj.write('\002')
99 self.fileobj.write('\377')
100 if fname:
101 self.fileobj.write(fname + '\000')
Guido van Rossum15262191997-04-30 16:04:57 +0000102
103 def _init_read(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000104 self.crc = zlib.crc32("")
105 self.size = 0
Guido van Rossum15262191997-04-30 16:04:57 +0000106
107 def _read_gzip_header(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000108 magic = self.fileobj.read(2)
109 if magic != '\037\213':
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000110 raise IOError, 'Not a gzipped file'
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000111 method = ord( self.fileobj.read(1) )
112 if method != 8:
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000113 raise IOError, 'Unknown compression method'
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000114 flag = ord( self.fileobj.read(1) )
115 # modtime = self.fileobj.read(4)
116 # extraflag = self.fileobj.read(1)
117 # os = self.fileobj.read(1)
118 self.fileobj.read(6)
Guido van Rossum15262191997-04-30 16:04:57 +0000119
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000120 if flag & FEXTRA:
121 # Read & discard the extra field, if present
Tim Peters07e99cb2001-01-14 23:47:14 +0000122 xlen=ord(self.fileobj.read(1))
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000123 xlen=xlen+256*ord(self.fileobj.read(1))
124 self.fileobj.read(xlen)
125 if flag & FNAME:
126 # Read and discard a null-terminated string containing the filename
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000127 while True:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000128 s=self.fileobj.read(1)
129 if not s or s=='\000': break
130 if flag & FCOMMENT:
131 # Read and discard a null-terminated string containing a comment
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000132 while True:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000133 s=self.fileobj.read(1)
134 if not s or s=='\000': break
135 if flag & FHCRC:
136 self.fileobj.read(2) # Read & discard the 16-bit header CRC
Guido van Rossum15262191997-04-30 16:04:57 +0000137
138
139 def write(self,data):
Martin v. Löwisdb044892002-03-11 06:46:52 +0000140 if self.mode != WRITE:
141 import errno
142 raise IOError(errno.EBADF, "write() on read-only GzipFile object")
Tim Peters863ac442002-04-16 01:38:40 +0000143
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000144 if self.fileobj is None:
145 raise ValueError, "write() on closed GzipFile object"
146 if len(data) > 0:
147 self.size = self.size + len(data)
148 self.crc = zlib.crc32(data, self.crc)
149 self.fileobj.write( self.compress.compress(data) )
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000150 self.offset += len(data)
Guido van Rossum15262191997-04-30 16:04:57 +0000151
Guido van Rossum56068012000-02-02 16:51:06 +0000152 def read(self, size=-1):
Martin v. Löwisdb044892002-03-11 06:46:52 +0000153 if self.mode != READ:
154 import errno
155 raise IOError(errno.EBADF, "write() on read-only GzipFile object")
Tim Peters863ac442002-04-16 01:38:40 +0000156
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000157 if self.extrasize <= 0 and self.fileobj is None:
158 return ''
Guido van Rossumb16a3b81998-01-27 19:29:45 +0000159
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000160 readsize = 1024
Guido van Rossum56068012000-02-02 16:51:06 +0000161 if size < 0: # get the whole thing
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000162 try:
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000163 while True:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000164 self._read(readsize)
165 readsize = readsize * 2
166 except EOFError:
167 size = self.extrasize
168 else: # just get some more of it
169 try:
170 while size > self.extrasize:
171 self._read(readsize)
172 readsize = readsize * 2
173 except EOFError:
Guido van Rossum84c6fc91998-08-03 15:41:39 +0000174 if size > self.extrasize:
175 size = self.extrasize
Tim Peters07e99cb2001-01-14 23:47:14 +0000176
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000177 chunk = self.extrabuf[:size]
178 self.extrabuf = self.extrabuf[size:]
179 self.extrasize = self.extrasize - size
Guido van Rossum15262191997-04-30 16:04:57 +0000180
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000181 self.offset += size
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000182 return chunk
Guido van Rossum15262191997-04-30 16:04:57 +0000183
Guido van Rossumb16a3b81998-01-27 19:29:45 +0000184 def _unread(self, buf):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000185 self.extrabuf = buf + self.extrabuf
Guido van Rossum84c6fc91998-08-03 15:41:39 +0000186 self.extrasize = len(buf) + self.extrasize
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000187 self.offset -= len(buf)
Guido van Rossumb16a3b81998-01-27 19:29:45 +0000188
189 def _read(self, size=1024):
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000190 if self.fileobj is None: raise EOFError, "Reached EOF"
Tim Peters07e99cb2001-01-14 23:47:14 +0000191
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000192 if self._new_member:
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000193 # If the _new_member flag is set, we have to
194 # jump to the next member, if there is one.
Tim Peters07e99cb2001-01-14 23:47:14 +0000195 #
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000196 # First, check if we're at the end of the file;
197 # if so, it's time to stop; no more members to read.
198 pos = self.fileobj.tell() # Save current position
199 self.fileobj.seek(0, 2) # Seek to end of file
200 if pos == self.fileobj.tell():
Andrew M. Kuchling2d813e51999-09-06 16:34:51 +0000201 raise EOFError, "Reached EOF"
Tim Peters07e99cb2001-01-14 23:47:14 +0000202 else:
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000203 self.fileobj.seek( pos ) # Return to original position
Tim Peters07e99cb2001-01-14 23:47:14 +0000204
205 self._init_read()
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000206 self._read_gzip_header()
207 self.decompress = zlib.decompressobj(-zlib.MAX_WBITS)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000208 self._new_member = False
Tim Peters07e99cb2001-01-14 23:47:14 +0000209
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000210 # Read a chunk of data from the file
211 buf = self.fileobj.read(size)
Tim Peters07e99cb2001-01-14 23:47:14 +0000212
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000213 # If the EOF has been reached, flush the decompression object
214 # and mark this object as finished.
Tim Peters07e99cb2001-01-14 23:47:14 +0000215
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000216 if buf == "":
217 uncompress = self.decompress.flush()
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000218 self._read_eof()
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000219 self._add_read_data( uncompress )
220 raise EOFError, 'Reached EOF'
Tim Peters07e99cb2001-01-14 23:47:14 +0000221
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000222 uncompress = self.decompress.decompress(buf)
223 self._add_read_data( uncompress )
224
225 if self.decompress.unused_data != "":
226 # Ending case: we've come to the end of a member in the file,
227 # so seek back to the start of the unused data, finish up
228 # this member, and read a new gzip header.
229 # (The number of bytes to seek back is the length of the unused
230 # data, minus 8 because _read_eof() will rewind a further 8 bytes)
231 self.fileobj.seek( -len(self.decompress.unused_data)+8, 1)
232
233 # Check the CRC and file size, and set the flag so we read
Tim Peters07e99cb2001-01-14 23:47:14 +0000234 # a new member on the next call
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000235 self._read_eof()
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000236 self._new_member = True
Tim Peters07e99cb2001-01-14 23:47:14 +0000237
238 def _add_read_data(self, data):
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000239 self.crc = zlib.crc32(data, self.crc)
240 self.extrabuf = self.extrabuf + data
241 self.extrasize = self.extrasize + len(data)
242 self.size = self.size + len(data)
Guido van Rossum15262191997-04-30 16:04:57 +0000243
244 def _read_eof(self):
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000245 # We've read to the end of the file, so we have to rewind in order
Tim Peters07e99cb2001-01-14 23:47:14 +0000246 # to reread the 8 bytes containing the CRC and the file size.
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000247 # We check the that the computed CRC and size of the
248 # uncompressed data matches the stored values.
249 self.fileobj.seek(-8, 1)
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000250 crc32 = read32(self.fileobj)
251 isize = read32(self.fileobj)
Guido van Rossum95bdd0b1999-04-12 14:34:16 +0000252 if crc32%0x100000000L != self.crc%0x100000000L:
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000253 raise ValueError, "CRC check failed"
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000254 elif isize != self.size:
Andrew M. Kuchlingf4f119c1999-03-25 21:49:14 +0000255 raise ValueError, "Incorrect length of data produced"
Tim Peters07e99cb2001-01-14 23:47:14 +0000256
Guido van Rossum15262191997-04-30 16:04:57 +0000257 def close(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000258 if self.mode == WRITE:
259 self.fileobj.write(self.compress.flush())
260 write32(self.fileobj, self.crc)
261 write32(self.fileobj, self.size)
262 self.fileobj = None
263 elif self.mode == READ:
264 self.fileobj = None
265 if self.myfileobj:
266 self.myfileobj.close()
267 self.myfileobj = None
Guido van Rossum15262191997-04-30 16:04:57 +0000268
Andrew M. Kuchling916fcc31999-08-10 13:19:30 +0000269 def __del__(self):
Jeremy Hyltone298c302000-05-08 16:59:59 +0000270 try:
271 if (self.myfileobj is None and
272 self.fileobj is None):
273 return
274 except AttributeError:
275 return
276 self.close()
Tim Peters07e99cb2001-01-14 23:47:14 +0000277
Guido van Rossum15262191997-04-30 16:04:57 +0000278 def flush(self):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000279 self.fileobj.flush()
Guido van Rossum15262191997-04-30 16:04:57 +0000280
Guido van Rossum15262191997-04-30 16:04:57 +0000281 def isatty(self):
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000282 return False
Guido van Rossum15262191997-04-30 16:04:57 +0000283
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000284 def tell(self):
285 return self.offset
286
287 def rewind(self):
288 '''Return the uncompressed stream file position indicator to the
Tim Petersab9ba272001-08-09 21:40:30 +0000289 beginning of the file'''
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000290 if self.mode != READ:
291 raise IOError("Can't rewind in write mode")
292 self.fileobj.seek(0)
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000293 self._new_member = True
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000294 self.extrabuf = ""
295 self.extrasize = 0
296 self.offset = 0
297
298 def seek(self, offset):
299 if self.mode == WRITE:
300 if offset < self.offset:
301 raise IOError('Negative seek in write mode')
302 count = offset - self.offset
Tim Petersab9ba272001-08-09 21:40:30 +0000303 for i in range(count/1024):
Fred Drake95b0eb72001-10-13 18:33:51 +0000304 self.write(1024*'\0')
Martin v. Löwis8cc965c2001-08-09 07:21:56 +0000305 self.write((count%1024)*'\0')
306 elif self.mode == READ:
307 if offset < self.offset:
308 # for negative seek, rewind and do positive seek
309 self.rewind()
310 count = offset - self.offset
311 for i in range(count/1024): self.read(1024)
312 self.read(count % 1024)
313
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000314 def readline(self, size=-1):
315 if size < 0: size = sys.maxint
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000316 bufs = []
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000317 readsize = min(100, size) # Read from the file in small chunks
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000318 while True:
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000319 if size == 0:
Eric S. Raymondee5e61d2001-02-09 09:10:35 +0000320 return "".join(bufs) # Return resulting line
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000321
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000322 c = self.read(readsize)
Eric S. Raymondee5e61d2001-02-09 09:10:35 +0000323 i = c.find('\n')
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000324 if size is not None:
325 # We set i=size to break out of the loop under two
Tim Peters07e99cb2001-01-14 23:47:14 +0000326 # conditions: 1) there's no newline, and the chunk is
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000327 # larger than size, or 2) there is a newline, but the
328 # resulting line would be longer than 'size'.
329 if i==-1 and len(c) > size: i=size-1
330 elif size <= i: i = size -1
Guido van Rossum15262191997-04-30 16:04:57 +0000331
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000332 if i >= 0 or c == '':
333 bufs.append(c[:i+1]) # Add portion of last chunk
334 self._unread(c[i+1:]) # Push back rest of chunk
Eric S. Raymondee5e61d2001-02-09 09:10:35 +0000335 return ''.join(bufs) # Return resulting line
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000336
337 # Append chunk to list, decrease 'size',
338 bufs.append(c)
339 size = size - len(c)
340 readsize = min(size, readsize * 2)
Tim Peters07e99cb2001-01-14 23:47:14 +0000341
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000342 def readlines(self, sizehint=0):
343 # Negative numbers result in reading all the lines
344 if sizehint <= 0: sizehint = sys.maxint
345 L = []
346 while sizehint > 0:
347 line = self.readline()
348 if line == "": break
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000349 L.append(line)
Andrew M. Kuchling41616ee2000-07-29 20:15:26 +0000350 sizehint = sizehint - len(line)
351
352 return L
Guido van Rossum15262191997-04-30 16:04:57 +0000353
Guido van Rossum68de3791997-07-19 20:22:23 +0000354 def writelines(self, L):
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000355 for line in L:
356 self.write(line)
Guido van Rossum51ca6e31997-12-30 20:09:08 +0000357
Neil Schemenauercacbdf62002-03-20 18:36:00 +0000358 def __iter__(self):
359 return self
360
361 def next(self):
362 line = self.readline()
363 if line:
364 return line
365 else:
366 raise StopIteration
367
Guido van Rossum51ca6e31997-12-30 20:09:08 +0000368
369def _test():
370 # Act like gzip; with -d, act like gunzip.
371 # The input file is not deleted, however, nor are any other gzip
372 # options or features supported.
Guido van Rossum51ca6e31997-12-30 20:09:08 +0000373 args = sys.argv[1:]
374 decompress = args and args[0] == "-d"
375 if decompress:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000376 args = args[1:]
Guido van Rossum51ca6e31997-12-30 20:09:08 +0000377 if not args:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000378 args = ["-"]
Guido van Rossum51ca6e31997-12-30 20:09:08 +0000379 for arg in args:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000380 if decompress:
381 if arg == "-":
382 f = GzipFile(filename="", mode="rb", fileobj=sys.stdin)
383 g = sys.stdout
384 else:
385 if arg[-3:] != ".gz":
386 print "filename doesn't end in .gz:", `arg`
387 continue
388 f = open(arg, "rb")
389 g = __builtin__.open(arg[:-3], "wb")
390 else:
391 if arg == "-":
392 f = sys.stdin
393 g = GzipFile(filename="", mode="wb", fileobj=sys.stdout)
394 else:
395 f = __builtin__.open(arg, "rb")
396 g = open(arg + ".gz", "wb")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000397 while True:
Guido van Rossum45e2fbc1998-03-26 21:13:24 +0000398 chunk = f.read(1024)
399 if not chunk:
400 break
401 g.write(chunk)
402 if g is not sys.stdout:
403 g.close()
404 if f is not sys.stdin:
405 f.close()
Guido van Rossum51ca6e31997-12-30 20:09:08 +0000406
407if __name__ == '__main__':
408 _test()