mirror of
https://github.com/python/cpython.git
synced 2025-10-10 00:43:41 +00:00
bpo-34969: Add --fast, --best on the gzip CLI (GH-9833)
This commit is contained in:
parent
fe62d877e3
commit
3e28eed9ec
4 changed files with 65 additions and 16 deletions
27
Lib/gzip.py
27
Lib/gzip.py
|
@ -17,7 +17,12 @@ FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
|
|||
|
||||
READ, WRITE = 1, 2
|
||||
|
||||
def open(filename, mode="rb", compresslevel=9,
|
||||
_COMPRESS_LEVEL_FAST = 1
|
||||
_COMPRESS_LEVEL_TRADEOFF = 6
|
||||
_COMPRESS_LEVEL_BEST = 9
|
||||
|
||||
|
||||
def open(filename, mode="rb", compresslevel=_COMPRESS_LEVEL_BEST,
|
||||
encoding=None, errors=None, newline=None):
|
||||
"""Open a gzip-compressed file in binary or text mode.
|
||||
|
||||
|
@ -121,7 +126,7 @@ class GzipFile(_compression.BaseStream):
|
|||
myfileobj = None
|
||||
|
||||
def __init__(self, filename=None, mode=None,
|
||||
compresslevel=9, fileobj=None, mtime=None):
|
||||
compresslevel=_COMPRESS_LEVEL_BEST, fileobj=None, mtime=None):
|
||||
"""Constructor for the GzipFile class.
|
||||
|
||||
At least one of fileobj and filename must be given a
|
||||
|
@ -515,7 +520,7 @@ class _GzipReader(_compression.DecompressReader):
|
|||
super()._rewind()
|
||||
self._new_member = True
|
||||
|
||||
def compress(data, compresslevel=9):
|
||||
def compress(data, compresslevel=_COMPRESS_LEVEL_BEST):
|
||||
"""Compress data in one shot and return the compressed string.
|
||||
Optional argument is the compression level, in range of 0-9.
|
||||
"""
|
||||
|
@ -537,10 +542,21 @@ def main():
|
|||
parser = ArgumentParser(description=
|
||||
"A simple command line interface for the gzip module: act like gzip, "
|
||||
"but do not delete the input file.")
|
||||
parser.add_argument("-d", "--decompress", action="store_true",
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group.add_argument('--fast', action='store_true', help='compress faster')
|
||||
group.add_argument('--best', action='store_true', help='compress better')
|
||||
group.add_argument("-d", "--decompress", action="store_true",
|
||||
help="act like gunzip instead of gzip")
|
||||
|
||||
parser.add_argument("args", nargs="*", default=["-"], metavar='file')
|
||||
args = parser.parse_args()
|
||||
|
||||
compresslevel = _COMPRESS_LEVEL_TRADEOFF
|
||||
if args.fast:
|
||||
compresslevel = _COMPRESS_LEVEL_FAST
|
||||
elif args.best:
|
||||
compresslevel = _COMPRESS_LEVEL_BEST
|
||||
|
||||
for arg in args.args:
|
||||
if args.decompress:
|
||||
if arg == "-":
|
||||
|
@ -555,7 +571,8 @@ def main():
|
|||
else:
|
||||
if arg == "-":
|
||||
f = sys.stdin.buffer
|
||||
g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer)
|
||||
g = GzipFile(filename="", mode="wb", fileobj=sys.stdout.buffer,
|
||||
compresslevel=compresslevel)
|
||||
else:
|
||||
f = builtins.open(arg, "rb")
|
||||
g = open(arg + ".gz", "wb")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue