C# GZip、Deflate 压缩解压算法函数
来源:互联网 发布:ubuntu安装软件不成功 编辑:程序博客网 时间:2024/04/28 03:49
/// <summary>
/// GZip解压函数
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public byte[] GZipDecompress(byte[] data)
{
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress))
{
byte[] bytes = new byte[40960];
int n;
while ((n = gZipStream.Read(bytes, 0, bytes.Length)) != 0)
{
stream.Write(bytes, 0, n);
}
gZipStream.Close();
}
return stream.ToArray();
}
}
/// <summary>
/// GZip压缩函数
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public byte[] GZipCompress(byte[] data)
{
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
{
gZipStream.Write(data, 0, data.Length);
gZipStream.Close();
}
return stream.ToArray();
}
}
/// <summary>
/// Deflate解压函数
/// JS:var details = eval_r('(' + utf8to16(zip_depress(base64decode(hidEnCode.value))) + ')')对应的C#压缩方法
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
public string DeflateDecompress(string strSource)
{
byte[] buffer = Convert.FromBase64String(strSource);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ms.Write(buffer, 0, buffer.Length);
ms.Position = 0;
using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress))
{
stream.Flush();
int nSize = 16 * 1024 + 256; //假设字符串不会超过16K
byte[] decompressBuffer = new byte[nSize];
int nSizeIncept = stream.Read(decompressBuffer, 0, nSize);
stream.Close();
return System.Text.Encoding.UTF8.GetString(decompressBuffer, 0, nSizeIncept); //转换为普通的字符串
}
}
}
/// <summary>
/// Deflate压缩函数
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
public string DeflateCompress(string strSource)
{
if (strSource == null || strSource.Length > 8 * 1024)
throw new System.ArgumentException("字符串为空或长度太大!");
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strSource);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress, true))
{
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
byte[] compressedData = ms.ToArray();
ms.Close();
return Convert.ToBase64String(compressedData); //将压缩后的byte[]转换为Base64String
}
}
/// GZip解压函数
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public byte[] GZipDecompress(byte[] data)
{
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream gZipStream = new GZipStream(new MemoryStream(data), CompressionMode.Decompress))
{
byte[] bytes = new byte[40960];
int n;
while ((n = gZipStream.Read(bytes, 0, bytes.Length)) != 0)
{
stream.Write(bytes, 0, n);
}
gZipStream.Close();
}
return stream.ToArray();
}
}
/// <summary>
/// GZip压缩函数
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public byte[] GZipCompress(byte[] data)
{
using (MemoryStream stream = new MemoryStream())
{
using (GZipStream gZipStream = new GZipStream(stream, CompressionMode.Compress))
{
gZipStream.Write(data, 0, data.Length);
gZipStream.Close();
}
return stream.ToArray();
}
}
/// <summary>
/// Deflate解压函数
/// JS:var details = eval_r('(' + utf8to16(zip_depress(base64decode(hidEnCode.value))) + ')')对应的C#压缩方法
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
public string DeflateDecompress(string strSource)
{
byte[] buffer = Convert.FromBase64String(strSource);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
ms.Write(buffer, 0, buffer.Length);
ms.Position = 0;
using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Decompress))
{
stream.Flush();
int nSize = 16 * 1024 + 256; //假设字符串不会超过16K
byte[] decompressBuffer = new byte[nSize];
int nSizeIncept = stream.Read(decompressBuffer, 0, nSize);
stream.Close();
return System.Text.Encoding.UTF8.GetString(decompressBuffer, 0, nSizeIncept); //转换为普通的字符串
}
}
}
/// <summary>
/// Deflate压缩函数
/// </summary>
/// <param name="strSource"></param>
/// <returns></returns>
public string DeflateCompress(string strSource)
{
if (strSource == null || strSource.Length > 8 * 1024)
throw new System.ArgumentException("字符串为空或长度太大!");
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(strSource);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
{
using (System.IO.Compression.DeflateStream stream = new System.IO.Compression.DeflateStream(ms, System.IO.Compression.CompressionMode.Compress, true))
{
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
byte[] compressedData = ms.ToArray();
ms.Close();
return Convert.ToBase64String(compressedData); //将压缩后的byte[]转换为Base64String
}
}
- C# GZip、Deflate 压缩解压算法函数
- GZip、Deflate压缩算法对应的C#压缩解压函数
- C# Deflate、GZip压缩与解压缩
- web中gzip,deflate的压缩与解压
- GZip 和 deflate压缩
- c#压缩解压缩bzip2、tar、zip、gzip、deflate、ntdll
- .NET GZIP,deflate压缩(转)
- c# Deflate压缩与解压缩函数
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- Snappy,Lzo,bzip2,gzip,deflate文件解压
- gzip,zip压缩格式和png图像格式的本质压缩算法----DEFLATE及其实现:zlib
- DEFLATE 压缩算法
- DEFLATE 压缩算法
- DEFLATE 压缩算法
- ActionFilterAttribute之CompressFilter,GZIP,DEFLATE压缩
- [原]Nodejs关于gzip/deflate压缩
- AT&T汇编语言与GCC内嵌汇编简介
- PHP: How to easily provide JSON and JSONP
- Hadoop下远程调试Child子进程 --- 原创
- 煮酒论英雄
- 蒸豆腐
- C# GZip、Deflate 压缩解压算法函数
- 从实体机到xen,一个企业信息管理平台主机系统的变迁之路
- Vim实战手册(7)关于加密
- SAP NetWeaver Application Server ABAP 7.02 SP6 Developer Version Download URL
- Intel VMM简介
- DugTV对vmm
- 写了个类继承CCSprite,结果用不了spriteWithFile 这个方法。
- libgdx示例-SuperJumper分析 1. 程序入口与初始化资源,加载配置信息
- 在 Mac OS X 中启用和使用“root”用户