-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathsplit.py
111 lines (96 loc) · 3.12 KB
/
split.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
108
109
110
111
import os
from shutil import rmtree
from sys import argv
from sys import exit as exit_
argc = len(argv)
filename = None
step = 4096
args = argv[1:]
argc -= 1
zstd_path = None
for i in range(argc):
lowered = args[i].lower()
is_last = i == argc - 1
if lowered == '--help' or lowered == '-h':
print('--help - Display information.')
print('--step {int} - Set step')
print('--filename {path} - Set file path')
print('--zstd_path {path} - Set path to zstd and use it')
exit_(0)
elif lowered == '--step' or lowered == '-s':
if is_last:
print('Argument required!')
exit_(1)
else:
step = int(args[i + 1])
elif lowered == '--filename' or lowered == '-f':
if is_last:
print('Argument required!')
exit_(1)
else:
filename = args[i + 1]
elif lowered == '--zstd_path' or lowered == '-z':
if os.name == 'posix':
zstd_path = 'zstd'
if os.system(zstd_path + ' --version'):
while True:
input_result = input('Install ' + zstd_path + ' [Y/n]? ').lower().strip()
if input_result == 'n':
exit_(1)
elif input_result == 'y':
os.system('sudo apt-get install zstd')
if os.system(zstd_path + ' --version'):
print(zstd_path + ' installation failed!')
exit_(1)
break
else:
if is_last:
print('Argument required!')
exit_(1)
else:
zstd_path = args[i + 1]
if argc <= 1 or not filename:
print('Use --help for more information')
exit_(1)
if not os.access(filename, os.F_OK):
print('Could not open file!')
exit_(1)
ext = filename.split('.')[-1]
no_ext = filename[:-len(ext) - 1].replace('\\', '/').split('/')[-1]
out_dir = os.path.join(os.path.dirname(filename), no_ext + '_out')
if os.path.isdir(out_dir):
if os.listdir(out_dir):
while True:
input_result = input('Out dir is not empty! Continue [Y/n]? ').lower().strip()
if input_result == 'n':
exit_(1)
elif input_result == 'y':
rmtree(out_dir)
os.mkdir(out_dir)
break
else:
os.mkdir(out_dir)
opened = open(filename, 'rb')
readf = opened.read()
opened.close()
file_length = len(readf)
i = 0
j = 0
while i < file_length - step:
file_path = os.path.join(out_dir, f'{no_ext}-{j}.{ext}')
temp_file = open(file_path, 'wb')
temp_file.write(readf[i:i + step])
temp_file.close()
if zstd_path:
cmd = f'{zstd_path} --format=zstd -o "{file_path}.zst" {file_path}'
os.system(cmd)
os.remove(file_path)
i += step
j += 1
file_path = os.path.join(out_dir, f'{no_ext}-{j}.{ext}')
temp_file = open(file_path, 'wb')
temp_file.write(readf[i:file_length])
temp_file.close()
if zstd_path:
os.system(f'{zstd_path} --format=zstd -o "{file_path}.zst" {file_path}')
os.remove(file_path)