forked from fayaz12g/totk-aar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepack.py
125 lines (95 loc) · 3.25 KB
/
repack.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import os
import sys
import time
try:
import SarcLib
except ImportError:
print("SarcLib is not installed!")
ans = input("Do you want to install it now? (y/n)\t")
if ans.lower() == 'y':
import pip
pip.main(['install', 'SarcLib==0.3'])
del pip
import SarcLib
else:
sys.exit(1)
try:
import libyaz0
except ImportError:
print("libyaz0 is not installed!")
ans = input("Do you want to install it now? (y/n)\t")
if ans.lower() == 'y':
import pip
pip.main(['install', 'libyaz0==0.5'])
del pip
import libyaz0
else:
sys.exit(1)
def pack_folder_to_blarc(folder_path, output_file):
"""
Pack the files and folders in the folder_path to a .blarc output_file.
"""
root = os.path.abspath(folder_path)
endianness = '>'
level = -1
pack(root, endianness, level, output_file)
def pack(root, endianness, level, outname):
"""
Pack the files and folders in the root folder.
"""
if "\\" in root:
root = "/".join(root.split("\\"))
if root[-1] == "/":
root = root[:-1]
arc = SarcLib.SARC_Archive(endianness=endianness)
lenroot = len(root.split("/"))
for path, dirs, files in os.walk(root):
if "\\" in path:
path = "/".join(path.split("\\"))
lenpath = len(path.split("/"))
if lenpath == lenroot:
path = ""
else:
path = "/".join(path.split("/")[lenroot - lenpath:])
for file in files:
if path:
filename = ''.join([path, "/", file])
else:
filename = file
print(filename)
fullname = ''.join([root, "/", filename])
i = 0
for folder in filename.split("/")[:-1]:
if not i:
exec("folder%i = SarcLib.Folder(folder + '/'); arc.addFolder(folder%i)".replace('%i', str(i)))
else:
exec("folder%i = SarcLib.Folder(folder + '/'); folder%m.addFolder(folder%i)".replace('%i', str(i)).replace('%m', str(i - 1)))
i += 1
with open(fullname, "rb") as f:
inb = f.read()
hasFilename = True
if file[:5] == "hash_":
hasFilename = False
if not i:
arc.addFile(SarcLib.File(file, inb, hasFilename))
else:
exec("folder%m.addFile(SarcLib.File(file, inb, hasFilename))".replace('%m', str(i - 1)))
data, maxAlignment = arc.save()
if level != -1:
outData = libyaz0.compress(data, maxAlignment, level)
del data
if not outname:
outname = ''.join([root, ".szs"])
else:
outData = data
if not outname:
outname = ''.join([root, ".sarc"])
with open(outname, "wb+") as output:
output.write(outData)
if __name__ == '__main__':
if len(sys.argv) != 3:
print("Usage: python compress_folder.py <folder_path> <output_file>")
sys.exit(1)
folder_path = sys.argv[1]
output_file = sys.argv[2]
pack_folder_to_blarc(folder_path, output_file)