1717
1818READ , WRITE = 1 , 2
1919
20- def open (filename , mode = "rb" , compresslevel = 9 ,
20+ _COMPRESS_LEVEL_FAST = 1
21+ _COMPRESS_LEVEL_TRADEOFF = 6
22+ _COMPRESS_LEVEL_BEST = 9
23+
24+
25+ def open (filename , mode = "rb" , compresslevel = _COMPRESS_LEVEL_BEST ,
2126 encoding = None , errors = None , newline = None ):
2227 """Open a gzip-compressed file in binary or text mode.
2328
@@ -121,7 +126,7 @@ class GzipFile(_compression.BaseStream):
121126 myfileobj = None
122127
123128 def __init__ (self , filename = None , mode = None ,
124- compresslevel = 9 , fileobj = None , mtime = None ):
129+ compresslevel = _COMPRESS_LEVEL_BEST , fileobj = None , mtime = None ):
125130 """Constructor for the GzipFile class.
126131
127132 At least one of fileobj and filename must be given a
@@ -515,7 +520,7 @@ def _rewind(self):
515520 super ()._rewind ()
516521 self ._new_member = True
517522
518- def compress (data , compresslevel = 9 ):
523+ def compress (data , compresslevel = _COMPRESS_LEVEL_BEST ):
519524 """Compress data in one shot and return the compressed string.
520525 Optional argument is the compression level, in range of 0-9.
521526 """
@@ -537,10 +542,21 @@ def main():
537542 parser = ArgumentParser (description =
538543 "A simple command line interface for the gzip module: act like gzip, "
539544 "but do not delete the input file." )
540- parser .add_argument ("-d" , "--decompress" , action = "store_true" ,
545+ group = parser .add_mutually_exclusive_group ()
546+ group .add_argument ('--fast' , action = 'store_true' , help = 'compress faster' )
547+ group .add_argument ('--best' , action = 'store_true' , help = 'compress better' )
548+ group .add_argument ("-d" , "--decompress" , action = "store_true" ,
541549 help = "act like gunzip instead of gzip" )
550+
542551 parser .add_argument ("args" , nargs = "*" , default = ["-" ], metavar = 'file' )
543552 args = parser .parse_args ()
553+
554+ compresslevel = _COMPRESS_LEVEL_TRADEOFF
555+ if args .fast :
556+ compresslevel = _COMPRESS_LEVEL_FAST
557+ elif args .best :
558+ compresslevel = _COMPRESS_LEVEL_BEST
559+
544560 for arg in args .args :
545561 if args .decompress :
546562 if arg == "-" :
@@ -555,7 +571,8 @@ def main():
555571 else :
556572 if arg == "-" :
557573 f = sys .stdin .buffer
558- g = GzipFile (filename = "" , mode = "wb" , fileobj = sys .stdout .buffer )
574+ g = GzipFile (filename = "" , mode = "wb" , fileobj = sys .stdout .buffer ,
575+ compresslevel = compresslevel )
559576 else :
560577 f = builtins .open (arg , "rb" )
561578 g = open (arg + ".gz" , "wb" )
0 commit comments