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> /// < ...
随机推荐
- centos7 memcached+memagent 集群
1. 安装libevent wget https://github.com/libevent/libevent/releases/download/release-2.0.22-stable/libe ...
- PSP个人耗时
PSP2.1 Personal Software Process Stage Time(min) Planing 计划 20 #Estimate #估计这个任务需要多长时间 180 Developi ...
- [翻译].NET随机数
原文链接:http://csharpindepth.com/Articles/Chapter12/Random.aspx 随机数 当你在Stack Overflow上看到看到某个问题标题当中有“随 ...
- Properties
java.util 类 Properties 因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法.但强烈反对使用这两个方法 ...
- java.logging的重定向?
接着昨天的工作. 上面说要重定向java.util.logging.Logger的输出, 发现也不是不可能. package jmx; import java.util.logging.FileHan ...
- AutoMapper的简单使用
接触AutoMapper已经有两年多的时间了,在ORM框架中,它使持久层对象与DTO对象之间的转换变得相当简单. 随着负责的项目的增多,使用的技术框架一多起来,很多具体的技术点难免记不清, 加上同时兼 ...
- 为什么Java方法里面不能再嵌套方法?
直接原因: 这是Java基本语法定义的,方法中不可以再次声明方法,只能调用其他的方法. 个人理解: 1.方法栈是需要一个载体的,这个载体就是Class,如果一个方法的上一级不是一个类,就说明没有载体. ...
- JS判断鼠标移入元素的方向
最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { v ...
- MVVM架构~Knockoutjs系列之对象与对象组合
返回目录 在面向对象的程序设计里,对象是核心,一切皆为对象,对象与对象之间的关系可以表现为继承和组合,而在Knockoutjs或者JS里,也存在着对象的概念,今天主要说一下JS里的对象及对象的组合. ...
- Atitit 基于图片图像 与文档混合文件夹的分类
Atitit 基于图片图像 与文档混合文件夹的分类 太小的文档(txt doc csv exl ppt pptx)单独分类 Mov10KminiDoc 但是可能会有一些书法图片迁移,因为他们很微小,需 ...