1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Web;
6 using zlib;
7
8 namespace LargePlatformService.Logic
9 {
10 public class ZLibNetHelper
11 {
12 /// <summary>
13 /// zlib.net 解压函数
14 /// </summary>
15 /// <param name="strSource">带解压数据源</param>
16 /// <returns>解压后的数据</returns>
17 public static string DeflateDecompress(string strSource)
18 {
19 int data = 0;
20 int stopByte = -1;
21 byte[] Buffer = Convert.FromBase64String(strSource); // 解base64
22 MemoryStream intms = new MemoryStream(Buffer);
23 zlib.ZInputStream inZStream = new zlib.ZInputStream(intms);
24 List<byte> inByteList = new List<byte>();
25 int i = 0;
26 while (stopByte != (data = inZStream.Read()))
27 {
28 inByteList.Add((byte)data);
29 }
30 inZStream.Close();
31 return System.Text.Encoding.UTF8.GetString(inByteList.ToArray(), 0, inByteList.Count);
32
33 }
34
35 /// <summary>
36 /// zlib.net 压缩函数
37 /// </summary>
38 /// <param name="strSource">待压缩数据</param>
39 /// <returns>压缩后数据</returns>
40 public static string DeflateCompress(string strSource)
41 {
42 MemoryStream outms = new MemoryStream();
43 byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strSource);
44 MemoryStream inms = new MemoryStream(bytes);
45 zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outms, zlib.zlibConst.Z_DEFAULT_COMPRESSION);
46 try
47 {
48 CopyStream(inms, outZStream);
49 }
50 catch (Exception ex)
51 {
52 throw ex;
53 }
54 finally
55 {
56 outZStream.Close();
57 }
58 return Convert.ToBase64String(outms.ToArray());
59 }
60
61
62 public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
63 {
64 byte[] buffer = new byte[2000];
65 int len;
66 while ((len = input.Read(buffer, 0, 2000)) > 0)
67 {
68 output.Write(buffer, 0, len);
69 }
70 output.Flush();
71 }
72 }
73 }