-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathzstd.py
151 lines (140 loc) · 7.02 KB
/
zstd.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import zstandard as zs
import sarc
import os
import io
import sys
def get_correct_path(relative_path):
try:
base_path = sys._MEIPASS
except Exception:
base_path = os.path.abspath(".")
return os.path.join(base_path, relative_path)
class Zstd:
# Initialize class variable for dictionaries
dictionaries = None
pack_zsdic = None
bcett_byml_zsdic = None
zs_zsdic = None
# Initialize decompressor
def __init__(self, format=zs.FORMAT_ZSTD1): # zs.FORMAT_ZSTD1_MAGICLESS for headerless
self.format = format
self.decompressor = zs.ZstdDecompressor()
# Load dictionaries only if they haven't been loaded before
if Zstd.dictionaries is None:
zs_dic_path = "dic/ZsDic.pack.zs"
zs_dic_path = get_correct_path(zs_dic_path)
with open(os.path.join(zs_dic_path), 'rb') as file:
data = file.read()
dictionaries = self.decompressor.decompress(data)
Zstd.dictionaries = sarc.Sarc(dictionaries).files
# Cache the three files
for dic in Zstd.dictionaries:
if dic["Name"] == 'pack.zsdic':
Zstd.pack_zsdic = dic["Data"]
elif dic["Name"] == 'bcett.byml.zsdic':
Zstd.bcett_byml_zsdic = dic["Data"]
elif dic["Name"] == 'zs.zsdic':
Zstd.zs_zsdic = dic["Data"]
# Decompresses specified file to specified location
def _DecompressFile(self, filepath, output_dir='', with_dict=False, no_output=False):
if with_dict and os.path.basename(filepath) != 'ZsDic.pack.zs':
if os.path.splitext(os.path.splitext(filepath)[0])[1] == '.pack':
dictionary = Zstd.pack_zsdic
elif os.path.splitext(os.path.splitext(filepath)[0])[1] == '.byml':
if filepath.endswith('.bcett.byml.zs'):
dictionary = Zstd.bcett_byml_zsdic
else:
dictionary = Zstd.zs_zsdic
else:
dictionary = Zstd.zs_zsdic
self.decompressor = zs.ZstdDecompressor(zs.ZstdCompressionDict(dictionary), format=self.format)
else:
self.decompressor = zs.ZstdDecompressor(format=self.format)
with open(filepath, 'rb') as file:
data = file.read()
if os.path.splitext(filepath)[1] in ['.zs', '.zstd']:
filepath = os.path.splitext(filepath)[0]
else:
return
try:
decompressed_data = self.decompressor.decompress(data)
except zs.ZstdError as e:
print(f"Error decompressing file {filepath}: {e}")
return None
if not(no_output):
with open(os.path.join(output_dir, os.path.basename(filepath)), 'wb') as file:
file.write(decompressed_data)
return decompressed_data
# Decompresses a file or directory
def Decompress(self, filepath, output_dir='', with_dict=True, no_output=False):
if os.path.isfile(filepath):
return self._DecompressFile(filepath, output_dir, with_dict, no_output)
elif os.path.isdir(filepath):
for root_dir, dir, files in os.walk(filepath):
for file in files:
if os.path.isfile(os.path.join(root_dir, file)):
rel_path = os.path.relpath(root_dir, filepath)
if not(os.path.exists(os.path.join(output_dir, rel_path))):
os.makedirs(os.path.join(output_dir, rel_path))
return self._DecompressFile(os.path.join(root_dir, file), os.path.join(output_dir, rel_path), with_dict, no_output)
# Get size of decompressed file
def GetDecompressedSize(self, filepath, with_dict=True):
with open(filepath, 'rb') as file:
if os.path.splitext(filepath)[1] in ['.zs', '.zstd']:
if os.path.splitext(filepath)[1] == '.mc':
file.seek(0xc)
data = file.read()
if with_dict:
if os.path.splitext(os.path.splitext(filepath)[0])[1] == '.pack':
dictionary = Zstd.pack_zsdic
elif os.path.splitext(os.path.splitext(filepath)[0])[1] == '.byml':
if filepath.endswith('.bcett.byml.zs'):
dictionary = Zstd.bcett_byml_zsdic
else:
dictionary = Zstd.zs_zsdic
else:
dictionary = Zstd.zs_zsdic
self.decompressor = zs.ZstdDecompressor(zs.ZstdCompressionDict(dictionary), format=self.format)
else:
self.decompressor = zs.ZstdDecompressor(format=self.format)
return len(self.decompressor.decompress(data))
else:
file.seek(0, io.SEEK_END)
return file.tell()
# Compresses file to specified location
def _CompressFile(self, filepath, output_dir='', level=16, with_dict=False):
if with_dict and os.path.basename(filepath) != 'ZsDic.pack.zs':
if filepath.endswith('.pack'):
dictionary = Zstd.pack_zsdic
elif filepath.endswith('.byml'):
if filepath.endswith('.bcett.byml'):
dictionary = Zstd.bcett_byml_zsdic
else:
dictionary = Zstd.zs_zsdic
else:
dictionary = Zstd.zs_zsdic
self.compressor = zs.ZstdCompressor(level, zs.ZstdCompressionDict(dictionary))
else:
self.compressor = zs.ZstdCompressor(level)
with open(filepath, 'rb') as file:
data = file.read()
filepath += '.zs'
with open(os.path.join(output_dir, os.path.basename(filepath)), 'wb') as file:
if self.format == zs.FORMAT_ZSTD1_MAGICLESS:
file.write(self.compressor.compress(data)[4:])
return self.compressor.compress(data)[4:]
else:
file.write(self.compressor.compress(data))
return self.compressor.compress(data)
# Compresses file or files and maintains directory structure
def Compress(self, filepath, output_dir='', level=16, with_dict=True):
if os.path.isfile(filepath):
return self._CompressFile(filepath, output_dir, level, with_dict)
elif os.path.isdir(filepath):
for root_dir, dir, files in os.walk(filepath):
for file in files:
if os.path.isfile(os.path.join(root_dir, file)):
rel_path = os.path.relpath(root_dir, filepath)
if not(os.path.exists(os.path.join(output_dir, rel_path))):
os.makedirs(os.path.join(output_dir, rel_path))
return self._CompressFile(os.path.join(root_dir, file), os.path.join(output_dir, rel_path), level, with_dict)