Skip to content

Instantly share code, notes, and snippets.

@thedreamwork
Last active March 4, 2022 07:22
Show Gist options
  • Star 49 You must be signed in to star a gist
  • Fork 34 You must be signed in to fork a gist
  • Save thedreamwork/01691af73eeda292e7327bc6bd3e1d08 to your computer and use it in GitHub Desktop.
Save thedreamwork/01691af73eeda292e7327bc6bd3e1d08 to your computer and use it in GitHub Desktop.
unpack wxapkg
#!/usr/bin/python
# usage python unwxapkg.py filename
import sys,os
import struct
class WxapkgFile:
nameLen = 0
name = ""
offset = 0
size = 0
if len(sys.argv) < 2:
print 'usage: unwxapkg.py filename'
exit()
with open(sys.argv[1], "rb") as f:
root = os.path.dirname(os.path.realpath(f.name))
name = os.path.basename(f.name)
if len(sys.argv) > 2:
name = sys.argv[2]
#read header
firstMark = struct.unpack('B', f.read(1))[0]
print 'first header mark = ' + str(firstMark)
info1 = struct.unpack('>L', f.read(4))[0]
print 'info1 = ' + str(info1)
indexInfoLength = struct.unpack('>L', f.read(4))[0]
print 'indexInfoLength = ' + str(indexInfoLength)
bodyInfoLength = struct.unpack('>L', f.read(4))[0]
print 'bodyInfoLength = ' + str(bodyInfoLength)
lastMark = struct.unpack('B', f.read(1))[0]
print 'last header mark = ' + str(lastMark)
if firstMark != 0xBE or lastMark != 0xED:
print 'its not a wxapkg file!!!!!'
exit()
fileCount = struct.unpack('>L', f.read(4))[0]
print 'fileCount = ' + str(fileCount)
#read index
fileList = []
for i in range(fileCount):
data = WxapkgFile()
data.nameLen = struct.unpack('>L', f.read(4))[0]
data.name = f.read(data.nameLen)
data.offset = struct.unpack('>L', f.read(4))[0]
data.size = struct.unpack('>L', f.read(4))[0]
print 'readFile = ' + data.name + ' at Offset = ' + str(data.offset)
fileList.append(data)
#save files
for d in fileList:
d.name = '/' + name + d.name
path = root + os.path.dirname(d.name)
if not os.path.exists(path):
os.makedirs(path)
w = open(root + d.name, 'w')
f.seek(d.offset)
w.write(f.read(d.size))
w.close()
print 'writeFile = ' + root + d.name
f.close()
@seozed
Copy link

seozed commented Jan 2, 2018

重新改了一版

# -*- coding:utf-8 -*-
# usage python unwxapkg.py filename
import sys,os
import struct
 
class WxapkgFile:
  nameLen = 0
  name = ""
  offset = 0
  size = 0

if len(sys.argv) < 2:
  print 'usage: unwxapkg.py filename'
  exit()

with open(sys.argv[1], "rb") as f:
 
  root = os.path.dirname(os.path.realpath(f.name))
  name = os.path.basename(f.name)

  if len(sys.argv) > 2:
    name = sys.argv[2]
 
  #read header
 
  firstMark = struct.unpack('B', f.read(1))[0]
  print 'first header mark = ' + str(firstMark)
 
  info1 = struct.unpack('>L', f.read(4))[0]
  print 'info1 = ' + str(info1)
 
  indexInfoLength = struct.unpack('>L', f.read(4))[0]
  print 'indexInfoLength = ' + str(indexInfoLength)
 
  bodyInfoLength = struct.unpack('>L', f.read(4))[0]
  print 'bodyInfoLength = ' + str(bodyInfoLength)
 
  lastMark = struct.unpack('B', f.read(1))[0]
  print 'last header mark = ' + str(lastMark)
 
  if firstMark != 0xBE or lastMark != 0xED:
    print 'its not a wxapkg file!!!!!'
    exit()
 
  fileCount = struct.unpack('>L', f.read(4))[0]
  print 'fileCount = ' + str(fileCount)
 
  #read index
 
  fileList = []
 
  for i in range(fileCount):
 
    data = WxapkgFile()
    data.nameLen = struct.unpack('>L', f.read(4))[0]
    data.name = f.read(data.nameLen)
    data.offset = struct.unpack('>L', f.read(4))[0]
    data.size = struct.unpack('>L', f.read(4))[0]
 
    print 'readFile = ' + data.name + ' at Offset = ' + str(data.offset)
 
    fileList.append(data)
 
  #save files
   
  for d in fileList:

    dir = os.path.dirname(root + d.name)
    path = root + d.name
    if not os.path.exists(dir):
        os.makedirs(dir)
    try:
        w = open(path, 'w')
        print "w:", w
        f.seek(d.offset)
        w.write(f.read(d.size))
        w.close()
        print "写入成功 :", path

    except Exception as e:
        print "写入失败:", path
 

