-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchive.py
More file actions
88 lines (77 loc) · 2.63 KB
/
Archive.py
File metadata and controls
88 lines (77 loc) · 2.63 KB
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
from tkinter import filedialog
import tkinter
import os
def generate_meta_data(files ,fileSizes):
metaData = ""
for i in range(len(files)-1):
metaData += files[i].split("/")[-1] + "<" + str(fileSizes[i]) + "<" + "\n"
metaData += files[len(files)-1].split("/")[-1] + "<" + str(fileSizes[len(files)-1]) + "<"
return metaData
# get files paths
root = tkinter.Tk()
root.withdraw()
files = filedialog.askopenfilenames()
root.destroy()
files = [file.replace("\\","/") for file in files]
#get file sizefor each selected file
fileSizes = [os.path.getsize(file) for file in files]
totalSize = sum(fileSizes)
#get part file size
required_size = int(float(input("Enter Part Size (Bytes) : ")))
if required_size<1:
required_size = 1 ;
#get part cout
part_count = (totalSize/required_size)
if int(part_count) < part_count:
part_count = part_count+1
part_count = int(part_count)
print(part_count)
#cteate path
path = "/".join(files[0].split("/")[:-1])
if path!="":
path = path + '/'
out_path = path + "Archives"
os.mkdir(out_path)
#change write file part to next part
def change_to_next_part_file():
global part_no
global write_file
global write_file_path
if part_no+1 <= part_count:
part_no +=1
write_file_path = out_path+"/part-{}.part".format(part_no)
write_file = open(write_file_path,"wb")
part_no = 1
writed_size = 0
write_file_path = out_path+"/part-{}.part".format(part_no)
write_file = open(write_file_path,"wb")
for i in range(len(files)):
read_file = open(files[i] ,"rb")
for content in read_file :
while content:
if (len(content) + writed_size) <= required_size :
writed_size += len(content)
write_file.write(content)
if writed_size == required_size:
write_file.close()
writed_size = 0;
change_to_next_part_file()
content = ""
else :
content_left = content[required_size-writed_size:]
content = content[0:required_size-writed_size]
write_file.write(content)
write_file.close()
content = content_left
writed_size = 0
change_to_next_part_file()
read_file.close()
if not write_file.closed:
write_file.close()
#create metadata file
meta_data = generate_meta_data(files,fileSizes)
with open(out_path+"/main.part","w") as meta_file :
meta_file.write(str(part_count)+"<")
meta_file.write(str(required_size)+"<")
meta_file.write(str(os.path.getsize(write_file_path))+"<\n")
meta_file.write(meta_data)