-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.html
229 lines (196 loc) · 6.16 KB
/
index.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html manifest="page.appcache">
<head>
<title>Unrar</title>
<meta name="viewport" content="width=device-width"/>
</head>
<body>
<h1>Unrar</h1>
<div id="loadStatus">Loading....</div>
<div id="drop-zone">Drop RAR file(s) here</div><br>
Or select from here: <input type="file" id="file" name="file" multiple/><br><br>
Password: <input id="password"/><br>
<button id="unrarBtn">Unrar</button>
<div id="output">
<ul></ul>
</div>
<style>
#drop-zone {
border: 2px dashed rgb(187, 187, 187);
border-radius: 5px 5px 5px 5px;
padding: 50px;
text-align: center;
color: rgb(87, 87, 87);
}
</style>
<!--polyfill for IE and older browser-->
<script src="Promise.min.js"></script>
<script src="download.js"></script>
<script src="jszip.min.js"></script>
<script src="rpc.js"></script>
<script>
"use strict";
var startTime
var filesToUnrar = []
var loaded = function() {
document.querySelector("#loadStatus").innerHTML = "Finish loading"
}
var progressShow = function(fileName, fileSize, progress) {
document.querySelector("#loadStatus").innerHTML = fileName + ' (' + (100 * progress / fileSize).toFixed(2) + '%)'
}
var errShow = function(errMsg) {
document.querySelector("#loadStatus").innerHTML = "<strong>" + errMsg + "</strong>"
}
var startUnrar = function(){
// sort by name
filesToUnrar.sort(function(a, b){
if(a.name > b.name) return 1
else if(a.name < b.name) return -1
else return 0
})
var buffers = []
var dataToPass = []
var reader = new FileReader();
reader.onload = callback
function callback(e) {
var data = e.target.result;
buffers.push(data)
dataToPass.push({name: filesToUnrar[cnt].name, content: data})
cnt += 1
if(cnt < filesToUnrar.length) {
//continue reading the next vol
document.querySelector("#loadStatus").innerHTML = "reading your file "+filesToUnrar[cnt].name
reader.readAsArrayBuffer(filesToUnrar[cnt]);
return
}
// all files finish reading
document.querySelector("#loadStatus").innerHTML = "finish reading files, start decompressing"
var password = document.querySelector("#password").value || null
startTime = Date.now()
//worker.postMessage({data: data, password: password}, [data]) //transferable object
document.querySelector("#loadStatus").innerHTML = "decompressing"
rpc.transferables = buffers
rpc.unrar(dataToPass, password).then(function(ret){
document.querySelector("#loadStatus").innerHTML = "Finish decompression, used " + ((Date.now() - startTime) / 1000).toFixed(2) + " s"
console.log(ret)
var rootListElem = document.querySelector("#output ul")
rootListElem.innerHTML = "" //clear
var filesOnly = []
var rec = function(key, entry) {
var li = document.createElement("li")
if (entry.type === 'dir') {
var ul = document.createElement('ul')
Object.keys(entry.ls).forEach(function(k) {
ul.appendChild(rec(k, entry.ls[k]))
})
li.appendChild(document.createTextNode(key))
li.appendChild(ul)
} else if (entry.type === 'file') {
filesOnly.push(entry)
var a = document.createElement('a')
a.appendChild(document.createTextNode(entry.fullFileName))
var fn = entry.fullFileName.split('/').pop()
a.download = fn
var blob = new Blob([entry.fileContent]);
var url = URL.createObjectURL(blob);
a.href = url
a.addEventListener('click', function(evt) {
evt.preventDefault()
download(blob, fn)
})
var span = document.createElement('span')
span.appendChild(document.createTextNode(' File Size: ' + entry.fileSize))
li.appendChild(a)
li.appendChild(span)
} else {
throw "unknown type"
}
return li
}
Object.keys(ret.ls).forEach(function(k) {
rootListElem.appendChild(rec(k, ret.ls[k]))
})
if(filesOnly.length > 1){
var lida = document.createElement('li')
var ada = document.createElement('a')
ada.appendChild(document.createTextNode("Download All as zip file"))
ada.href = '#'
ada.addEventListener('click', function(evt){
evt.preventDefault()
try {
var zip = new JSZip();
filesOnly.forEach(function(e) {
zip.file(e.fullFileName, e.fileContent)
})
var content = zip.generate({type:"blob"});
// see FileSaver.js
download(content, "all.zip");
} catch(e) {
alert(e.toString())
}
})
lida.appendChild(ada)
rootListElem.appendChild(lida)
}
}).catch(errShow)
};
var cnt = 0
if(filesToUnrar.length > 0){
document.querySelector("#loadStatus").innerHTML = "reading your file "+filesToUnrar[cnt].name
reader.readAsArrayBuffer(filesToUnrar[cnt]);
} else {
errShow("No file selected")
}
}
var rpc
var so = {loaded:loaded, progressShow:progressShow}
RPC.new("./worker.js", so).then(function(r) {
rpc = r
})
var dropzone = document.querySelector('#drop-zone');
dropzone.addEventListener('dragover', function(event) {
//var event = jqEvent.originalEvent;
event.stopPropagation();
event.preventDefault();
event.dataTransfer.dropEffect = 'copy';
return false
});
dropzone.addEventListener('dragend', function(event) {
event.stopPropagation();
event.preventDefault();
return false
})
dropzone.addEventListener('drop', function(event) {
//var event = jqEvent.originalEvent;
event.stopPropagation();
event.preventDefault();
var files = event.dataTransfer.files
if (files.length === 0) {
return false;
}
// we want array not file list
filesToUnrar = []
for(var i = 0; i < files.length; i++){
filesToUnrar.push(files[i])
}
//filesToUnrar = files
document.querySelector("#loadStatus").innerHTML = "Selected "+filesToUnrar[0].name
startUnrar()
return false
});
var setFileBtn = function(evt){
var files = evt.target.files;
if(files.length === 0) return
// we want array not file list
filesToUnrar = []
for(var i = 0; i < files.length; i++){
filesToUnrar.push(files[i])
}
//filesToUnrar = files
document.querySelector("#loadStatus").innerHTML = "Click the unrar button"
}
document.getElementById('file').addEventListener('change', setFileBtn, false);
document.getElementById('unrarBtn').addEventListener('click', startUnrar, false);
</script>
</body>
</html>