Create a gist now

Instantly share code, notes, and snippets.

Embed
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

This comment has been minimized.

Show comment
Hide comment
@seozed

seozed Jan 2, 2018

现在包的结构好像不一样了?

readFile = /res/second_door.png at Offset = 1040043
readFile = /res/shadow.png at Offset = 1040172
readFile = /res/sing.mp3 at Offset = 1043030
readFile = /res/sing.png at Offset = 1218259
readFile = /res/start.mp3 at Offset = 1255521
readFile = /res/stool_shadow.png at Offset = 1349606
readFile = /res/stool.png at Offset = 1350007
readFile = /res/store_bottom.png at Offset = 1417457
readFile = /res/store_top.png at Offset = 1417857
readFile = /res/store.mp3 at Offset = 1419563
readFile = /res/stripe.png at Offset = 1481003
readFile = /res/suc.png at Offset = 1481096
readFile = /res/success.mp3 at Offset = 1481774
readFile = /res/tired.png at Offset = 1523614
readFile = /res/tit.png at Offset = 1570337
readFile = /res/title.png at Offset = 1645322
readFile = /res/top.png at Offset = 1667024
readFile = /res/water.mp3 at Offset = 1668356
readFile = /res/well.png at Offset = 1725888
readFile = /res/westore_desk.png at Offset = 1809459
readFile = /res/westore.png at Offset = 1811384
readFile = /res/white_face.png at Offset = 1813702
readFile = /res/white.png at Offset = 1815449
readFile = /res/气泡狗白脸.png at Offset = 1815876
readFile = /game.js at Offset = 1818878
Traceback (most recent call last):
  File "unwxapkg.py", line 73, in <module>
    w = open(root + d.name, 'w')
IOError: [Errno 20] Not a directory: '/mnt/d/temp/wxapp/3.wxapkg/app-config.json'

seozed commented Jan 2, 2018

现在包的结构好像不一样了?

readFile = /res/second_door.png at Offset = 1040043
readFile = /res/shadow.png at Offset = 1040172
readFile = /res/sing.mp3 at Offset = 1043030
readFile = /res/sing.png at Offset = 1218259
readFile = /res/start.mp3 at Offset = 1255521
readFile = /res/stool_shadow.png at Offset = 1349606
readFile = /res/stool.png at Offset = 1350007
readFile = /res/store_bottom.png at Offset = 1417457
readFile = /res/store_top.png at Offset = 1417857
readFile = /res/store.mp3 at Offset = 1419563
readFile = /res/stripe.png at Offset = 1481003
readFile = /res/suc.png at Offset = 1481096
readFile = /res/success.mp3 at Offset = 1481774
readFile = /res/tired.png at Offset = 1523614
readFile = /res/tit.png at Offset = 1570337
readFile = /res/title.png at Offset = 1645322
readFile = /res/top.png at Offset = 1667024
readFile = /res/water.mp3 at Offset = 1668356
readFile = /res/well.png at Offset = 1725888
readFile = /res/westore_desk.png at Offset = 1809459
readFile = /res/westore.png at Offset = 1811384
readFile = /res/white_face.png at Offset = 1813702
readFile = /res/white.png at Offset = 1815449
readFile = /res/气泡狗白脸.png at Offset = 1815876
readFile = /game.js at Offset = 1818878
Traceback (most recent call last):
  File "unwxapkg.py", line 73, in <module>
    w = open(root + d.name, 'w')
IOError: [Errno 20] Not a directory: '/mnt/d/temp/wxapp/3.wxapkg/app-config.json'
@seozed

This comment has been minimized.

Show comment
Hide comment
@seozed

seozed 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()

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

This comment has been minimized.

Show comment
Hide comment
@xunyixiangchao

xunyixiangchao Jan 2, 2018

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

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

@piao6236703

This comment has been minimized.

Show comment
Hide comment
@piao6236703

piao6236703 Jan 2, 2018

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

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

@xunyixiangchao

This comment has been minimized.

Show comment
Hide comment
@xunyixiangchao

xunyixiangchao Jan 2, 2018

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

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

@wenxingxing

This comment has been minimized.

Show comment
Hide comment
@wenxingxing

wenxingxing 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()

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

This comment has been minimized.

Show comment
Hide comment
@chenjiancan

chenjiancan Jan 3, 2018

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

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

@zergtant

This comment has been minimized.

Show comment
Hide comment
@zergtant

zergtant Jan 3, 2018

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

zergtant commented Jan 3, 2018

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

@yljphp

This comment has been minimized.

Show comment
Hide comment
@yljphp

yljphp 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

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