13MD5编码Zlib压缩解压缩

Posted 清幽火焰

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了13MD5编码Zlib压缩解压缩相关的知识,希望对你有一定的参考价值。

1.MD5加密

 1     /// <summary>  
 2     /// 使用MD5加密算法  
 3     /// </summary>  
 4     /// <param name="md5MessageStr">需要加密的字符串</param>  
 5     /// <returns>加密后返回字符串</returns>  
 6     public static string GetMD5String(string md5MessageStr)  
 7     {  
 8         using (MD5 md5 = new MD5CryptoServiceProvider())  
 9         {  
10             byte[] convertValue = Encoding.UTF8.GetBytes(md5MessageStr);  
11             byte[] resultValue = md5.ComputeHash(convertValue);  
12             string strResult = string.Empty;  
13             for (int i = 0; i < 16; i++)  
14             {  
15                 strResult += resultValue[i].ToString("x2");  
16             }  
17             return strResult;  
18         }  
19     }  

 

2.Zlib压缩解压缩

nuget获取zlib.net

 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         } 

 

以上是关于13MD5编码Zlib压缩解压缩的主要内容,如果未能解决你的问题,请参考以下文章

C/C++ 自制一个基于zlib的文件的(解)压缩系统

使用DEFLATE(RFC 1951)函数压缩和解压缩zlib(RFC 1950)

使用minizip解压缩多个文件(基于zlib)

webservice(axis)接口上传文件附件 及 用zlib解压缩

C++ 基于ZLIB压缩库的数据或文件的压缩与解压缩小程序

使用 zlib+dictionary 压缩/解压缩 spdy 名称/值块时出错