在当今网络传输中,由于带宽的限制以及一些特殊的需求,需要对传输的文件进行压缩处理,这在实际的网络开发中非常有用。下面便是一个小例子,主要是应用.net新添加的命名空间“System.IO.Compression”中包含的能压缩文件的类,这些类使用的GZIP或Deflate算法,这两种算法都是公开的、免费。由于由于技术出台比较晚,相关的实例也比较少,大多人仍然在使用ICSharpCode.SharpZipLib.dll,至于这两者具体实现区别,我现在还不是很清楚。下面是关于这个算法的实例,希望能给大家一些帮助。
首先是最普遍的也是我们最长用的,将字符串进行压缩和解压缩处理
首先是最普遍的也是我们最长用的,将字符串进行压缩和解压缩处理
1using System;
2using System.Text;
3using System.IO;
4using System.IO.Compression;
5
6/// <summary>
7/// Summary description for Compress
8/// </summary>
9public class Compress
10{
11 public Compress()
12 {
13 //
14 // TODO: Add constructor logic here
15 //
16 }
17 /// <summary>
18 /// 压缩文件,返回字符串
19 /// </summary>
20 /// <param name="param"></param>
21 /// <returns></returns>
22 public static string CompressObject(string param)
23 {
24 string compressString = "";
25 //声明一个nei
26 MemoryStream mstream = new MemoryStream();
27 GZipStream cstream = new GZipStream(mstream, CompressionMode.Compress, true);
28 StreamWriter bwriter = new StreamWriter(cstream);
29 bwriter.Write(param);
30 //BinaryWriter bwriter = new BinaryWriter(cstream);
31 //bwriter.Write(Encoding.UTF8.GetBytes(param));
32 bwriter.Close();
33 cstream.Close();
34 compressString = Convert.ToBase64String(mstream.ToArray());
35 mstream.Close();
36 return compressString;
37 }
38 /// <summary>
39 /// 解压缩字符串
40 /// </summary>
41 /// <param name="param"></param>
42 /// <returns></returns>
43 public static string DepressObject(string param)
44 {
45 string commonString = "";
46 byte[] data = Convert.FromBase64String(param) ;
47 MemoryStream mstream = new MemoryStream(data);
48 GZipStream cstream = new GZipStream(mstream, CompressionMode.Decompress);
49 StreamReader reader = new StreamReader(cstream);
50 commonString = reader.ReadToEnd();
51 mstream.Close();
52 cstream.Close();
53 reader.Close();
54 return commonString;
55 }
56}
57
补充,对文件的压缩处理,这只是一个小例子。2using System.Text;
3using System.IO;
4using System.IO.Compression;
5
6/// <summary>
7/// Summary description for Compress
8/// </summary>
9public class Compress
10{
11 public Compress()
12 {
13 //
14 // TODO: Add constructor logic here
15 //
16 }
17 /// <summary>
18 /// 压缩文件,返回字符串
19 /// </summary>
20 /// <param name="param"></param>
21 /// <returns></returns>
22 public static string CompressObject(string param)
23 {
24 string compressString = "";
25 //声明一个nei
26 MemoryStream mstream = new MemoryStream();
27 GZipStream cstream = new GZipStream(mstream, CompressionMode.Compress, true);
28 StreamWriter bwriter = new StreamWriter(cstream);
29 bwriter.Write(param);
30 //BinaryWriter bwriter = new BinaryWriter(cstream);
31 //bwriter.Write(Encoding.UTF8.GetBytes(param));
32 bwriter.Close();
33 cstream.Close();
34 compressString = Convert.ToBase64String(mstream.ToArray());
35 mstream.Close();
36 return compressString;
37 }
38 /// <summary>
39 /// 解压缩字符串
40 /// </summary>
41 /// <param name="param"></param>
42 /// <returns></returns>
43 public static string DepressObject(string param)
44 {
45 string commonString = "";
46 byte[] data = Convert.FromBase64String(param) ;
47 MemoryStream mstream = new MemoryStream(data);
48 GZipStream cstream = new GZipStream(mstream, CompressionMode.Decompress);
49 StreamReader reader = new StreamReader(cstream);
50 commonString = reader.ReadToEnd();
51 mstream.Close();
52 cstream.Close();
53 reader.Close();
54 return commonString;
55 }
56}
57
1using System;
2using System.Data;
3using System.IO;
4using System.IO.Compression;
5
6/// <summary>
7/// Summary description for Cop
8/// </summary>
9public class Cop
10{
11 public Cop()
12 {
13 //
14 // TODO: Add constructor logic here
15 //
16 }
17 /// <summary>
18 /// 压缩文件
19 /// </summary>
20 /// <param name="fileName">文件名(全路径)</param>
21 /// <param name="data">需要压缩的字符串</param>
22 public void CompressFile(string fileName, string data)
23 {
24 FileStream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
25 GZipStream gstream = new GZipStream(fstream, CompressionMode.Compress);
26 StreamWriter swriter = new StreamWriter(gstream);
27 swriter.Write(data);
28 swriter.Close();
29 gstream.Close();
30 fstream.Close();
31 }
32 /// <summary>
33 /// 解压缩
34 /// </summary>
35 /// <param name="fileName">文件名(全路径)</param>
36 /// <returns></returns>
37 public string DecompressFile(string fileName)
38 {
39 string cstring="";
40 FileStream fstream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
41 GZipStream gstream = new GZipStream(fstream, CompressionMode.Decompress);
42 StreamReader reader = new StreamReader(gstream);
43 cstring=reader.ReadToEnd();
44 reader.Close();
45 gstream.Close();
46 fstream.Close();
47 return cstring;
48 }
49}
50
2using System.Data;
3using System.IO;
4using System.IO.Compression;
5
6/// <summary>
7/// Summary description for Cop
8/// </summary>
9public class Cop
10{
11 public Cop()
12 {
13 //
14 // TODO: Add constructor logic here
15 //
16 }
17 /// <summary>
18 /// 压缩文件
19 /// </summary>
20 /// <param name="fileName">文件名(全路径)</param>
21 /// <param name="data">需要压缩的字符串</param>
22 public void CompressFile(string fileName, string data)
23 {
24 FileStream fstream = new FileStream(fileName, FileMode.Create, FileAccess.Write);
25 GZipStream gstream = new GZipStream(fstream, CompressionMode.Compress);
26 StreamWriter swriter = new StreamWriter(gstream);
27 swriter.Write(data);
28 swriter.Close();
29 gstream.Close();
30 fstream.Close();
31 }
32 /// <summary>
33 /// 解压缩
34 /// </summary>
35 /// <param name="fileName">文件名(全路径)</param>
36 /// <returns></returns>
37 public string DecompressFile(string fileName)
38 {
39 string cstring="";
40 FileStream fstream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
41 GZipStream gstream = new GZipStream(fstream, CompressionMode.Decompress);
42 StreamReader reader = new StreamReader(gstream);
43 cstring=reader.ReadToEnd();
44 reader.Close();
45 gstream.Close();
46 fstream.Close();
47 return cstring;
48 }
49}
50