-
Notifications
You must be signed in to change notification settings - Fork 31
/
doZlib.py
64 lines (59 loc) · 1.85 KB
/
doZlib.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
#!/user/bin/python
#!conding=utf8
import zlib
import sys
import os
def compressStr(msg):
compressed = zlib.compress(msg)
return compressed
def decompressStr(commsg):
decompressed = zlib.decompress(commsg)
return decompressed
def compress(infile, dst, level=9):
infile = open(infile, 'rb')
dst = open(dst, 'wb')
compress = zlib.compressobj(level)
data = infile.read(1024)
while data:
dst.write(compress.compress(data))
data = infile.read(1024)
dst.write(compress.flush())
def decompress(infile, dst):
infile = open(infile, 'rb')
dst = open(dst, 'wb')
decompress = zlib.decompressobj()
data = infile.read(1024)
while data:
dst.write(decompress.decompress(data))
data = infile.read(1024)
dst.write(decompress.flush())
def decompressDir(dirpath):
dirpath=dirpath.strip()
dirpath=dirpath.rstrip("\\")
newDir = dirpath + "plain"
##parentDir = os.path.abspath(os.path.join(dirpath, ".."))
g = os.walk(dirpath)
for path,d,filelist in g:
#print d;
for filename in filelist:
filepath = os.path.join(path, filename)
print filepath
distPath = filepath.replace(dirpath,newDir)
fileDir , tmpName = os.path.split(distPath)
isExists=os.path.exists(fileDir)
if not isExists:
os.makedirs(fileDir)
decompress(filepath, distPath)
print distPath + "succeed!"
if __name__ == "__main__":
#compress('in.txt', 'out.txt')
if(len(sys.argv) < 2):
print sys.argv[0] + " filePath / destPath"
sys.exit()
if(os.path.isfile(sys.argv[1])):
decompress(sys.argv[1], sys.argv[2])
sys.exit()
if(os.path.isdir(sys.argv[1])):
decompressDir(sys.argv[1])
sys.exit()
print ( " path is error! " )