forked from thedreamwork/unwxapkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (94 loc) · 2.64 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
const _ = require('./util');
const DEFAULT = {
firstMark : [0xBE],
lastMark : [0xED],
}
function wxapkgFileInfo(){
this.nameLen = 0;
this.name = "";
this.offset = 0;
this.size = 0;
}
class Wxapkg{
constructor(file){
this.index = 0; // 解码游标
this.buffer = new Uint8Array(file);
}
/**
* 读取buffer数组的指定字节数
* @param {Number} length 读取长度
* @return {Array} 读取到的数据
*/
readBytes(length) {
let buffer = _.readBytes(this.buffer, this.index, length);
this.index += length;
return buffer;
}
/**
* 解码
* @return {Array} 文件数组
*/
_decode() {
if(!this.buffer) {
throw new Error('不存在待解码数据!');
}
this._decodeHeader(); // 解析头部信息
return this._decodeChunkInfo(); // 解析数据块元数据
}
/**
* 解码输出文件
* @return {Void}
*/
decode() {
let files = [];
let fileInfoList = this._decode();
fileInfoList.forEach((f)=>{
let file = {};
file.chunk = _.readBytes(this.buffer, f.offset, f.size);
file.name = f.name;
files.push(file);
})
return files;
}
/**
* 解码头部信息
* @return {Void}
*/
_decodeHeader() {
if(this.index !== 0) {
throw new Error('index属性指向非0!');
}
this.firstMark = this.readBytes(1);
if(!_.equal(this.firstMark, DEFAULT.firstMark)) {
throw new Error('wxpakg文件错误 - firstMark')
}
this.fileInfo = _.readInt32(this.readBytes(4));
this.indexInfoLength = _.readInt32(this.readBytes(4));
this.bodyInfoLength = _.readInt32(this.readBytes(4));
this.lastMark = this.readBytes(1);
if(!_.equal(this.lastMark, DEFAULT.lastMark)) {
throw new Error('wxpakg文件错误 - lastMark')
}
this.fileCount = _.readInt32(this.readBytes(4));
console.log(`fileCount = ${this.fileCount}`)
}
/**
* 解析元数据
* @return {Array} 数据块元数据数组
*/
_decodeChunkInfo() {
let fileList = [];
for (let i = 0; i < this.fileCount; i++){
let fileInfo = new wxapkgFileInfo;
fileInfo.nameLen = _.readInt32(this.readBytes(4));
fileInfo.name = _.bufferToString(this.readBytes(fileInfo.nameLen))
fileInfo.offset = _.readInt32(this.readBytes(4));
fileInfo.size = _.readInt32(this.readBytes(4));
console.log(`fileName - ${fileInfo.name}`)
fileList.push(fileInfo);
}
return fileList;
}
}
module.exports = Wxapkg;