-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzip2gz.py
executable file
·65 lines (58 loc) · 2.63 KB
/
zip2gz.py
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
#!/usr/bin/env python
# This script takes a zip file as input and creates a tree of files containing
# the same raw data as they have in the zip file, but in a way that is usable.
# So for example if a file in the zip file is compressed with deflate, it will
# take that raw deflate data and slap a gzip header and footer around it.
import zipfile
import struct
import sys
import os
import posixpath
if len(sys.argv) == 2:
zipFileName = sys.argv[1]
else:
sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " zipFile")
def skipZipHeader(zipInfo, rawZipFile):
# Use definitions found in pythons zipfile.py to skip the header
rawZipHeader = rawZipFile.read(zipfile.sizeFileHeader)
zipHeader = struct.unpack(zipfile.structFileHeader, rawZipHeader)
rawZipFile.seek(zipHeader[zipfile._FH_FILENAME_LENGTH] + zipHeader[zipfile._FH_EXTRA_FIELD_LENGTH], 1)
def createGzipHeader(zipInfo):
# Create simplest possible gzip header
magic1 = 0x1f # Gzip identification
magic2 = 0x8b
compressionMethod = 0x08 # Deflate
flags = 0x00 # No header CRC, no extra fields, no filename, no comment
timestamp = 0x00000000 # No timestamp
compressionFlags = 0x00 # No level of compression
operatingSystem = 0xff # Unknown operating system
return struct.pack("<BBBBLBB", magic1, magic2, compressionMethod, flags, timestamp, compressionFlags, operatingSystem)
def createGzipFooter(zipInfo):
return struct.pack("<LL", zipInfo.CRC, zipInfo.file_size & 0xffffffff)
zipFile = zipfile.ZipFile(zipFileName)
rawZipFile = open(zipFileName, 'rb')
for zipInfo in zipFile.infolist():
dirName, fileName = posixpath.split(zipInfo.filename)
if "" == fileName:
print(zipInfo.filename)
# Check to not fail if a path is specified twice
if not os.path.isdir(dirName):
os.makedirs(dirName)
else:
rawZipFile.seek(zipInfo.header_offset)
skipZipHeader(zipInfo, rawZipFile)
# Now we are where the deflate data starts
if zipfile.ZIP_STORED == zipInfo.compress_type:
print(zipInfo.filename)
outputFile = open(zipInfo.filename, "wb")
outputFile.write(rawZipFile.read(zipInfo.compress_size))
outputFile.close()
elif zipfile.ZIP_DEFLATED == zipInfo.compress_type:
gzipFilename = zipInfo.filename + ".gz"
print(gzipFilename)
outputFile = open(gzipFilename, "wb")
outputFile.write(createGzipHeader(zipInfo))
rawData = rawZipFile.read(zipInfo.compress_size)
outputFile.write(rawData)
outputFile.write(createGzipFooter(zipInfo))
outputFile.close()