Repository navigation
Expand file tree
/
Copy pathgzip_check.py
More file actions
executable file
·85 lines (81 loc) · 2.51 KB
/
Copy pathgzip_check.py
File metadata and controls
executable file
·85 lines (81 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python2
from __future__ import print_function
import os
import sys
import struct
import datetime
import zlib
if len(sys.argv) < 2 or not os.path.isfile(sys.argv[1]) or not sys.argv[1].endswith(('.tgz', '.gz')):
print('Please provide correct tar.gz file')
sys.exit(1)
FTEXT, FHCRC, FEXTRA, FNAME, FCOMMENT = 1, 2, 4, 8, 16
with open(sys.argv[1]) as f:
# read and check gzip header
print('magic header is ', end='')
if f.read(2) == '\037\213':
print('correct')
else:
print('not correct. exiting')
sys.exit(0)
print('compression method is ', end='')
if ord(f.read(1)) == 8:
print('correct')
else:
print('not correct')
flag = ord(f.read(1))
print('flag = %s' % flag)
print('modification time is %s' % datetime.datetime.fromtimestamp(int(struct.unpack("<L", f.read(4))[0])).strftime('%d/%m/%Y %H:%M:%S'))
print('extra flag = %s' % ord(f.read(1)))
print('os type = %s' % ord(f.read(1)))
if flag & FHCRC:
print('skipping 16-bit header CRC')
f.read(2)
if flag & FEXTRA:
print('skipping the extra field')
xlen = ord(f.read(1))
xlen += 256*ord(f.read(1))
f.read(xlen)
if flag & FNAME:
print('filename is ', end='')
while True:
s = f.read(1)
if s == '\0':
break
print(s, end='')
print('')
if flag & FCOMMENT:
print('comment is ', end='')
while True:
s = f.read(1)
if s == '\0':
break
print(s, end='')
print('')
# read compressed data
decompressor = zlib.decompressobj(-zlib.MAX_WBITS)
crcval = zlib.crc32("") & 0xffffffffL
length = 0
while True:
data = f.read(1024)
if data == '':
break
decompdata = decompressor.decompress(data)
length += len(decompdata)
crcval = zlib.crc32(decompdata, crcval)
decompdata = decompressor.flush()
length += len(decompdata)
crcval = zlib.crc32(decompdata, crcval) & 0xffffffffL
# reread last 8 bytes to get CRC and file size
f.seek(-8, 2)
crc32 = struct.unpack("<I", f.read(4))[0]
isize = struct.unpack("<I", f.read(4))[0]
print('CRC check ', end='')
if crc32 == crcval:
print('passed')
else:
print('failed (%s != %s)' % (crc32, crcval))
print('file size is ', end='')
if isize == length:
print('correct')
else:
print('incorrect (%s != %s)' % (isize, length))