diff options
author | Georg Brandl <georg@python.org> | 2010-12-30 17:22:33 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2010-12-30 17:22:33 (GMT) |
commit | 4cf83f4d128bd40ebe3b6e59ced4895f554d18de (patch) | |
tree | ccc6e4c3e03a711c45f4badf811314231d646d95 /Demo/zlib | |
parent | d1fc34d563a9fd06a78226b1bb4e56286c70e035 (diff) | |
download | cpython-4cf83f4d128bd40ebe3b6e59ced4895f554d18de.zip cpython-4cf83f4d128bd40ebe3b6e59ced4895f554d18de.tar.gz cpython-4cf83f4d128bd40ebe3b6e59ced4895f554d18de.tar.bz2 |
Remove some of the old demos. (Put a few somewhere else.)
Diffstat (limited to 'Demo/zlib')
-rwxr-xr-x | Demo/zlib/minigzip.py | 134 | ||||
-rwxr-xr-x | Demo/zlib/zlibdemo.py | 47 |
2 files changed, 0 insertions, 181 deletions
diff --git a/Demo/zlib/minigzip.py b/Demo/zlib/minigzip.py deleted file mode 100755 index b57de73..0000000 --- a/Demo/zlib/minigzip.py +++ /dev/null @@ -1,134 +0,0 @@ -#!/usr/bin/env python3 -# Demo program for zlib; it compresses or decompresses files, but *doesn't* -# delete the original. This doesn't support all of gzip's options. -# -# The 'gzip' module in the standard library provides a more complete -# implementation of gzip-format files. - -import zlib, sys, os - -FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16 - -def write32(output, value): - output.write(bytes([value & 255])) ; value=value // 256 - output.write(bytes([value & 255])) ; value=value // 256 - output.write(bytes([value & 255])) ; value=value // 256 - output.write(bytes([value & 255])) - -def read32(input): - v = ord(input.read(1)) - v += (ord(input.read(1)) << 8 ) - v += (ord(input.read(1)) << 16) - v += (ord(input.read(1)) << 24) - return v - -def compress(filename, input, output): - output.write(b'\037\213\010') # Write the header, ... - output.write(bytes([FNAME])) # ... flag byte ... - - statval = os.stat(filename) # ... modification time ... - mtime = statval[8] - write32(output, mtime) - output.write(b'\002') # ... slowest compression alg. ... - output.write(b'\377') # ... OS (=unknown) ... - bfilename = os.fsencode(filename) - output.write(bfilename + b'\000') # ... original filename ... - - crcval = zlib.crc32(b'') - compobj = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS, - zlib.DEF_MEM_LEVEL, 0) - while True: - data = input.read(1024) - if data == b'': - break - crcval = zlib.crc32(data, crcval) - output.write(compobj.compress(data)) - output.write(compobj.flush()) - write32(output, crcval) # ... the CRC ... - write32(output, statval[6]) # and the file size. - -def decompress(input, output): - magic = input.read(2) - if magic != b'\037\213': - print('Not a gzipped file') - sys.exit(0) - if ord(input.read(1)) != 8: - print('Unknown compression method') - sys.exit(0) - flag = ord(input.read(1)) - input.read(4+1+1) # Discard modification time, - # extra flags, and OS byte. - if flag & FEXTRA: - # Read & discard the extra field, if present - xlen = ord(input.read(1)) - xlen += 256*ord(input.read(1)) - input.read(xlen) - if flag & FNAME: - # Read and discard a null-terminated string containing the filename - while True: - s = input.read(1) - if s == b'\0': break - if flag & FCOMMENT: - # Read and discard a null-terminated string containing a comment - while True: - s = input.read(1) - if s == b'\0': break - if flag & FHCRC: - input.read(2) # Read & discard the 16-bit header CRC - - decompobj = zlib.decompressobj(-zlib.MAX_WBITS) - crcval = zlib.crc32(b'') - length = 0 - while True: - data = input.read(1024) - if data == b"": - break - decompdata = decompobj.decompress(data) - output.write(decompdata) - length += len(decompdata) - crcval = zlib.crc32(decompdata, crcval) - - decompdata = decompobj.flush() - output.write(decompdata) - length += len(decompdata) - crcval = zlib.crc32(decompdata, crcval) - - # We've read to the end of the file, so we have to rewind in order - # to reread the 8 bytes containing the CRC and the file size. The - # decompressor is smart and knows when to stop, so feeding it - # extra data is harmless. - input.seek(-8, 2) - crc32 = read32(input) - isize = read32(input) - if crc32 != crcval: - print('CRC check failed.') - if isize != length: - print('Incorrect length of data produced') - -def main(): - if len(sys.argv)!=2: - print('Usage: minigzip.py <filename>') - print(' The file will be compressed or decompressed.') - sys.exit(0) - - filename = sys.argv[1] - if filename.endswith('.gz'): - compressing = False - outputname = filename[:-3] - else: - compressing = True - outputname = filename + '.gz' - - input = open(filename, 'rb') - output = open(outputname, 'wb') - - if compressing: - compress(filename, input, output) - else: - decompress(input, output) - - input.close() - output.close() - -if __name__ == '__main__': - main() diff --git a/Demo/zlib/zlibdemo.py b/Demo/zlib/zlibdemo.py deleted file mode 100755 index 40a36d4..0000000 --- a/Demo/zlib/zlibdemo.py +++ /dev/null @@ -1,47 +0,0 @@ -#!/usr/bin/env python3 - -# Takes an optional filename, defaulting to this file itself. -# Reads the file and compresses the content using level 1 and level 9 -# compression, printing a summary of the results. - -import zlib, sys - -def main(): - if len(sys.argv) > 1: - filename = sys.argv[1] - else: - filename = sys.argv[0] - print('Reading', filename) - - with open(filename, 'rb') as f: # Get the data to compress - s = f.read() - - # First, we'll compress the string in one step - comptext = zlib.compress(s, 1) - decomp = zlib.decompress(comptext) - - print('1-step compression: (level 1)') - print(' Original:', len(s), 'Compressed:', len(comptext), end=' ') - print('Uncompressed:', len(decomp)) - - # Now, let's compress the string in stages; set chunk to work in smaller steps - - chunk = 256 - compressor = zlib.compressobj(9) - decompressor = zlib.decompressobj() - comptext = decomp = b'' - for i in range(0, len(s), chunk): - comptext += compressor.compress(s[i:i+chunk]) - # Don't forget to call flush()!! - comptext += compressor.flush() - - for i in range(0, len(comptext), chunk): - decomp += decompressor.decompress(comptext[i:i+chunk]) - decomp += decompressor.flush() - - print('Progressive compression (level 9):') - print(' Original:', len(s), 'Compressed:', len(comptext), end=' ') - print('Uncompressed:', len(decomp)) - -if __name__ == '__main__': - main() |