-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpack.py
More file actions
76 lines (64 loc) · 1.88 KB
/
Copy pathpack.py
File metadata and controls
76 lines (64 loc) · 1.88 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
import glob
import struct
import sys
import os
import io
from PIL import Image
def packFile(file):
base, ext = os.path.splitext(file)
name = os.path.basename(base)
size = None
data = None
# 过滤掉txt文件
if ext == '.txt':
return None
# 去掉png和ogg的后缀,其他后缀保留
if ext != '.png' and ext != '.ogg':
name += ext
print(file)
if ext == '.png':
im = Image.open(file)
out = io.BytesIO()
im.save(out, format='BMP')
data = out.getvalue()
size = len(data)
return {
'name': bytes(name, 'ascii'),
'size': size or os.path.getsize(file),
'file': file,
'data': data
}
def pack(file):
packName = file + '.arc'
print()
print('package:', packName)
files = map(lambda x: os.path.join(file, x), os.listdir(file))
packEntries = map(packFile, files)
packEntries = filter(lambda x: x, packEntries)
packEntries = list(packEntries)
# print(packEntries)
p = open(packName, 'wb')
p.write(struct.pack('<12sL', b'PackFile ', len(packEntries)))
dataOffset = p.tell() + len(packEntries)*struct.calcsize('<16sLLLL');
entryOffset = 0
for entry in packEntries:
p.write(struct.pack('<16sLLLL', entry['name'], entryOffset, entry['size'], 0, 0))
entryOffset += entry['size']
for entry in packEntries:
# print(entry['file'])
if entry['data']:
p.write(entry['data'])
else:
e = open(entry['file'], 'rb')
p.write(e.read())
e.close()
p.close()
if __name__ == '__main__':
if len(sys.argv) < 2:
print('Usage: pack.py <dirs>')
print('BGI pack arc package')
sys.exit(1)
for arg in sys.argv[1:]:
for file in glob.glob(arg):
if os.path.isdir(file):
pack(file)