C# 中压缩与解压缩的方法
来源:互联网 发布:淘宝语嫣 编辑:程序博客网 时间:2024/04/28 07:58
/// <summary>
/// 压缩流
/// </summary>
/// <param name="sourceStream"></param>
/// <returns></returns>
public static Stream DecompressStream(Stream sourceStream)
{
MemoryStream responseStream = new MemoryStream();
using (GZipStream compressedStream = new GZipStream(responseStream, CompressionMode.Compress, true))
{
byte[] buffer = new byte[sourceStream.Length];
int checkCounter = sourceStream.Read(buffer, 0, buffer.Length);
if (checkCounter != buffer.Length) throw new ApplicationException();
compressedStream.Write(buffer, 0, buffer.Length);
}
responseStream.Position = 0;
return responseStream;
}
/// <summary>
/// 解压缩流
/// </summary>
/// <param name="sourceStream"></param>
/// <returns></returns>
public static Stream UncompressStream(Stream sourceStream)
{
MemoryStream destinationStream = new MemoryStream();
byte[] quartetBuffer = new byte[4];
int position = (int)sourceStream.Length - 4;
sourceStream.Position = position;
sourceStream.Read(quartetBuffer, 0, 4);
sourceStream.Position = 0;
int checkLength = BitConverter.ToInt32(quartetBuffer, 0);
byte[] buffer = new byte[checkLength + 100];
using (GZipStream decompressedStream = new GZipStream(sourceStream, CompressionMode.Decompress, true))
{
int total = 0;
for (int offset = 0; ; )
{
int bytesRead = decompressedStream.Read(buffer, offset, 100);
if (bytesRead == 0) break;
offset += bytesRead;
total += bytesRead;
}
destinationStream.Write(buffer, 0, total);
}
destinationStream.Position = 0;
return destinationStream;
}
- C# 中压缩与解压缩的方法
- C#中压缩/解压缩7-zip文件的方法
- C#对ZIP的压缩与解压缩
- 常见的“压缩与解压缩”方法...
- C#压缩与解压缩文件
- c#字符串压缩与解压缩
- C# Deflate 压缩与解压缩
- C#实现压缩与解压缩
- C# 文件压缩与解压缩
- C#实现.rar的动态压缩与解压缩
- C#压缩与解压缩流类 GZipStream 的使用
- Tar打包、压缩与解压缩到指定目录的方法
- Tar打包、压缩与解压缩到指定目录的方法
- Tar打包、压缩与解压缩到指定目录的方法
- Tar打包、压缩与解压缩到指定目录的方法
- Tar打包、压缩与解压缩到指定目录的方法
- Tar打包、压缩与解压缩到指定目录的方法
- Tar打包、压缩与解压缩到指定目录的方法
- weblogic10.3 java.lang.ClassCastException: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl
- 中国剩余定理简介、算例及其应用
- DXGridView 添加checkbox样式列
- 什么是博士?
- 刷新别人网页的方法和防止被别人恶意刷新网页
- C# 中压缩与解压缩的方法
- 对敏捷开发的误解
- jsp页面显示新闻.公告之类的上一篇下一篇
- F#入门-第二章 F#基础-第七节 条件分歧
- 《云计算应用架构》连载一:什么是云?
- 一个死锁的脚本(谁锁谁)
- 用JavaScript调用FCKeditor
- Sample Post From a File
- 用户体验!=挑剔的美工设计