b7390320f74d515c881201f1c4c19e7dd2e3a6ce.svn-base 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Runtime.Serialization.Formatters.Binary;
  6. using System.Data;
  7. using System.Runtime.Serialization;
  8. using System.IO.Compression;
  9. namespace Core.Mes.Common
  10. {
  11. public class Utility
  12. {
  13. #region dataset to byte[]
  14. /// <summary>
  15. /// 序列化数据集
  16. /// </summary>
  17. /// <param name="ds"></param>
  18. /// <returns></returns>
  19. public static byte[] SerializeDataSet(DataSet ds)
  20. {
  21. byte[] buffer = null;
  22. ds.RemotingFormat = SerializationFormat.Binary;
  23. MemoryStream ms = new MemoryStream();
  24. IFormatter bf = new BinaryFormatter();
  25. bf.Serialize(ms, ds);
  26. buffer = ms.ToArray();
  27. ms.Close();
  28. ms.Dispose();
  29. ms = null;
  30. ds.Dispose();
  31. ds = null;
  32. return buffer;
  33. }
  34. /// <summary>
  35. /// 反序列化数据集
  36. /// </summary>
  37. /// <param name="buffer"></param>
  38. /// <returns></returns>
  39. public static DataSet ReSerializable(byte[] buffer)
  40. {
  41. MemoryStream ms = new MemoryStream(buffer);
  42. IFormatter bf = new BinaryFormatter();
  43. object obj = bf.Deserialize(ms);
  44. DataSet ds = (DataSet)obj;
  45. ms.Close();
  46. ms.Dispose();
  47. return ds;
  48. }
  49. /// <summary>
  50. /// 序列化对象
  51. /// </summary>
  52. /// <param name="ds"></param>
  53. /// <returns></returns>
  54. public static byte[] SerializeObject(Object obj)
  55. {
  56. byte[] buffer = null;
  57. MemoryStream ms = new MemoryStream();
  58. IFormatter bf = new BinaryFormatter();
  59. bf.Serialize(ms, obj);
  60. buffer = ms.ToArray();
  61. ms.Close();
  62. ms.Dispose();
  63. ms = null;
  64. return buffer;
  65. }
  66. /// <summary>
  67. /// 反序列化对象
  68. /// </summary>
  69. /// <param name="buffer"></param>
  70. /// <returns></returns>
  71. public static Object ReSerializeObject(byte[] buffer)
  72. {
  73. MemoryStream ms = new MemoryStream(buffer);
  74. IFormatter bf = new BinaryFormatter();
  75. object obj = bf.Deserialize(ms);
  76. ms.Close();
  77. ms.Dispose();
  78. return obj;
  79. }
  80. /// <summary>
  81. /// 序列化对象
  82. /// </summary>
  83. /// <param name="ds"></param>
  84. /// <returns></returns>
  85. public static byte[] SerializeAndCompressObject(Object obj)
  86. {
  87. byte[] buffer = null;
  88. MemoryStream ms = new MemoryStream();
  89. IFormatter bf = new BinaryFormatter();
  90. bf.Serialize(ms, obj);
  91. buffer = ms.ToArray();
  92. ms.Close();
  93. ms.Dispose();
  94. ms = null;
  95. return CompressBytes(buffer);
  96. }
  97. /// <summary>
  98. /// 反序列化对象
  99. /// </summary>
  100. /// <param name="buffer"></param>
  101. /// <returns></returns>
  102. public static Object ReSerializeCompressedObject(byte[] buffer)
  103. {
  104. MemoryStream ms = new MemoryStream(Decompress(buffer));
  105. IFormatter bf = new BinaryFormatter();
  106. object obj = bf.Deserialize(ms);
  107. ms.Close();
  108. ms.Dispose();
  109. return obj;
  110. }
  111. #endregion
  112. #region 压缩、解压缩
  113. //压缩字节
  114. public static byte[] CompressBytes(byte[] bytes)
  115. { /* 1.创建压缩的数据流
  116. 2.设定compressStream为存放被压缩的文件流,并设定为压缩模式
  117. 3.将需要压缩的字节写到被压缩的文件流
  118. */
  119. using (MemoryStream compressStream = new MemoryStream())
  120. {
  121. using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Compress))
  122. zipStream.Write(bytes, 0, bytes.Length);
  123. return compressStream.ToArray();
  124. }
  125. }
  126. //解压缩字节
  127. public static byte[] Decompress(byte[] bytes)
  128. {
  129. /* 1.创建被压缩的数据流
  130. 2.创建zipStream对象,并传入解压的内存流
  131. 3.创建目标流
  132. 4.zipStream拷贝到目标流
  133. 5.返回目标流输出字节
  134. * */
  135. byte[] buffer = new byte[4096];//定义数据缓冲
  136. using (MemoryStream compressStream = new MemoryStream(bytes))
  137. {
  138. using (GZipStream zipStream = new GZipStream(compressStream, CompressionMode.Decompress))
  139. {
  140. using (MemoryStream resultStream = new MemoryStream())
  141. {
  142. int offset = 0;
  143. while ((offset = zipStream.Read(buffer, 0, buffer.Length)) != 0)
  144. {
  145. resultStream.Write(buffer, 0, offset);
  146. }
  147. return resultStream.ToArray();
  148. }
  149. }
  150. }
  151. }
  152. //////////////////////////////////////////////////////////////////////////
  153. ///读取Stream
  154. //////////////////////////////////////////////////////////////////////////
  155. public static int ReadAllBytesFromStream(Stream stream, byte[] buffer)
  156. {
  157. int offset = 0;
  158. int totalCount = 0;
  159. while (true)
  160. {
  161. int bytesRead = stream.Read(buffer, offset, 100);
  162. if (bytesRead == 0)
  163. {
  164. break;
  165. }
  166. offset += bytesRead;
  167. totalCount += bytesRead;
  168. }
  169. return totalCount;
  170. }
  171. #endregion
  172. }
  173. }