Zlib.net官方源码demo中提供了压缩文件的源码算法。处于项目研发的需要,我需要对内存流进行压缩,由于zlib.net并无相关文字帮助只能自己看源码解决。通过对SharpZipLib的demo研究,写出了Zlib.net的流压缩算法。
中间花费了不少的时间,应为通过Stream压缩出来的数据全是空的,呵呵,主要原因就是忽略了ZOutputStream.flush()和ZOutPutStream.close()方法。大家自己看吧。关于字符串压缩,自然是把字符串塞到MemoryStream中就可以了:)
其中Zlib.net从http://www.componentace.com下载。/Files/yangyong/zlib.NET_104.rar
现贴源码如下:
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using System.Data;
6using System.IO;
7using System.Runtime.Serialization.Formatters.Binary;
8using ComponentAce.Compression.Libs.zlib;
9
10namespace Test.Compress
11{
12 /**//**//**//// <summary>
13 /// Zlib压缩算法压缩应用类
14 /// </summary>
15 public class ZlibCompress
16 {
17 /**//**//**//// <summary>
18 /// 拷贝复制流字节
19 /// </summary>
20 /// <param name="input"></param>
21 /// <param name="output"></param>
22 private static void CopyStream(Stream input, Stream output)
23 {
24 byte[] buffer = new byte[ZipConst.ZIP_BUFFER_SIZE];
25 int len;
)
27 {
, len);
29 }
30 output.Flush();
31 }
32
33 /**//**//**//// <summary>
34 /// 压缩文件
35 /// </summary>
36 /// <param name="FileName">被压缩文件名(必须输入绝对路径)</param>
37 /// <param name="CompressedFileName">压缩后保存的文件名(必须输入绝对路径)</param>
38 /// <returns></returns>
39 public static bool CompressFile(string FileName, string CompressedFileName)
40 {
41 bool bResult = false;
42
43 FileStream outFileStream = new FileStream(CompressedFileName, FileMode.Create);
44 ZOutputStream outZStream = new ZOutputStream(outFileStream, zlibConst.Z_DEFAULT_COMPRESSION);
45 FileStream inFileStream = new FileStream(FileName, FileMode.Open);
46 try
47 {
48 CopyStream(inFileStream, outZStream);
49 bResult = true;
50 }
51 catch
52 {
53 bResult = false;
54 }
55 finally
56 {
57 outZStream.Close();
58 outFileStream.Close();
59 inFileStream.Close();
60 }
61 return bResult;
62 }
63
64 /**//**//**//// <summary>
65 /// 解压文件
66 /// </summary>
67 /// <param name="CompressedFileName">被解压文件名(必须输入绝对路径)</param>
68 /// <param name="DecompressFileName">解压后保存的文件名(必须输入绝对路径)</param>
69 /// <returns></returns>
70 public static bool DecompressFile(string CompressedFileName, string DecompressFileName)
71 {
72 bool bResult = false;
73 FileStream outFileStream = new FileStream(DecompressFileName, FileMode.Create);
74 ZOutputStream outZStream = new ZOutputStream(outFileStream);
75 FileStream inFileStream = new FileStream(CompressedFileName, FileMode.Open);
76 try
77 {
78 CopyStream(inFileStream, outZStream);
79 bResult = true;
80 }
81 catch
82 {
83 bResult = false;
84 }
85 finally
86 {
87 outZStream.Close();
88 outFileStream.Close();
89 inFileStream.Close();
90 }
91 return bResult;
92 }
93
94 /**//**//**//// <summary>
95 /// 压缩byte数组数据
96 /// </summary>
97 /// <param name="SourceByte">需要被压缩的Byte数组数据</param>
98 /// <returns></returns>
99 public static byte[] CompressBytes(byte[] SourceByte)
{
try
{
MemoryStream stmInput = new MemoryStream(SourceByte);
Stream stmOutPut = ZlibCompress.CompressStream(stmInput);
byte[] bytOutPut = new byte[stmOutPut.Length];
;
, bytOutPut.Length);
return bytOutPut;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 解压byte数据数据
/// </summary>
/// <param name="SourceByte">需要被解压的byte数组数据</param>
/// <returns></returns>
public static byte[] DecompressBytes(byte[] SourceByte)
{
try
{
MemoryStream stmInput = new MemoryStream(SourceByte);
Stream stmOutPut = ZlibCompress.DecompressStream(stmInput);
byte[] bytOutPut = new byte[stmOutPut.Length];
;
, bytOutPut.Length);
return bytOutPut;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 压缩流
/// </summary>
/// <param name="SourceStream">需要被压缩的流数据</param>
/// <returns></returns>
public static Stream CompressStream(Stream SourceStream)
{
try
{
MemoryStream stmOutTemp = new MemoryStream();
ZOutputStream outZStream = new ZOutputStream(stmOutTemp, zlibConst.Z_DEFAULT_COMPRESSION);
CopyStream(SourceStream, outZStream);
outZStream.finish();
return stmOutTemp;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 解压流
/// </summary>
/// <param name="SourceStream">需要被解缩的流数据</param>
/// <returns></returns>
public static Stream DecompressStream(Stream SourceStream)
{
try
{
MemoryStream stmOutput = new MemoryStream();
ZOutputStream outZStream = new ZOutputStream(stmOutput);
CopyStream(SourceStream, outZStream);
outZStream.finish();
return stmOutput;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 压缩字符串
/// </summary>
/// <param name="SourceString">需要被压缩的字符串</param>
/// <returns></returns>
public static string CompressString(string SourceString)
{
byte[] byteSource = System.Text.Encoding.UTF8.GetBytes(SourceString);
byte[] byteCompress = ZlibCompress.CompressBytes(byteSource);
if (byteCompress != null)
{
return Convert.ToBase64String(byteCompress);
}
else
{
return null;
}
}
/**//**//**//// <summary>
/// 解压字符串
/// </summary>
/// <param name="SourceString">需要被解压的字符串</param>
/// <returns></returns>
public static string DecompressString(string SourceString)
{
byte[] byteSource = Convert.FromBase64String(SourceString);
byte[] byteDecompress = ZlibCompress.DecompressBytes(byteSource);
if (byteDecompress != null)
{
return System.Text.Encoding.UTF8.GetString(byteDecompress);
}
else
{
return null;
}
}
}
}
2using System.Collections.Generic;
3using System.Text;
4using System.Xml;
5using System.Data;
6using System.IO;
7using System.Runtime.Serialization.Formatters.Binary;
8using ComponentAce.Compression.Libs.zlib;
9
10namespace Test.Compress
11{
12 /**//**//**//// <summary>
13 /// Zlib压缩算法压缩应用类
14 /// </summary>
15 public class ZlibCompress
16 {
17 /**//**//**//// <summary>
18 /// 拷贝复制流字节
19 /// </summary>
20 /// <param name="input"></param>
21 /// <param name="output"></param>
22 private static void CopyStream(Stream input, Stream output)
23 {
24 byte[] buffer = new byte[ZipConst.ZIP_BUFFER_SIZE];
25 int len;
)
27 {
, len);
29 }
30 output.Flush();
31 }
32
33 /**//**//**//// <summary>
34 /// 压缩文件
35 /// </summary>
36 /// <param name="FileName">被压缩文件名(必须输入绝对路径)</param>
37 /// <param name="CompressedFileName">压缩后保存的文件名(必须输入绝对路径)</param>
38 /// <returns></returns>
39 public static bool CompressFile(string FileName, string CompressedFileName)
40 {
41 bool bResult = false;
42
43 FileStream outFileStream = new FileStream(CompressedFileName, FileMode.Create);
44 ZOutputStream outZStream = new ZOutputStream(outFileStream, zlibConst.Z_DEFAULT_COMPRESSION);
45 FileStream inFileStream = new FileStream(FileName, FileMode.Open);
46 try
47 {
48 CopyStream(inFileStream, outZStream);
49 bResult = true;
50 }
51 catch
52 {
53 bResult = false;
54 }
55 finally
56 {
57 outZStream.Close();
58 outFileStream.Close();
59 inFileStream.Close();
60 }
61 return bResult;
62 }
63
64 /**//**//**//// <summary>
65 /// 解压文件
66 /// </summary>
67 /// <param name="CompressedFileName">被解压文件名(必须输入绝对路径)</param>
68 /// <param name="DecompressFileName">解压后保存的文件名(必须输入绝对路径)</param>
69 /// <returns></returns>
70 public static bool DecompressFile(string CompressedFileName, string DecompressFileName)
71 {
72 bool bResult = false;
73 FileStream outFileStream = new FileStream(DecompressFileName, FileMode.Create);
74 ZOutputStream outZStream = new ZOutputStream(outFileStream);
75 FileStream inFileStream = new FileStream(CompressedFileName, FileMode.Open);
76 try
77 {
78 CopyStream(inFileStream, outZStream);
79 bResult = true;
80 }
81 catch
82 {
83 bResult = false;
84 }
85 finally
86 {
87 outZStream.Close();
88 outFileStream.Close();
89 inFileStream.Close();
90 }
91 return bResult;
92 }
93
94 /**//**//**//// <summary>
95 /// 压缩byte数组数据
96 /// </summary>
97 /// <param name="SourceByte">需要被压缩的Byte数组数据</param>
98 /// <returns></returns>
99 public static byte[] CompressBytes(byte[] SourceByte)
{
try
{
MemoryStream stmInput = new MemoryStream(SourceByte);
Stream stmOutPut = ZlibCompress.CompressStream(stmInput);
byte[] bytOutPut = new byte[stmOutPut.Length];
;
, bytOutPut.Length);
return bytOutPut;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 解压byte数据数据
/// </summary>
/// <param name="SourceByte">需要被解压的byte数组数据</param>
/// <returns></returns>
public static byte[] DecompressBytes(byte[] SourceByte)
{
try
{
MemoryStream stmInput = new MemoryStream(SourceByte);
Stream stmOutPut = ZlibCompress.DecompressStream(stmInput);
byte[] bytOutPut = new byte[stmOutPut.Length];
;
, bytOutPut.Length);
return bytOutPut;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 压缩流
/// </summary>
/// <param name="SourceStream">需要被压缩的流数据</param>
/// <returns></returns>
public static Stream CompressStream(Stream SourceStream)
{
try
{
MemoryStream stmOutTemp = new MemoryStream();
ZOutputStream outZStream = new ZOutputStream(stmOutTemp, zlibConst.Z_DEFAULT_COMPRESSION);
CopyStream(SourceStream, outZStream);
outZStream.finish();
return stmOutTemp;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 解压流
/// </summary>
/// <param name="SourceStream">需要被解缩的流数据</param>
/// <returns></returns>
public static Stream DecompressStream(Stream SourceStream)
{
try
{
MemoryStream stmOutput = new MemoryStream();
ZOutputStream outZStream = new ZOutputStream(stmOutput);
CopyStream(SourceStream, outZStream);
outZStream.finish();
return stmOutput;
}
catch
{
return null;
}
}
/**//**//**//// <summary>
/// 压缩字符串
/// </summary>
/// <param name="SourceString">需要被压缩的字符串</param>
/// <returns></returns>
public static string CompressString(string SourceString)
{
byte[] byteSource = System.Text.Encoding.UTF8.GetBytes(SourceString);
byte[] byteCompress = ZlibCompress.CompressBytes(byteSource);
if (byteCompress != null)
{
return Convert.ToBase64String(byteCompress);
}
else
{
return null;
}
}
/**//**//**//// <summary>
/// 解压字符串
/// </summary>
/// <param name="SourceString">需要被解压的字符串</param>
/// <returns></returns>
public static string DecompressString(string SourceString)
{
byte[] byteSource = Convert.FromBase64String(SourceString);
byte[] byteDecompress = ZlibCompress.DecompressBytes(byteSource);
if (byteDecompress != null)
{
return System.Text.Encoding.UTF8.GetString(byteDecompress);
}
else
{
return null;
}
}
}
}