CompressHelper
public static string CompressString(string unCompressedString)
{
byte[] bytData = System.Text.Encoding.UTF8.GetBytes(unCompressedString);
MemoryStream ms = new MemoryStream();
using (Stream s = new GZipStream(ms, CompressionMode.Compress))
{
s.Write(bytData, 0, bytData.Length);
s.Close();
}
byte[] compressedData = (byte[])ms.ToArray(); return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);
} /// <summary>
/// 解压字符串
///
/// </summary>
/// <param name="unCompressedString"></param>
/// <returns></returns>
public static string DecompressString(string unCompressedString)
{
System.Text.StringBuilder uncompressedString = new System.Text.StringBuilder();
byte[] writeData = new byte[4096]; byte[] bytData = System.Convert.FromBase64String(unCompressedString);
int totalLength = 0;
int size = 0; using (Stream s = new GZipStream(new MemoryStream(bytData), CompressionMode.Decompress))
{
while (true)
{
size = s.Read(writeData, 0, writeData.Length);
if (size > 0)
{
totalLength += size;
uncompressedString.Append(System.Text.Encoding.UTF8.GetString(writeData, 0, size));
}
else
{
break;
}
}
s.Close();
}
return uncompressedString.ToString();
} /// <summary>
/// 提供文件名称列表和压缩后保存的文件名称,对指定文件进行压缩
/// </summary>
/// <param name="fileName">需要压缩的文件列表</param>
/// <param name="compressFileName">压缩后存放的文件名称</param>
/// <returns></returns>
public static bool CompressFiles(IList<string> fileName, string compressFileName)
{
bool result = false;
try
{
ArrayList fileList = new ArrayList(); List<byte[]> byteList = new List<byte[]>();
foreach (string item in fileName)
{
if (File.Exists(item))
{
CompressFileInfo fileInfo = new CompressFileInfo();
fileInfo.FileName = Path.GetFileName(item);
fileInfo.FileBuffer = File.ReadAllBytes(item);
fileList.Add(fileInfo);
}
} IFormatter formatter = new BinaryFormatter();
using (Stream s = new MemoryStream())
{
formatter.Serialize(s, fileList);
s.Position = 0;
CreateCompressFiles(s, compressFileName);
result = true;
}
}
catch (Exception ex)
{
throw ex;
} return result;
}
/// <summary>
/// 将指定的压缩文件进行解压,并输出到指定路径中
/// </summary>
/// <param name="fileName">需要解压的文件全名</param>
/// <param name="outputPath">解压后文件存放路径</param>
/// <returns></returns>
public static bool DecompressFiles(string fileName, string outputPath)
{
bool result = false;
using (Stream source = File.OpenRead(fileName))
{
using (Stream destination = new MemoryStream())
{
using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true))
{
byte[] bytes = new byte[4096];
int n;
while ((n = input.Read(bytes, 0, bytes.Length)) != 0)
{
destination.Write(bytes, 0, n);
}
}
destination.Flush();
destination.Position = 0;
DeserializeFileInfo(destination, outputPath);
result = true; }
} return result;
} /// <summary>
/// 将需要压缩的文件流进行压缩,然后保存到指定的文件下
/// </summary>
/// <param name="sourceStream"></param>
/// <param name="compressFileName"></param>
private static void CreateCompressFiles(Stream sourceStream, string compressFileName)
{
using (Stream destination = new FileStream(compressFileName, FileMode.Create, FileAccess.Write))
{
using (GZipStream output = new GZipStream(destination, CompressionMode.Compress))
{
byte[] bytes = new byte[4096];
int n;
while ((n = sourceStream.Read(bytes, 0, bytes.Length)) != 0)
{
output.Write(bytes, 0, n); }
}
} } /// <summary>
/// 反序列化文件描述对象
/// </summary>
/// <param name="sourceStream"></param>
/// <param name="outputPath"></param>
private static void DeserializeFileInfo(Stream sourceStream, string outputPath)
{
BinaryFormatter b = new BinaryFormatter();
ArrayList list = (ArrayList)b.Deserialize(sourceStream); foreach (CompressFileInfo item in list)
{
string newName =Path.Combine(outputPath , item.FileName);
using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write))
{
fs.Write(item.FileBuffer, 0, item.FileBuffer.Length);
fs.Close();
}
}
}
CompressHelper的更多相关文章
- MessageReceiver
class MessageReceiver { private RelayEngine<MessageCollection> _MessageRelayEngine; private st ...
- MessageClient
using Manager.Common; using System; using System.Collections.Generic; using System.Diagnostics; usin ...
- javascript中base64和Gzip的使用
一般的使用流程(4步): 服务器端将字符串Gzip压缩为 字节数组——>通过base64转为字符串(后传递到客户端)——>解码base64字符串为字节数组——>Gzip解码字节数组为 ...
- 【转载】.NET压缩/解压文件/夹组件
转自:http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html 阅读目录 1.前言 2.关于压缩格式和算法的基础 3.几种常见的.N ...
- android -------- 压缩图片文件工具类
项目中常常遇到文件压缩问题,上传文件大小限制 今天简单的分享一点干货,文件压缩,图片压缩,压缩Bitmap 主要通过尺寸压缩和质量压缩,以达到清晰度最优 效果图 源码地址: https://githu ...
- github_源码
固定头部: hongyangAndroid/Android-StickyNavLayout:ListView 与ViewPager 滑动冲突处理,滑动到顶部固定位置停顿; ufo22940268/ ...
- winform自动升级方案
未涉及过winform升级,研究一阵,大致出来个不成熟的方案. 我的解决方案(判断升级,升级程序下载安装包的压缩包,解压,自动安装,重新启动程序). 1.首先根据服务器中软件版本号和本地软件版本号是否 ...
- Android开源库集合(工具)
图片加载框架: Glide https://github.com/bumptech/glide Android-Universal-Image-Loader https://github.com/no ...
- SharpCompress压缩和解压缩,并解决压缩的中文乱码问题
一.下载SharpCompress库 二.解压缩 (1)不带密码 /// <summary> /// 解压缩(支持rar,zip) /// </summary> /// < ...
随机推荐
- (转)对SQLSERVER数据库事务日志的疑问
本文转载自桦仔的博客http://www.cnblogs.com/lyhabc/archive/2013/06/10/3130856.html 对SQLSERVER数据库事务日志的疑问 摸不透SQLS ...
- 公共代码参考(Volley)
Volley 是google提供的一个网络库,相对于自己写httpclient确实方便很多,本文参考部分网上例子整理如下,以作备忘: 定义一个缓存类: public class BitmapCache ...
- java提高篇(二九)-----Vector
在java提高篇(二一)-–ArrayList.java提高篇(二二)-LinkedList,详细讲解了ArrayList.linkedList的原理和实现过程,对于List接口这里还介绍一个它的实现 ...
- java提高篇(三)-----java的四舍五入
Java小事非小事!!!!!!!!!!!! 四舍五入是我们小学的数学问题,这个问题对于我们程序猿来说就类似于1到10的加减乘除那么简单了.在讲解之间我们先看如下一个经典的案例: public stat ...
- 你不可不知的HTML优化技巧
如何提升Web页面的性能,很多开发人员从多个方面来下手如JavaScript.图像优化.服务器配置,文件压缩或是调整CSS. 很显然HTML 已经达到了一个瓶颈,尽管它是开发Web 界面必备的核心语言 ...
- Spring Trasnaction管理(3)- 事务嵌套
问题导读 Spring 如何管理嵌套的事务 Spring事务传播机制 Nested 和 RequireNew 有何区别 事务传播机制 事务的传播机制应该都比较熟悉 在日常开发中会遇到需要事务嵌套的情况 ...
- Repeater绑定数组并显示其值
web开发中,尤其是对于数据展示,不得不说Repeater是一个万能的控件,而且使用也很方便. 在ASP.NET中将数组绑定到Repeater中请问如何在Repeater前台页面中显示该数组的值? s ...
- NodeJS实例系列~环境搭建,Hello world归来!
回到目录 1 安装Node.js服务端程序 https://github.com/Microsoft/nodejstools/wiki/Install-Node.js-and-get-started- ...
- Atitit 数据库事务实现原理
Atitit 数据库事务实现原理 1.1. 自己在程序中实现事务操作. 如果只是需要事务的话,你自己给mongo操作加上事务功能就可以啦..数据库事务只不过是他自己实现了而已..如果数据库不支持事 ...
- fir.im Weekly - 热门 iOS 第三方库大盘点
本期 fir.im Weekly 收集的热度资源,大部分关于Android.iOS 开发工具.源码和脑洞大开的 UI 动画,希望给你带来更多的工作创意与灵感. 盘点国内程序员不常用的热门iOS第三方库 ...