SHARE
TWEET

Wander Wonder .FLD Unpack v1

a guest Apr 12th, 2015 20 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import os
  2. import struct
  3. import glob
  4. specfile = 'fldfilespec.csv' #constant value
  5. specpath = 'fldpathspec.csv' #constant value
  6.  
  7. #Gets data
  8. def get_data(filename):
  9.     totalbytes = os.path.getsize(filename)
  10.     infile = open(filename, 'rb')
  11.     totalfiledata = infile.read(totalbytes)
  12.     infile.close()
  13.     return totalfiledata
  14.  
  15. #The workhorse function. It will unpack an .fld file like the name says.
  16. def unpack_fld(filename):
  17.     print 'Unpacking {}:'.format(filename) #User message
  18.     filedata = get_data(filename)          #Get data
  19.     indextablestart = struct.unpack('<I',filedata[0x8:0xC])[0]  #Find index table start position
  20.     number_of_files = struct.unpack('<I',filedata[0xC:0x10])[0] #Find number of files in archive
  21.  
  22.     if filedata[0x14] == '\x00': #Case 1: No path info. This is only true for wander.fld.
  23.         path = ''
  24.     else:                       #Case 2: There is path info.
  25.         path = filedata[0x14:filedata.find('\x00',0x14)]   #Find path info
  26.         if os.path.exists(path[:-1]) == False:             #Make a directory if there isn't already one
  27.             os.mkdir(path[:-1])
  28. #Builds up the SPECPATH file, which will eventually contain path info for all .fld files
  29. #The SPECPATH file is used by the fld packing program when it runs later    
  30.     with open(specpath,'ab') as f:              
  31.         f.write(filename + ',' + path + '\r\n') #Append the .fld filename and path to SPECPATH file
  32.    
  33. #pos is the position counter
  34.     pos = indextablestart   #Jump the position counter to the start of the index table
  35.     for x in range(number_of_files):    #Does these steps a number of times equal to the number of files in the archive
  36.         fname = filedata[pos:pos+12]    #Find filename data for this file
  37.         while fname[-1] == '\x00':      #Strip out null characters from the filename
  38.             fname = fname[:-1]          #Deletes the last character (if it's a null)
  39.         pos += 12           #Jump the position counter to the file offset data
  40.         fpos = struct.unpack('<I',filedata[pos:pos+4])[0]   #Get the offset data
  41.         pos += 4            #Jump the position counter to the file size data
  42.         fsize = struct.unpack('<I',filedata[pos:pos+4])[0]  #Get the file size data
  43.         pos += 4            #Jump the position counter to the file name for the next file
  44.         print 'Filename: {}, Size: {}'.format(fname, fsize)     #Print message for user
  45.         with open(path + fname,'wb') as f:  #Save the file using offset and size data previous found, and with the proper path
  46.             f.write(filedata[fpos:fpos+fsize])
  47. #Builds up the SPECFILE file, which will eventually contain file list info for all .fld files
  48. #The SPECFILE file is used by the fld packing program when it runs later    
  49.         with open(specfile,'ab') as f:      #Append the .fld filename and sub-filename to SPECFILE file
  50.             f.write(filename + ',' + fname + '\r\n')
  51.     print ''    #Spacing line prints when finished processing each .fld file
  52.  
  53. #Blanks out the SPECFILE and SPECPATH files when the program starts
  54. with open(specfile,'wb') as f:
  55.     f.write('')
  56. with open(specpath,'wb') as f:
  57.     f.write('')
  58. #Wander.fld contains other .fld files
  59. unpack_fld('wander.fld')
  60. #Unpacks the rest of the .fld files
  61. for f in glob.glob('*.fld'):
  62.     if f != 'wander.fld':
  63.         unpack_fld(f)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top