前端之家收集整理的这篇文章主要介绍了
对文件和字符串压缩及解压缩类,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> /// <summary>
/// 对文件和字符串压缩及解压缩类
/// </summary>
public class GZip
{
/// <summary>
/// 对字符串进行压缩
/// </summary>
/// <param name="str">待压缩的字符串</param>
/// <returns>压缩后的字符串</returns>
public static string CompressString(string str)
{
string compressString = "";
byte[] compressBeforeByte = Encoding.Default.GetBytes(str);
byte[] compressAfterByte=GZip.Compress(compressBeforeByte);
compressString = Encoding.Default.GetString(compressAfterByte);
return compressString;
}
/// <summary>
/// 对字符串进行解压缩
/// </summary>
/// <param name="str">待解压缩的字符串</param>
/// <returns>解压缩后的字符串</returns>
public static string DecompressString(string str)
{
string compressString = "";
byte[] compressBeforeByte = Encoding.Default.GetBytes(str);
byte[] compressAfterByte = GZip.Decompress(compressBeforeByte);
compressString = Encoding.Default.GetString(compressAfterByte);
return compressString;
}
/// <summary>
/// 对文件进行压缩
/// </summary>
/// <param name="sourceFile">待压缩的文件名</param>
/// <param name="destinationFile">压缩后的文件名</param>
public static void CompressFile(string sourceFile,string destinationFile)
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 对文件进行解压缩
/// </summary>
/// <param name="sourceFile">待解压缩的文件名</param>
/// <param name="destinationFile">解压缩后的文件名</param>
/// <returns></returns>
public static void DecompressFile(string sourceFile,string destinationFile)
{
throw new Exception("The method or operation is not implemented.");
}
/// <summary>
/// 对byte数组进行压缩
/// </summary>
/// <param name="data">待压缩的byte数组</param>
/// <returns>压缩后的byte数组</returns>
public static byte[] Compress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream();
GZipStream zip = new GZipStream(ms,CompressionMode.Compress,true);
zip.Write(data,data.Length);
zip.Close();
byte[] buffer = new byte[ms.Length];
ms.Position=0;
ms.Read(buffer,buffer.Length);
ms.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
public static byte[] Decompress(byte[] data)
{
try
{
MemoryStream ms = new MemoryStream(data);
GZipStream zip = new GZipStream(ms,CompressionMode.Decompress,true);
MemoryStream msreader = new MemoryStream();
byte[] buffer = new byte[0x1000];
while (true)
{
int reader=zip.Read(buffer,buffer.Length);
if (reader <= 0)
{
break;
}
msreader.Write(buffer,reader);
}
zip.Close();
ms.Close();
msreader.Position = 0;
buffer = msreader.ToArray();
msreader.Close();
return buffer;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/**//// <summary>
/// 对目标文件夹进行压缩,将压缩结果保存为指定文件
/// </summary>
/// <param name="dirPath">目标文件夹</param>
/// <param name="fileName">压缩文件</param>
public static void Compress(string dirPath,string fileName)
{
ArrayList list = new ArrayList();
foreach (string f in Directory.GetFiles(dirPath))
{
byte[] destBuffer = File.ReadAllBytes(f);
SerializeFileInfo sfi = new SerializeFileInfo(f,destBuffer);
list.Add(sfi);
}
IFormatter formatter = new BinaryFormatter();
using (Stream s = new MemoryStream())
{
formatter.Serialize(s,list);
s.Position = 0;
CreateCompressFile(s,fileName);
}
}
/**//// <summary>
/// 对目标压缩文件解压缩,将内容解压缩到指定文件夹
/// </summary>
/// <param name="fileName">压缩文件</param>
/// <param name="dirPath">解压缩目录</param>
public static void DeCompress(string fileName,string dirPath)
{
using (Stream source = File.OpenRead(fileName))
{
using (Stream destination = new MemoryStream())
{
using (GZipStream input = new GZipStream(source,true))
{
byte[] bytes = new byte[4096];
int n;
while ((n = input.Read(bytes,bytes.Length)) != 0)
{
destination.Write(bytes,n);
}
}
destination.Flush();
destination.Position = 0;
DeSerializeFiles(destination,dirPath);
}
}
}
private static void DeSerializeFiles(Stream s,string dirPath)
{
BinaryFormatter b = new BinaryFormatter();
ArrayList list = (ArrayList)b.Deserialize(s);
foreach (SerializeFileInfo f in list)
{
string newName = dirPath + Path.GetFileName(f.FileName);
using (FileStream fs = new FileStream(newName,FileMode.Create,FileAccess.Write))
{
fs.Write(f.FileBuffer,f.FileBuffer.Length);
fs.Close();
}
}
}
private static void CreateCompressFile(Stream source,string destinationName)
{
using (Stream destination = new FileStream(destinationName,FileAccess.Write))
{
using (GZipStream output = new GZipStream(destination,CompressionMode.Compress))
{
byte[] bytes = new byte[4096];
int n;
while ((n = source.Read(bytes,bytes.Length)) != 0)
{
output.Write(bytes,n);
}
}
}
}
[Serializable]
class SerializeFileInfo
{
public SerializeFileInfo(string name,byte[] buffer)
{
fileName = name;
fileBuffer = buffer;
}
string fileName;
public string FileName
{
get
{
return fileName;
}
}
byte[] fileBuffer;
public byte[] FileBuffer
{
get
{
return fileBuffer;
}
}
}
}
From:http://www.cnblogs.com/chenqingwei/archive/2010/07/15/1777806.html