Gzip压缩和解压
来源:互联网 发布:2048代码 java 编辑:程序博客网 时间:2024/05/17 03:38
转帖地址:
http://www.cnblogs.com/qin0773/archive/2009/06/10/1499864.html里面有详细的一套gzip压缩和解压的代码
http://www.cnblogs.com/dajianshi/archive/2008/03/18/1111180.html
压缩代码:
public static byte[] Compress(string s)
{
byte[] buf = System.Text.Encoding.UTF8.GetBytes(s);
MemoryStream ms = new MemoryStream();
byte[] rb;
GZipStream gzip = new GZipStream(ms, CompressionMode.Compress, true);
gzip.Write(buf, 0, buf.Length);
gzip.Flush();
gzip.Close();//必须在ms读出之前关闭掉
ms.Position = 0;
rb = new byte[ms.Length];
ms.Read(rb, 0, (int)ms.Length);
ms.Close();
return rb;
}
解压代码:
public static string Decompress(byte[] buf)
{
long totalLength = 0;
int size = 0;
MemoryStream ms = new MemoryStream(), msD = new MemoryStream();
ms.Write(buf, 0, buf.Length);
ms.Seek(0, SeekOrigin.Begin);
GZipStream zip;
zip = new GZipStream(ms, CompressionMode.Decompress);
byte[] db;
bool readed = false;
while (true)
{
size = zip.ReadByte();
if (size != -1)
{
if (!readed) readed = true;
totalLength++;
msD.WriteByte((byte)size);
}
else
{
if (readed) break;
}
}
zip.Close();
db = msD.ToArray();
msD.Close();
return System.Text.Encoding.UTF8.GetString(db);
}
=======================================================
/// 使用GZipStream压缩数据
public static byte[] CompressionData(byte[] input)
{
byte[] temp = null;
try
{
using (MemoryStream ms = new MemoryStream())
{
using (GZipStream compressStream = new GZipStream(ms, CompressionMode.Compress, true))
{
// 写入目标流
compressStream.Write(input, 0, input.Length);
}
temp = ms.ToArray();
}
}
catch (Exception ex)
{
throw ex;
}
return temp;
}
/// 解压缩数据
public static byte[] DecompressionData(byte[] input)
{
byte[] temp = null;
try
{
using (MemoryStream baseData = new MemoryStream())
{
MemoryStream rmstemp = new MemoryStream(input);
using (GZipStream DecompressString = new GZipStream(rmstemp, CompressionMode.Decompress))
{
byte[] buff = new byte[4096];
int n;
while ((n = DecompressString.Read(buff, 0, buff.Length)) != 0)
{
baseData.Write(buff, 0, n);
}
}
rmstemp.Dispose();
rmstemp.Close();
temp = baseData.ToArray();
}
}
catch (Exception ex)
{
temp = null;
throw ex;
}
return temp;
}
- gzip解压和压缩
- Gzip压缩和解压
- GZIP压缩和解压
- Gzip解压和压缩
- iOS gzip 压缩和解压
- 用GZip压缩和解压(.Net)
- C# GZip对字符串压缩和解压
- C# GZip对字符串压缩和解压
- gzip, StringIO的压缩和解压
- GZip对字符串压缩和解压
- Gzip压缩和解压文件工具类
- 使用zlib压缩和解压gzip
- C# GZip对字符串压缩和解压
- Java实现Gzip的压缩和解压
- Java使用GZIP进行压缩和解压
- GZIP压缩解压类
- Gzip压缩数据解压
- GZIP压缩解压类
- 关于浏览器下载文件的临时目录
- GTF
- DBCC DBREINDEX重建索引提高SQL Server性能
- C|C++中的静态全局变量,静态局部变量,全局变量,局部变量的区别
- 随机数
- Gzip压缩和解压
- 笑话
- 问题
- 实用的google安全浏览诊断工具
- C#全局变量的使用
- 将vs2008 项目与 VS2005 相互转换解决方法
- 嵌入式怎么开始
- jQuery资料网址整理
- OPENFILENAME 说明