Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 51ca6e3

Browse files
committed
When there's no filename, don't make one up.
Added _test() that behaves (a bit) like gzip. Fix a comment (*sequential* access is okay -- *random* access it out!)
1 parent ccd25d1 commit 51ca6e3

1 file changed

Lines changed: 54 additions & 6 deletions

File tree

Lib/gzip.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
# implements a python function that reads and writes a gzipped file
88
# the user of the file doesn't have to worry about the compression,
9-
# but sequential access is not allowed
9+
# but random access is not allowed
1010

1111
# based on Andrew Kuchling's minigzip.py distributed with the zlib module
1212

@@ -52,7 +52,7 @@ def __init__(self, filename=None, mode=None,
5252
fileobj = self.myfileobj = __builtin__.open(filename, mode or 'r')
5353
if filename is None:
5454
if hasattr(fileobj, 'name'): filename = fileobj.name
55-
else: filename = 'GzippedFile'
55+
else: filename = ''
5656
if mode is None:
5757
if hasattr(fileobj, 'mode'): mode = fileobj.mode
5858
else: mode = 'r'
@@ -98,11 +98,16 @@ def _init_write(self, filename):
9898
def _write_gzip_header(self):
9999
self.fileobj.write('\037\213') # magic header
100100
self.fileobj.write('\010') # compression method
101-
self.fileobj.write(chr(FNAME))
101+
fname = self.filename[:-3]
102+
flags = 0
103+
if fname:
104+
flags = FNAME
105+
self.fileobj.write(chr(flags))
102106
write32(self.fileobj, int(time.time()))
103107
self.fileobj.write('\002')
104108
self.fileobj.write('\377')
105-
self.fileobj.write(self.filename[:-3] + '\000')
109+
if fname:
110+
self.fileobj.write(fname + '\000')
106111

107112
def _init_read(self):
108113
self.crc = zlib.crc32("")
@@ -132,12 +137,12 @@ def _read_gzip_header(self):
132137
# Read and discard a null-terminated string containing the filename
133138
while (1):
134139
s=self.fileobj.read(1)
135-
if s=='\000': break
140+
if not s or s=='\000': break
136141
if flag & FCOMMENT:
137142
# Read and discard a null-terminated string containing a comment
138143
while (1):
139144
s=self.fileobj.read(1)
140-
if s=='\000': break
145+
if not s or s=='\000': break
141146
if flag & FHCRC:
142147
self.fileobj.read(2) # Read & discard the 16-bit header CRC
143148

@@ -251,3 +256,46 @@ def readlines(self):
251256
def writelines(self, L):
252257
for line in L:
253258
self.write(line)
259+
260+
261+
def _test():
262+
# Act like gzip; with -d, act like gunzip.
263+
# The input file is not deleted, however, nor are any other gzip
264+
# options or features supported.
265+
import sys
266+
args = sys.argv[1:]
267+
decompress = args and args[0] == "-d"
268+
if decompress:
269+
args = args[1:]
270+
if not args:
271+
args = ["-"]
272+
for arg in args:
273+
if decompress:
274+
if arg == "-":
275+
f = GzipFile(filename="", mode="rb", fileobj=sys.stdin)
276+
g = sys.stdout
277+
else:
278+
if arg[-3:] != ".gz":
279+
print "filename doesn't end in .gz:", `arg`
280+
continue
281+
f = open(arg, "rb")
282+
g = __builtin__.open(arg[:-3], "wb")
283+
else:
284+
if arg == "-":
285+
f = sys.stdin
286+
g = GzipFile(filename="", mode="wb", fileobj=sys.stdout)
287+
else:
288+
f = __builtin__.open(arg, "rb")
289+
g = open(arg + ".gz", "wb")
290+
while 1:
291+
chunk = f.read(1024)
292+
if not chunk:
293+
break
294+
g.write(chunk)
295+
if g is not sys.stdout:
296+
g.close()
297+
if f is not sys.stdin:
298+
f.close()
299+
300+
if __name__ == '__main__':
301+
_test()

0 commit comments

Comments
 (0)