gpt4 book ai didi

javascript - ZLIB 解压缩 - 客户端

转载 作者:可可西里 更新时间:2023-11-01 01:36:47 25 4
gpt4 key购买 nike

我正在接收作为“ZLIB”压缩输入流的数据。

使用 Javascript/Ajax/JQuery,我需要在客户端解压它。

有办法吗?

我已经在 J​​AVA 中进行了如下操作,但需要在客户端执行此操作。

url = new URL(getCodeBase(), dataSrcfile); 
URLConnection urlConn = url.openConnection();
urlConn.setUseCaches(false);
InputStream in = urlConn.getInputStream();
InflaterInputStream inflate = new InflaterInputStream(in);
InputStreamReader inputStreamReader = new InputStreamReader(inflate);
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufReader = new BufferedReader(inputStreamReader);
// Read until no more '#'
int i = 0;
int nHidden = 0;
String line1;
do //------------------------Parsing Starts Here
{
line1 = bufReader.readLine();
.............
...... so on

最佳答案

Pako是一个完整而现代的 Zlib 端口。

这是一个非常简单的示例,您可以从那里开始工作。

获取pako.js你可以像这样解压 byteArray:

<html>
<head>
<title>Gunzipping binary gzipped string</title>
<script type="text/javascript" src="pako.js"></script>
<script type="text/javascript">

// Get datastream as Array, for example:
var charData = [31,139,8,0,0,0,0,0,0,3,5,193,219,13,0,16,16,4,192,86,214,151,102,52,33,110,35,66,108,226,60,218,55,147,164,238,24,173,19,143,241,18,85,27,58,203,57,46,29,25,198,34,163,193,247,106,179,134,15,50,167,173,148,48,0,0,0];

// Turn number array into byte-array
var binData = new Uint8Array(charData);

// Pako magic
var data = pako.inflate(binData);

// Convert gunzipped byteArray back to ascii string:
var strData = String.fromCharCode.apply(null, new Uint16Array(data));

// Output to console
console.log(strData);

</script>
</head>
<body>
Open up the developer console.
</body>
</html>

运行示例:http://jsfiddle.net/9yH7M/

或者,您可以在发送之前对数组进行 base64 编码,因为在以 JSON 或 XML 格式发送时,数组会占用大量开销。同样解码:

// Get some base64 encoded binary data from the server. Imagine we got this:
var b64Data = 'H4sIAAAAAAAAAwXB2w0AEBAEwFbWl2Y0IW4jQmziPNo3k6TuGK0Tj/ESVRs6yzkuHRnGIqPB92qzhg8yp62UMAAAAA==';

// Decode base64 (convert ascii to binary)
var strData = atob(b64Data);

// Convert binary string to character-number array
var charData = strData.split('').map(function(x){return x.charCodeAt(0);});

// Turn number array into byte-array
var binData = new Uint8Array(charData);

// Pako magic
var data = pako.inflate(binData);

// Convert gunzipped byteArray back to ascii string:
var strData = String.fromCharCode.apply(null, new Uint16Array(data));

// Output to console
console.log(strData);

运行示例:http://jsfiddle.net/9yH7M/1/

要更高级,这里是 pako API documentation .

关于javascript - ZLIB 解压缩 - 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4507316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com