+++ /dev/null
-#!/usr/bin/env python\r
-# Demo program for zlib; it compresses or decompresses files, but *doesn't*\r
-# delete the original. This doesn't support all of gzip's options.\r
-#\r
-# The 'gzip' module in the standard library provides a more complete\r
-# implementation of gzip-format files.\r
-\r
-import zlib, sys, os\r
-\r
-FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16\r
-\r
-def write32(output, value):\r
- output.write(chr(value & 255)) ; value=value // 256\r
- output.write(chr(value & 255)) ; value=value // 256\r
- output.write(chr(value & 255)) ; value=value // 256\r
- output.write(chr(value & 255))\r
-\r
-def read32(input):\r
- v = ord(input.read(1))\r
- v += (ord(input.read(1)) << 8 )\r
- v += (ord(input.read(1)) << 16)\r
- v += (ord(input.read(1)) << 24)\r
- return v\r
-\r
-def compress (filename, input, output):\r
- output.write('\037\213\010') # Write the header, ...\r
- output.write(chr(FNAME)) # ... flag byte ...\r
-\r
- statval = os.stat(filename) # ... modification time ...\r
- mtime = statval[8]\r
- write32(output, mtime)\r
- output.write('\002') # ... slowest compression alg. ...\r
- output.write('\377') # ... OS (=unknown) ...\r
- output.write(filename+'\000') # ... original filename ...\r
-\r
- crcval = zlib.crc32("")\r
- compobj = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,\r
- zlib.DEF_MEM_LEVEL, 0)\r
- while True:\r
- data = input.read(1024)\r
- if data == "":\r
- break\r
- crcval = zlib.crc32(data, crcval)\r
- output.write(compobj.compress(data))\r
- output.write(compobj.flush())\r
- write32(output, crcval) # ... the CRC ...\r
- write32(output, statval[6]) # and the file size.\r
-\r
-def decompress (input, output):\r
- magic = input.read(2)\r
- if magic != '\037\213':\r
- print 'Not a gzipped file'\r
- sys.exit(0)\r
- if ord(input.read(1)) != 8:\r
- print 'Unknown compression method'\r
- sys.exit(0)\r
- flag = ord(input.read(1))\r
- input.read(4+1+1) # Discard modification time,\r
- # extra flags, and OS byte.\r
- if flag & FEXTRA:\r
- # Read & discard the extra field, if present\r
- xlen = ord(input.read(1))\r
- xlen += 256*ord(input.read(1))\r
- input.read(xlen)\r
- if flag & FNAME:\r
- # Read and discard a null-terminated string containing the filename\r
- while True:\r
- s = input.read(1)\r
- if s == '\0': break\r
- if flag & FCOMMENT:\r
- # Read and discard a null-terminated string containing a comment\r
- while True:\r
- s=input.read(1)\r
- if s=='\0': break\r
- if flag & FHCRC:\r
- input.read(2) # Read & discard the 16-bit header CRC\r
-\r
- decompobj = zlib.decompressobj(-zlib.MAX_WBITS)\r
- crcval = zlib.crc32("")\r
- length = 0\r
- while True:\r
- data=input.read(1024)\r
- if data == "":\r
- break\r
- decompdata = decompobj.decompress(data)\r
- output.write(decompdata)\r
- length += len(decompdata)\r
- crcval = zlib.crc32(decompdata, crcval)\r
-\r
- decompdata = decompobj.flush()\r
- output.write(decompdata)\r
- length += len(decompdata)\r
- crcval = zlib.crc32(decompdata, crcval)\r
-\r
- # We've read to the end of the file, so we have to rewind in order\r
- # to reread the 8 bytes containing the CRC and the file size. The\r
- # decompressor is smart and knows when to stop, so feeding it\r
- # extra data is harmless.\r
- input.seek(-8, 2)\r
- crc32 = read32(input)\r
- isize = read32(input)\r
- if crc32 != crcval:\r
- print 'CRC check failed.'\r
- if isize != length:\r
- print 'Incorrect length of data produced'\r
-\r
-def main():\r
- if len(sys.argv)!=2:\r
- print 'Usage: minigzip.py <filename>'\r
- print ' The file will be compressed or decompressed.'\r
- sys.exit(0)\r
-\r
- filename = sys.argv[1]\r
- if filename.endswith('.gz'):\r
- compressing = False\r
- outputname = filename[:-3]\r
- else:\r
- compressing = True\r
- outputname = filename + '.gz'\r
-\r
- input = open(filename, 'rb')\r
- output = open(outputname, 'wb')\r
-\r
- if compressing:\r
- compress(filename, input, output)\r
- else:\r
- decompress(input, output)\r
-\r
- input.close()\r
- output.close()\r
-\r
-if __name__ == '__main__':\r
- main()\r