更新 'plugins/gzip.js'

master
src48597962 8 months ago
parent ec45347209
commit 09f5efbbea
  1. 132
      plugins/gzip.js

@ -10,78 +10,7 @@ const ByteArrayOutputStream = java.io.ByteArrayOutputStream;
const ByteArrayInputStream = java.io.ByteArrayInputStream;
const Deflater = java.util.zip.Deflater;
//远程依赖
//require("https://cdn.bootcdn.net/ajax/libs/pako/2.1.0/pako.es5.min.js");
//仓库:
require("http://hiker.nokia.press/hikerule/rulelist.json?id=6974");
//javascript
function uint8ArrayToBase64(uint8Array) {
var binaryString = String.fromCharCode.apply(null, Array.from(uint8Array));
return window0.btoa(binaryString);
}
function zip(str, mode) {
mode = mode ? mode : "gzip";
var arr = []
if (mode == "gzip") {
arr = pako.gzip((str), {
to: 'string'
});
} else if (mode == "deflate") {
arr = pako.deflate((str), {
to: 'string'
});
}
return uint8ArrayToBase64(arr);
}
function unzip(b64Data) {
let strData = window0.atob(b64Data);
const charData = strData.split('').map(function (x) {
return x.charCodeAt(0);
});
const binData = new Uint8Array(charData);
const data = pako.inflate(binData);
//strData = String.fromCharCode.apply(null, new Uint16Array(data));
return Utf8ArrayToStr(data);
}
function Utf8ArrayToStr(array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while (i < len) {
c = array[i++];
switch (c >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
out += String.fromCharCode(c);
break;
case 12:
case 13:
char2 = array[i++];
out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
break;
case 14:
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(
((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
);
break;
}
}
return out;
}
//javascript
function zipForJava(text, mode) {
function zip(text, mode) {
mode = mode ? mode : "gzip";
let baseStr = new javaString(text);
// 使用 ByteArrayOutputStream 来捕获压缩后的数据
@ -107,7 +36,7 @@ function zipForJava(text, mode) {
return String(base64String);
}
function unzipForJava(text, mode) {
function unzip(text, mode) {
mode = mode ? mode : "gzip";
var compressedData = Base64.getDecoder().decode(text);
var bais = new ByteArrayInputStream(compressedData);
@ -134,58 +63,7 @@ function unzipForJava(text, mode) {
// 打印解压缩后的字符串
return String(decompressedString);
}
const Gzip = {
islog: false,
env: "java",
mode: "gzip",
log: function (input) {
if (this.islog) {
log(input)
}
},
zip(text, mode) {
this.log("----------分割线----------")
this.log("方法:压缩")
this.log("模式:" + this.mode)
this.log("文本数量:" + text.length)
var s = new Date().getTime();
var result;
if (this.env == "java") {
this.log("环境:" + this.env)
result = zipForJava(text, mode || this.mode)
var e = new Date().getTime();
this.log("耗时:" + (e - s))
this.log("压缩后:" + result.length)
return result;
}
this.log("环境:" + this.env)
result = zip(text, mode || this.mode);
var e = new Date().getTime();
this.log("耗时:" + (e - s))
this.log("压缩后:" + result.length)
return result;
},
unzip(text, mode) {
this.log("----------分割线----------")
this.log("方法:解压")
this.log("模式:" + this.mode)
this.log("文本数量:" + text.length)
var s = new Date().getTime();
var result;
if (this.env == "java") {
this.log("环境:" + this.env)
result = unzipForJava(text, mode || this.mode);
var e = new Date().getTime();
this.log("耗时:" + (e - s))
this.log("解压后:" + result.length)
return result;
}
this.log("环境:" + this.env)
result = unzip(text, mode || this.mode);
var e = new Date().getTime();
this.log("耗时:" + (e - s))
this.log("解压后:" + result.length)
return result
}
$.exports = {
zip,
unzip
};
Loading…
Cancel
Save