2024年6月14日发(作者:)

GZip、Deflate压缩算法对应的C#压缩解压函数

///

/// GZip解压函数

///

///

///

public byte[] GZipDecompress(byte[] data)

{

using (MemoryStream stream = new MemoryStream())

{

using (GZipStream gZipStream = new

GZipStream(new MemoryStream(data), ress))

{

byte[] bytes = new byte[40960];

int n;

while ((n =

(bytes, 0, )) != 0)

{

(bytes, 0,

n);

}

();

}

return y();

}

}

///

/// GZip压缩函数

///

///

///

public byte[] GZipCompress(byte[] data)

{

using (MemoryStream stream = new MemoryStream())

{

using (GZipStream gZipStream = new

GZipStream(stream, ss))

{

(data, 0,

);

();

}

return y();

}

}

///

/// Deflate解压函数

/// JS:var details = eval('(' +

utf8to16(zip_depress(base64decode())) + ')')对应的C#压

缩方法

///

///

///

public string DeflateDecompress(string strSource)

{

byte[] buffer =

se64String(strSource);

using (Stream ms = new

Stream())

{

(buffer, 0, );

on = 0;

using

(eStream stream = new

eStream(ms,

ress))

{

();

int nSize = 16 * 1024 +

256; //假设字符串不会超过16K

byte[] decompressBuffer = new

byte[nSize];

int nSizeIncept =

(decompressBuffer, 0, nSize);

();

return

ing(decompressBuffer, 0,

nSizeIncept); //转换为普通的字符串

}

}

}

///

/// Deflate压缩函数

///

///

///

public string DeflateCompress(string strSource)

{

if (strSource == null || > 8 *

1024)

throw new ntException("字

符串为空或长度太大!");

byte[] buffer =

es(strSource);

using (Stream ms = new

Stream())

{

using

(eStream stream = new

eStream(ms,

ss, true))

{

(buffer, 0,

);

();

}

byte[] compressedData = y();

();

return

64String(compressedData); //将压缩后的byte[]

转换为Base64String

}

}