Advertisement
Guest User

unpack.py

a guest
Apr 23rd, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. import os,struct,binascii
  2.  
  3. def get_int4():
  4.         return int( struct.unpack("I",index.read(4))[0] )
  5. def get_int2():
  6.         return int( struct.unpack("H",index.read(2))[0] )
  7.  
  8. def get_sz():
  9.         out = ""
  10.         while True:
  11.                 cur = index.read(1)
  12.                 if cur == b'\x00': break
  13.                 out += struct.unpack("c",cur)[0].decode("ASCII")
  14.         return out
  15.  
  16. index = open("cstrike_pak_dir.vpk",'rb')
  17.  
  18. print( "Signature:",binascii.b2a_hex(index.read(4)) )
  19. print( "Version:",get_int4() )
  20. print( "Directory length:", get_int4() )
  21. print( "Unknown1:", get_int4() )
  22. unknown2 = get_int4() # footer length?
  23. print( "Unknown2:", unknown2 )
  24. unknown3 = get_int4()
  25. print( "Unknown3:", unknown3 )
  26. print( "Unknown4:", get_int4() )
  27.  
  28. class VpkFile():
  29.         path = ""
  30.         CRC = -1
  31.         archive_index = -1
  32.         offset = -1
  33.         length = -1
  34.         preload = bytes()
  35.  
  36. vpk_files = []
  37.  
  38. while True:
  39.         extension = get_sz()
  40.         if not extension: break        
  41.  
  42.         while True:
  43.                 folder = get_sz()
  44.                 if not folder: break
  45.  
  46.                 while True:
  47.                         filename = get_sz()
  48.                         if not filename: break
  49.  
  50.                         cur_file = VpkFile()
  51.                         vpk_files.append(cur_file)
  52.                         cur_file.path = "{}/{}.{}".format(folder,filename,extension)
  53.  
  54.                         cur_file.CRC = get_int4()
  55.                         preload_bytes = get_int2()
  56.                         cur_file.archive_index = get_int2()
  57.                         if cur_file.archive_index == b'\x7fff':
  58.                                 print("EMBED")
  59.                         cur_file.offset = get_int4()
  60.                         cur_file.length = get_int4()
  61.                         get_int2() # terminator
  62.  
  63.                         if preload_bytes:
  64.                                 cur_file.preload = index.read(preload_bytes)
  65.  
  66. index.close()
  67.  
  68. print("Extracting...")
  69. for vf in vpk_files:
  70.         print(vf.path,"({} bytes)".format(len(vf.preload)+vf.length))
  71.         ending = vf.path.replace("/","\\")
  72.         if ending[0] == " ":
  73.             ending = ending[:0]
  74.             if len(ending) == 0:
  75.                 ending = "xyz"
  76.         full_path = os.path.join(os.getcwd(),"vpk_extracted",ending)
  77.  
  78.         dir = os.path.dirname(full_path)
  79.         if not os.path.isdir(dir):
  80.                 os.makedirs(dir)
  81.         out_data = open(full_path,'wb')
  82.         out_data.write(vf.preload)
  83.         if vf.length:
  84.                 vpk = open("cstrike_pak_{}.vpk".format(str(vf.archive_index).zfill(3)),'rb')
  85.                 vpk.seek(vf.offset)
  86.                 out_data.write(vpk.read(vf.length))
  87.                 vpk.close()
  88.         out_data.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement