Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import struct
- import glob
- specfile = 'fldfilespec.csv' #constant value
- specpath = 'fldpathspec.csv' #constant value
- #Gets data
- def get_data(filename):
- totalbytes = os.path.getsize(filename)
- infile = open(filename, 'rb')
- totalfiledata = infile.read(totalbytes)
- infile.close()
- return totalfiledata
- #The workhorse function. It will unpack an .fld file like the name says.
- def unpack_fld(filename):
- print 'Unpacking {}:'.format(filename) #User message
- filedata = get_data(filename) #Get data
- indextablestart = struct.unpack('<I',filedata[0x8:0xC])[0] #Find index table start position
- number_of_files = struct.unpack('<I',filedata[0xC:0x10])[0] #Find number of files in archive
- if filedata[0x14] == '\x00': #Case 1: No path info. This is only true for wander.fld.
- path = ''
- else: #Case 2: There is path info.
- path = filedata[0x14:filedata.find('\x00',0x14)] #Find path info
- if os.path.exists(path[:-1]) == False: #Make a directory if there isn't already one
- os.mkdir(path[:-1])
- #Builds up the SPECPATH file, which will eventually contain path info for all .fld files
- #The SPECPATH file is used by the fld packing program when it runs later
- with open(specpath,'ab') as f:
- f.write(filename + ',' + path + '\r\n') #Append the .fld filename and path to SPECPATH file
- #pos is the position counter
- pos = indextablestart #Jump the position counter to the start of the index table
- for x in range(number_of_files): #Does these steps a number of times equal to the number of files in the archive
- fname = filedata[pos:pos+12] #Find filename data for this file
- while fname[-1] == '\x00': #Strip out null characters from the filename
- fname = fname[:-1] #Deletes the last character (if it's a null)
- pos += 12 #Jump the position counter to the file offset data
- fpos = struct.unpack('<I',filedata[pos:pos+4])[0] #Get the offset data
- pos += 4 #Jump the position counter to the file size data
- fsize = struct.unpack('<I',filedata[pos:pos+4])[0] #Get the file size data
- pos += 4 #Jump the position counter to the file name for the next file
- print 'Filename: {}, Size: {}'.format(fname, fsize) #Print message for user
- with open(path + fname,'wb') as f: #Save the file using offset and size data previous found, and with the proper path
- f.write(filedata[fpos:fpos+fsize])
- #Builds up the SPECFILE file, which will eventually contain file list info for all .fld files
- #The SPECFILE file is used by the fld packing program when it runs later
- with open(specfile,'ab') as f: #Append the .fld filename and sub-filename to SPECFILE file
- f.write(filename + ',' + fname + '\r\n')
- print '' #Spacing line prints when finished processing each .fld file
- #Blanks out the SPECFILE and SPECPATH files when the program starts
- with open(specfile,'wb') as f:
- f.write('')
- with open(specpath,'wb') as f:
- f.write('')
- #Wander.fld contains other .fld files
- unpack_fld('wander.fld')
- #Unpacks the rest of the .fld files
- for f in glob.glob('*.fld'):
- if f != 'wander.fld':
- unpack_fld(f)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement