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> /// < ...
随机推荐
- 浅论Android网络请求库——android-async-http
在iOS开发中有大名鼎鼎的ASIHttpRequest库,用来处理网络请求操作,今天要介绍的是一个在Android上同样强大的网络请求库android-async-http,目前非常火的应用Insta ...
- SQL SERVER--DBA 常用到的一些脚本
自己整理了一些常用到的脚本,希望对各位有用 下载地址 --================================== 妹子不能少,是吧 BTW, 妹子是我辛苦百度来的,请不要求种求介绍各种求 ...
- Unity3D shader简介
Unity3D shader简介 可以肯定的说Unity3D使得很多开发者开发游戏更容易.毫无疑问,shader(着色器)编码,仍有很长的路要走.shader是一个专门运行在GPU的程序,经常被神秘包 ...
- js中各种跨域问题实战小结(二)
这里接上篇:js中各种跨域问题实战小结(一) 后面继续学习的过程中,对上面第一篇有稍作休整.下面继续第二部分: -->5.利用iframe和location.hash -->6.windo ...
- Unity3d热更新全书-加载(一)从AssetBundle说起
Unity3D动态下载资源,有没有解?有,AssetBundle就是通用解,任何一本书都会花大幅篇章来介绍AssetBundle. 我们也来说说AssetBundle 我们试全面的分析一下Unity3 ...
- Effective Java 创建和销毁对象
<Effective Java>阅读笔记,用适合自己理解的方式提炼该书内容.<Effective Java>是一本很实用的书,阅读方法应该是快速的领会,总结,然后应用.而非,一 ...
- IOS 多线程02-pthread 、 NSThread 、GCD 、NSOperationQueue、NSRunLoop
注:本人是翻译过来,并且加上本人的一点见解. 要点: 1.前言 2.pthread 3.NSThread 4.Grand Central Dispatch(GCD) 5.Operation Queue ...
- iOS-------应用性能调优的25个建议和技巧
性能对 iOS 应用的开发尤其重要,如果你的应用失去反应或者很慢,失望的用户会把他们的失望写满App Store的评论.然而由于iOS设备的限制,有时搞好性能是一件难事.开发过程中你会有很多需要注意的 ...
- python中常用的函数与库一
1, collections.deque 在python里如果我们用列表作为队列使用也是可以的,只是当从队尾删除或者增加元素的时候是很快的,但是从队首删除或者增加元素则要慢得多,这是因为在队首进行操作 ...
- rabbitMQ第三篇:采用不同的交换机规则
在上一篇我们都是采用发送信息到队列然后队列把信息在发送到消费者,其实实际情况并非如此,rabbitMQ其实真正的思想是生产者不发送任何信息到队列,甚至不知道信息将发送到哪个队列.相反生产者只能发送信息 ...