-
Notifications
You must be signed in to change notification settings - Fork 15
/
lib_csv.py
107 lines (81 loc) · 2.24 KB
/
lib_csv.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import lzma
#
# decode_file
#
def decode_file(path):
basename = os.path.splitext(path)[0]
decodedname = basename + ".decoded.csv"
print("process:", path, "->", decodedname)
with open(path, 'rb') as f:
data = f.read()
tempdata = bytearray()
for i in range(0, 8):
tempdata.append(data[i])
for i in range(0, 4):
tempdata.append(0)
for i in range(8, len(data)):
tempdata.append(data[i])
try:
with open(decodedname, 'wb') as f:
decompressor = lzma.LZMADecompressor()
unpack_data = decompressor.decompress(tempdata)
f.write(unpack_data)
except:
print("invalid input:", path)
#
# decode_file
#
def len_2_bytes(datalen, max_len=4):
data = []
while datalen > 0:
item = datalen % 256
datalen = int(datalen / 256)
data.append(item)
while len(data) < max_len:
data.append(0)
return data
def encode_file(path, max_len=4):
basename = os.path.splitext(path)[0]
encodedname = basename + ".encoded.csv"
print("process:", path, "->", encodedname)
with open(path, 'rb') as f:
data = f.read()
filters = [
{
"id": lzma.FILTER_LZMA1,
"dict_size": 256 * 1024,
"lc": 3,
"lp": 0,
"pb": 2,
"mode": lzma.MODE_NORMAL
},
]
pack_data = lzma.compress(data, format=lzma.FORMAT_ALONE, filters=filters)
lzmadata = bytearray()
for i in range(0, 5):
lzmadata.append(pack_data[i])
data_size = len_2_bytes(len(data), max_len)
for size in data_size:
lzmadata.append(size)
for i in range(13, len(pack_data)):
lzmadata.append(pack_data[i])
#for i in range(0, len(lzmadata)):
# print("i: ", i, " val: ", lzmadata[i])
with open(encodedname, 'wb') as f:
f.write(lzmadata)
#
# restore_file
#
def restore_file(path):
with open(path, 'rb') as f:
data = f.read()
tempdata = bytearray()
for i in range(0, 8):
tempdata.append(data[i])
for i in range(0, 4):
tempdata.append(0)
for i in range(8, len(data)):
tempdata.append(data[i])
with open('real.lzma', 'wb') as f:
f.write(tempdata)