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> /// < ...
随机推荐
- C#函数式编程之序列
过了许久的时间,终于趁闲暇的时间来继续将函数式编程这个专辑连载下去,这段时间开头是为IOS这个新方向做准备,将OC的教程写成了SWIFT版,当然我个人是支持Xamarin,但是我一般会先掌握原生态的开 ...
- JDBC学习2:为什么要写Class.forName("XXX")?
Class.forName(String name) 接上一篇JDBC.本来这个内容是放在前面的一篇里面的一起的,后来发现越写越多,想想看就算了,还是单独开一篇文章好了,这样也能写得更加详细点. 上一 ...
- 一个App完成入门篇(一)-从Hello world开始
程序员学习新技术都是通过Hello World开始的,我们也不例外.第一课我们简单了解利用do平台开发App的基本流程,能了解到的知识点是: 开发环境搭建 创建开发者账号 新建项目 拖拽一个组件 修改 ...
- 客户端GUI程序开发漫谈
这篇文章包含了这个领域的很多开源项目的介绍,还有我多年来的心血和汗水 去年夏天的时候,我用QT做了一个小工具 后来还用QT做了流程设计器 我把程序分享给飞扬青云之后,他甚至搞出来一套QT的皮肤来 说 ...
- 防止开发人员获取到敏感数据(SQL Server的数据加密简介)
背景 有时候,我们还真的会碰到这样的需求:防止开发人员获取到敏感数据.也许你觉得很简单,把开发和运营分开不就可以了吗?是的,如果公司有专门的运营团队的话,但对于很多小公司来说,几个人的开发团队就兼顾了 ...
- [.net 面向对象编程基础] (15) 抽象类
[.net 面向对象编程基础] (15) 抽象类 前面我们已经使用到了虚方法(使用 Virtual修饰符)和抽象类及抽象方法(使用abstract修饰符)我们在多态一节中说到要实现类成员的重写必须定义 ...
- Git 远程仓库搭建
大名鼎鼎的git就不多做介绍了,总之.我们使用git来作为项目的一个版本控制工具,多人开发的项目的时候会轻松很多. 安装git whthomas@whthomas:~/workplace/gitOne ...
- 使用MYSQL命令直接导入导出SQL文件
很多时候,我们的数据开发都会用到很多开发利器,比如powerdesigner, navicat等这些软件,虽然好用,但是要收费,在公司里面是禁止使用盗版软件的,怕罚款各方面的,所以我们也不敢直接在公司 ...
- lua元表Metatable
Lua 中的每个值都可以用一个 metatable. 这个 metatable 就是一个原始的 Lua table , 它用来定义原始值在特定操作下的行为. 你可以通过在 metatable 中的特定 ...
- CSS3_02之2D、3D动画
1.转换属性:transform:取值:transform-function(转换函数): 2.转换原点:默认元素的中心处:更改转换原点:transform-origin:取值:数字/百分比/关键字: ...