-
Notifications
You must be signed in to change notification settings - Fork 3
/
archive.py
68 lines (60 loc) · 2.21 KB
/
archive.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
import os
import re
import time
import sys
import json
import zipfile
import hashlib
from base64 import standard_b64encode as b64encode
def base64sha256(zip_path):
with open(zip_path, 'rb') as zip_file:
sha256 = hashlib.sha256()
sha256.update(zip_file.read())
base64sha256 = b64encode(sha256.digest()).decode('utf-8')
return base64sha256
def get_all_file_paths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append({"filepath": filepath, "filename": filename})
return file_paths
def main():
data = json.load(sys.stdin)
file_paths = get_all_file_paths(data['source_dir'])
try:
with zipfile.ZipFile(data['output_path'], mode='w') as zf:
for file_path in sorted(file_paths, key=lambda d: d['filename']):
file_path = file_path['filepath']
file_bytes = open(file_path, 'rb').read()
file_bytes = file_bytes.replace(b'\r\n', b'\n')
file_path = re.sub('^' + data['source_dir'], '', file_path)
file_path = re.sub('^' + '/', '', file_path)
file_path = re.sub('^' + r'\\', '', file_path)
info = zipfile.ZipInfo(
str(file_path),
date_time=(1980, 1, 1, 00, 00, 00),
)
info.compress_type = zipfile.ZIP_DEFLATED
info.create_system = 0
info.external_attr = 0o777 << 16
zf.writestr(info, file_bytes)
zf.close()
finally:
zip_data = {
"output_path":
str(data['output_path']),
"output_absolute_path":
str(os.path.abspath(data['output_path'])),
"output_size":
str(os.stat(data['output_path']).st_size),
"output_md5":
str(
hashlib.md5(open(data['output_path'],
'rb').read()).hexdigest()),
"output_base64sha256":
str(base64sha256(data['output_path']))
}
print(json.dumps(zip_data))
if __name__ == "__main__":
main()