]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Demo/zlib/minigzip.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Demo / zlib / minigzip.py
CommitLineData
4710c53d 1#!/usr/bin/env python\r
2# Demo program for zlib; it compresses or decompresses files, but *doesn't*\r
3# delete the original. This doesn't support all of gzip's options.\r
4#\r
5# The 'gzip' module in the standard library provides a more complete\r
6# implementation of gzip-format files.\r
7\r
8import zlib, sys, os\r
9\r
10FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16\r
11\r
12def write32(output, value):\r
13 output.write(chr(value & 255)) ; value=value // 256\r
14 output.write(chr(value & 255)) ; value=value // 256\r
15 output.write(chr(value & 255)) ; value=value // 256\r
16 output.write(chr(value & 255))\r
17\r
18def read32(input):\r
19 v = ord(input.read(1))\r
20 v += (ord(input.read(1)) << 8 )\r
21 v += (ord(input.read(1)) << 16)\r
22 v += (ord(input.read(1)) << 24)\r
23 return v\r
24\r
25def compress (filename, input, output):\r
26 output.write('\037\213\010') # Write the header, ...\r
27 output.write(chr(FNAME)) # ... flag byte ...\r
28\r
29 statval = os.stat(filename) # ... modification time ...\r
30 mtime = statval[8]\r
31 write32(output, mtime)\r
32 output.write('\002') # ... slowest compression alg. ...\r
33 output.write('\377') # ... OS (=unknown) ...\r
34 output.write(filename+'\000') # ... original filename ...\r
35\r
36 crcval = zlib.crc32("")\r
37 compobj = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS,\r
38 zlib.DEF_MEM_LEVEL, 0)\r
39 while True:\r
40 data = input.read(1024)\r
41 if data == "":\r
42 break\r
43 crcval = zlib.crc32(data, crcval)\r
44 output.write(compobj.compress(data))\r
45 output.write(compobj.flush())\r
46 write32(output, crcval) # ... the CRC ...\r
47 write32(output, statval[6]) # and the file size.\r
48\r
49def decompress (input, output):\r
50 magic = input.read(2)\r
51 if magic != '\037\213':\r
52 print 'Not a gzipped file'\r
53 sys.exit(0)\r
54 if ord(input.read(1)) != 8:\r
55 print 'Unknown compression method'\r
56 sys.exit(0)\r
57 flag = ord(input.read(1))\r
58 input.read(4+1+1) # Discard modification time,\r
59 # extra flags, and OS byte.\r
60 if flag & FEXTRA:\r
61 # Read & discard the extra field, if present\r
62 xlen = ord(input.read(1))\r
63 xlen += 256*ord(input.read(1))\r
64 input.read(xlen)\r
65 if flag & FNAME:\r
66 # Read and discard a null-terminated string containing the filename\r
67 while True:\r
68 s = input.read(1)\r
69 if s == '\0': break\r
70 if flag & FCOMMENT:\r
71 # Read and discard a null-terminated string containing a comment\r
72 while True:\r
73 s=input.read(1)\r
74 if s=='\0': break\r
75 if flag & FHCRC:\r
76 input.read(2) # Read & discard the 16-bit header CRC\r
77\r
78 decompobj = zlib.decompressobj(-zlib.MAX_WBITS)\r
79 crcval = zlib.crc32("")\r
80 length = 0\r
81 while True:\r
82 data=input.read(1024)\r
83 if data == "":\r
84 break\r
85 decompdata = decompobj.decompress(data)\r
86 output.write(decompdata)\r
87 length += len(decompdata)\r
88 crcval = zlib.crc32(decompdata, crcval)\r
89\r
90 decompdata = decompobj.flush()\r
91 output.write(decompdata)\r
92 length += len(decompdata)\r
93 crcval = zlib.crc32(decompdata, crcval)\r
94\r
95 # We've read to the end of the file, so we have to rewind in order\r
96 # to reread the 8 bytes containing the CRC and the file size. The\r
97 # decompressor is smart and knows when to stop, so feeding it\r
98 # extra data is harmless.\r
99 input.seek(-8, 2)\r
100 crc32 = read32(input)\r
101 isize = read32(input)\r
102 if crc32 != crcval:\r
103 print 'CRC check failed.'\r
104 if isize != length:\r
105 print 'Incorrect length of data produced'\r
106\r
107def main():\r
108 if len(sys.argv)!=2:\r
109 print 'Usage: minigzip.py <filename>'\r
110 print ' The file will be compressed or decompressed.'\r
111 sys.exit(0)\r
112\r
113 filename = sys.argv[1]\r
114 if filename.endswith('.gz'):\r
115 compressing = False\r
116 outputname = filename[:-3]\r
117 else:\r
118 compressing = True\r
119 outputname = filename + '.gz'\r
120\r
121 input = open(filename, 'rb')\r
122 output = open(outputname, 'wb')\r
123\r
124 if compressing:\r
125 compress(filename, input, output)\r
126 else:\r
127 decompress(input, output)\r
128\r
129 input.close()\r
130 output.close()\r
131\r
132if __name__ == '__main__':\r
133 main()\r