f.close()

@xunyixiangchao
Copy link

@seozed 你这个改动,最后都是写入失败,虽然创建了文件,但都是空啊。

@piao6236703
Copy link

usage python unwxapkg.py filename 请问这个路径怎么拼,我这样执行不对么
image
image

@xunyixiangchao
Copy link

@piao6236703 你这是版本问题,2的 print 'xxx', 3的print('xxx')

@wenxingxing
Copy link

wenxingxing commented Jan 3, 2018

稍微改了一下应该可以用

#!/usr/bin/python

# usage python unwxapkg.py filename

import sys, os
import struct


class WxapkgFile:
    nameLen = 0
    name = ""
    offset = 0
    size = 0


if len(sys.argv) < 2:
    print 'usage: unwxapkg.py filename'
    exit()

with open(sys.argv[1], "rb") as f:

    root = os.path.dirname(os.path.realpath(f.name))
    name = os.path.basename(f.name)

    if len(sys.argv) > 2:
        name = sys.argv[2]

    #read header

    firstMark = struct.unpack('B', f.read(1))[0]
    print 'first header mark = ' + str(firstMark)

    info1 = struct.unpack('>L', f.read(4))[0]
    print 'info1 = ' + str(info1)

    indexInfoLength = struct.unpack('>L', f.read(4))[0]
    print 'indexInfoLength = ' + str(indexInfoLength)

    bodyInfoLength = struct.unpack('>L', f.read(4))[0]
    print 'bodyInfoLength = ' + str(bodyInfoLength)

    lastMark = struct.unpack('B', f.read(1))[0]
    print 'last header mark = ' + str(lastMark)

    if firstMark != 0xBE or lastMark != 0xED:
        print 'its not a wxapkg file!!!!!'
        exit()

    fileCount = struct.unpack('>L', f.read(4))[0]
    print 'fileCount = ' + str(fileCount)

    #read index

    fileList = []

    for i in range(fileCount):

        data = WxapkgFile()
        data.nameLen = struct.unpack('>L', f.read(4))[0]
        data.name = f.read(data.nameLen)
        data.offset = struct.unpack('>L', f.read(4))[0]
        data.size = struct.unpack('>L', f.read(4))[0]

        print 'readFile = ' + data.name + ' at Offset = ' + str(data.offset)

        fileList.append(data)

    #save files

    for d in fileList:
        path = root + '/' + name + '_'
        file_ = path + d.name

        if not os.path.exists(os.path.dirname(file_)):
            os.makedirs(os.path.dirname(file_))

        w = open(file_, 'w')
        f.seek(d.offset)
        w.write(f.read(d.size))
        w.close()

        print 'writeFile = ' + file_

    f.close()

@chenjiancan
Copy link

写入文件语句 w = open(file_, 'w') 改为 w = open(file_, 'wb') 资源文件是二进制的格式需要用二进制方式操作,否则会出现图片和音乐格式错误

@zergtant
Copy link

zergtant commented Jan 3, 2018

fork了一版python3的,亲测可用,需要的亲自下,感谢作者

@yljphp
Copy link

yljphp commented Jan 4, 2018

raceback (most recent call last):
File "index.py", line 30, in
firstMark = struct.unpack('B', f.read(1))[0]
struct.error: unpack requires a string argument of length 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment