forked from prophile/xsera
-
Notifications
You must be signed in to change notification settings - Fork 1
/
write_xar.py
44 lines (36 loc) · 957 Bytes
/
write_xar.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
import os
import sys
import struct
import os.path
fileHeader = ""
fileBlockData = ""
numFiles = 0
def write_file ( path ):
global numFiles
global fileHeader
global fileBlockData
numFiles = numFiles + 1
fp = open(path, 'r')
fileContent = fp.read()
fp.close()
offset = len(fileBlockData)
length = len(fileContent)
fileBlockData += fileContent
fileHeader += struct.pack('>IIH', offset, length, len(path))
fileHeader += path
print 'A ' + path + '\n'
def write_directory_recursive ( directory ):
fileList = os.listdir(directory)
for fileEntry in fileList:
if os.path.isdir(directory + '/' + fileEntry):
write_directory_recursive(directory + '/' + fileEntry)
else:
write_file(directory + '/' + fileEntry)
targetFile = sys.argv[1]
sourceDirectory = sys.argv[2]
write_directory_recursive(sourceDirectory)
fp = open(targetFile, 'w')
fp.write('XARK')
fp.write(struct.pack('>H', numFiles))
fp.write(fileHeader)
fp.write(fileBlockData)