-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.html
209 lines (197 loc) · 7.67 KB
/
unzip.html
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<html>
<head>
<meta charset="UTF-8"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
</head>
<body>
<label for="file-input">Choose a file</label>
<input id="file-input" type="file"></input>
<pre id="hexdump"> </pre>
<div id="tree"></div>
<div id="using_json_2"></div>
<script>
var input = document.getElementById ("file-input"); // get the input element
var hexdump = document.getElementById ("hexdump"); // get the input element
function hexDump(bs){
let hex = "";
for (let i = 0; i < bs.length; i++) {
// Get ASCII code of each character
let code = bs.charCodeAt(i);
// Convert to hexadecimal and pad with zeros
hex += code.toString(16).padStart(2, "0");
}
// Print hexadecimal in formatted way
let output = "";
for (let i = 0; i < hex.length; i += 32) {
// Get address of each line
let address = (i / 2).toString(16).padStart(8, "0");
// Get hexadecimal bytes of each line
let bytes = hex.slice(i, i + 32);
// Add spaces between every two bytes
bytes = bytes.replace(/(..)/g, "$1 ");
// Get ASCII characters of each line
let chars = "";
for (let j = i / 2; j < (i + 32) / 2; j++) {
// Get ASCII code of each byte
let code = parseInt(hex.slice(j * 2, j * 2 + 2), 16);
// Check if printable character or dot
chars += code >= 32 && code <= 126 ? String.fromCharCode(code) : ".";
}
// Add address, bytes and chars to output
output += `${address} ${bytes} ${chars}\n`;
}
return output;
}
function readFileAsync (file, offset, length) {
return new Promise ((resolve, reject) => {
let reader = new FileReader (); // create a FileReader object
reader.onload = () => { // set the onload handler
resolve (reader.result); // resolve with the result
};
var size = file.size; // get the total number of bytes in the file
var blob
if (offset < 0){
blob = file.slice (size + offset, size + offset + length); // create a blob that contains only the last 16 bytes
} else {
blob = file.slice (offset, offset + length); // create a blob that contains only the last 16 bytes
}
reader.onerror = reject; // reject on error
reader.readAsBinaryString(blob)
});
}
function handleFileSelect () {
var input = document.getElementById ("file-input"); // get the input element
var fileName = input.value; // get the name of the selected file
var fileList = input.files; // get a FileList object
var file = fileList[0]; // get the first File object
console.log (fileName); // do something with it
console.log (file); // do something with it
var reader = new FileReader (); // create a FileReader object
var size = file.size; // get the total number of bytes in the file
var blob = file.slice (size - 16, size); // create a blob that contains only the last 16 bytes
reader.readAsBinaryString (blob); // read the blob as a binary string
reader.addEventListener ("loadend", async function () {
var result = reader.result; // get the result as a binary string
console.log ("loadend", result); // do something with it
});
}
function str2ab(str) {
var buf = new ArrayBuffer(str.length); // 1 byte for each char
var bufView = new Uint8Array(buf);
for (var i=0, strLen=str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
var nodeid = 0
var tree = {
id: "#",
children: {}
}
function tree_append(file_name){
var comps = file_name.split('/')
var cur = tree
for (i in comps){
var tmp = cur.children[comps[i]]
if (!tmp){
tmp = {
id: nodeid++,
children: {}
}
cur.children[comps[i]] = tmp
}
cur = tmp
}
}
function render_tree(current, nodes){
var cnt = 0
for (k in current.children){
nodes.push({ "id" : "" + current.children[k].id, "parent" : "" + current.id, "text" : k, "icon": "./d.png"})
render_tree(current.children[k], nodes)
cnt += 1
}
if (cnt == 0 && nodes.length > 0){
let parent = nodes[nodes.length - 1];
parent['icon'] = "./f.png"
}
}
async function handleFileSelect2(){
nodeid = 0
tree = {
id: "#",
children: {}
}
var input = document.getElementById ("file-input"); // get the input element
var fileName = input.value; // get the name of the selected file
var fileList = input.files; // get a FileList object
var file = fileList[0]; // get the first File object
let zip_tailer = await readFileAsync(file, -22, 22);
// console.log (hexdump(zip_tailer)); // do something with it
// let content2 = await readFileAsync(file, 0, 16);
// console.log (hexdump(content2)); // do something with it
// hexdump.textContent = hexDump(zip_tailer)
let ab = str2ab(zip_tailer)
let dataView = new DataView(ab)
console.log("len:", dataView.getUint32(12, true))
console.log("ofs:", dataView.getUint32(16, true))
console.log("----", dataView.getUint32(4, true))
let central_dir = await readFileAsync(file, dataView.getUint32(16, true), dataView.getUint32(12, true))
// console.log (hexDump(central_dir));
let cd_ab = str2ab(central_dir);
let cd_dv = new DataView(cd_ab)
var pos = 0
console.log(pos, cd_dv.byteLength)
while(pos < cd_dv.byteLength){
let section_type = cd_dv.getUint16(pos + 2, true)
// console.log(`section_type: ${section_type}`)
switch (section_type){
case 0x201:{
var len_file_name = cd_dv.getUint16(pos + 28, true)
var len_extra = cd_dv.getUint16(pos + 30, true)
var len_comment = cd_dv.getUint16(pos + 32, true)
var ofs_file_name = pos + 46
var file_name = central_dir.slice(ofs_file_name, ofs_file_name + len_file_name)
// console.log(`central_dir_entry len_file_name=${len_file_name} len_comment=${len_comment} file_name=${file_name}`);
pos = pos + 46 + len_file_name + len_extra + len_comment
tree_append(file_name)
}
break;
case 0x403:
console.log("0x403");
pos = cd_dv.byteLength
break;
case 0x605:
console.log("0x605");
pos = cd_dv.byteLength
break;
case 0x807:
console.log("0x807");
pos = cd_dv.byteLength
break;
}
}
// console.log(tree)
var data = []
render_tree(tree, data)
// console.log(data)
$('#using_json_2').jstree({ 'core' : {
'data' : data
} });
}
input.addEventListener('change', handleFileSelect2);
</script>
<script>
/*
$('#using_json_2').jstree({ 'core' : {
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
} });
*/
</script>
</body></html>