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;
26 while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
27 {
28 output.Write(buffer, 0, 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)
100 {
101 try
102 {
103 MemoryStream stmInput = new MemoryStream(SourceByte);
104 Stream stmOutPut = ZlibCompress.CompressStream(stmInput);
105 byte[] bytOutPut = new byte[stmOutPut.Length];
106 stmOutPut.Position = 0;
107 stmOutPut.Read(bytOutPut, 0, bytOutPut.Length);
108 return bytOutPut;
109 }
110 catch
111 {
112 return null;
113 }
114 }
115
116 /**//// <summary>
117 /// 解压byte数据数据
118 /// </summary>
119 /// <param name="SourceByte">需要被解压的byte数组数据</param>
120 /// <returns></returns>
121 public static byte[] DecompressBytes(byte[] SourceByte)
122 {
123 try
124 {
125 MemoryStream stmInput = new MemoryStream(SourceByte);
126 Stream stmOutPut = ZlibCompress.DecompressStream(stmInput);
127 byte[] bytOutPut = new byte[stmOutPut.Length];
128 stmOutPut.Position = 0;
129 stmOutPut.Read(bytOutPut, 0, bytOutPut.Length);
130 return bytOutPut;
131 }
132 catch
133 {
134 return null;
135 }
136 }
137
138 /**//// <summary>
139 /// 压缩流
140 /// </summary>
141 /// <param name="SourceStream">需要被压缩的流数据</param>
142 /// <returns></returns>
143 public static Stream CompressStream(Stream SourceStream)
144 {
145 try
146 {
147 MemoryStream stmOutTemp = new MemoryStream();
148 ZOutputStream outZStream = new ZOutputStream(stmOutTemp, zlibConst.Z_DEFAULT_COMPRESSION);
149 CopyStream(SourceStream, outZStream);
150 outZStream.finish();
151 return stmOutTemp;
152 }
153 catch
154 {
155 return null;
156 }
157 }
158
159 /**//// <summary>
160 /// 解压流
161 /// </summary>
162 /// <param name="SourceStream">需要被解缩的流数据</param>
163 /// <returns></returns>
164 public static Stream DecompressStream(Stream SourceStream)
165 {
166 try
167 {
168 MemoryStream stmOutput = new MemoryStream();
169 ZOutputStream outZStream = new ZOutputStream(stmOutput);
170 CopyStream(SourceStream, outZStream);
171 outZStream.finish();
172 return stmOutput;
173 }
174 catch
175 {
176 return null;
177 }
178 }
179
180 /**//// <summary>
181 /// 压缩字符串
182 /// </summary>
183 /// <param name="SourceString">需要被压缩的字符串</param>
184 /// <returns></returns>
185 public static string CompressString(string SourceString)
186 {
187 byte[] byteSource = System.Text.Encoding.UTF8.GetBytes(SourceString);
188 byte[] byteCompress = ZlibCompress.CompressBytes(byteSource);
189 if (byteCompress != null)
190 {
191 return Convert.ToBase64String(byteCompress);
192 }
193 else
194 {
195 return null;
196 }
197 }
198
199 /**//// <summary>
200 /// 解压字符串
201 /// </summary>
202 /// <param name="SourceString">需要被解压的字符串</param>
203 /// <returns></returns>
204 public static string DecompressString(string SourceString)
205 {
206 byte[] byteSource = Convert.FromBase64String(SourceString);
207 byte[] byteDecompress = ZlibCompress.DecompressBytes(byteSource);
208 if (byteDecompress != null)
209 {
210 return System.Text.Encoding.UTF8.GetString(byteDecompress);
211 }
212 else
213 {
214 return null;
215 }
216 }
217
218 }
219}
220
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;
26 while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
27 {
28 output.Write(buffer, 0, 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)
100 {
101 try
102 {
103 MemoryStream stmInput = new MemoryStream(SourceByte);
104 Stream stmOutPut = ZlibCompress.CompressStream(stmInput);
105 byte[] bytOutPut = new byte[stmOutPut.Length];
106 stmOutPut.Position = 0;
107 stmOutPut.Read(bytOutPut, 0, bytOutPut.Length);
108 return bytOutPut;
109 }
110 catch
111 {
112 return null;
113 }
114 }
115
116 /**//// <summary>
117 /// 解压byte数据数据
118 /// </summary>
119 /// <param name="SourceByte">需要被解压的byte数组数据</param>
120 /// <returns></returns>
121 public static byte[] DecompressBytes(byte[] SourceByte)
122 {
123 try
124 {
125 MemoryStream stmInput = new MemoryStream(SourceByte);
126 Stream stmOutPut = ZlibCompress.DecompressStream(stmInput);
127 byte[] bytOutPut = new byte[stmOutPut.Length];
128 stmOutPut.Position = 0;
129 stmOutPut.Read(bytOutPut, 0, bytOutPut.Length);
130 return bytOutPut;
131 }
132 catch
133 {
134 return null;
135 }
136 }
137
138 /**//// <summary>
139 /// 压缩流
140 /// </summary>
141 /// <param name="SourceStream">需要被压缩的流数据</param>
142 /// <returns></returns>
143 public static Stream CompressStream(Stream SourceStream)
144 {
145 try
146 {
147 MemoryStream stmOutTemp = new MemoryStream();
148 ZOutputStream outZStream = new ZOutputStream(stmOutTemp, zlibConst.Z_DEFAULT_COMPRESSION);
149 CopyStream(SourceStream, outZStream);
150 outZStream.finish();
151 return stmOutTemp;
152 }
153 catch
154 {
155 return null;
156 }
157 }
158
159 /**//// <summary>
160 /// 解压流
161 /// </summary>
162 /// <param name="SourceStream">需要被解缩的流数据</param>
163 /// <returns></returns>
164 public static Stream DecompressStream(Stream SourceStream)
165 {
166 try
167 {
168 MemoryStream stmOutput = new MemoryStream();
169 ZOutputStream outZStream = new ZOutputStream(stmOutput);
170 CopyStream(SourceStream, outZStream);
171 outZStream.finish();
172 return stmOutput;
173 }
174 catch
175 {
176 return null;
177 }
178 }
179
180 /**//// <summary>
181 /// 压缩字符串
182 /// </summary>
183 /// <param name="SourceString">需要被压缩的字符串</param>
184 /// <returns></returns>
185 public static string CompressString(string SourceString)
186 {
187 byte[] byteSource = System.Text.Encoding.UTF8.GetBytes(SourceString);
188 byte[] byteCompress = ZlibCompress.CompressBytes(byteSource);
189 if (byteCompress != null)
190 {
191 return Convert.ToBase64String(byteCompress);
192 }
193 else
194 {
195 return null;
196 }
197 }
198
199 /**//// <summary>
200 /// 解压字符串
201 /// </summary>
202 /// <param name="SourceString">需要被解压的字符串</param>
203 /// <returns></returns>
204 public static string DecompressString(string SourceString)
205 {
206 byte[] byteSource = Convert.FromBase64String(SourceString);
207 byte[] byteDecompress = ZlibCompress.DecompressBytes(byteSource);
208 if (byteDecompress != null)
209 {
210 return System.Text.Encoding.UTF8.GetString(byteDecompress);
211 }
212 else
213 {
214 return null;
215 }
216 }
217
218 }
219}
220