a99d0ce06e205316c37994dd9506e6f8e8ca12e5.svn-base 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. namespace Core.Mes.Common
  9. {
  10. public class Utility
  11. {
  12. #region dataset to byte[]
  13. /// <summary>
  14. ///
  15. /// </summary>
  16. /// <param name="ds"></param>
  17. /// <returns></returns>
  18. public static byte[] SerializeDataSet(DataSet ds)
  19. {
  20. byte[] buffer = null;
  21. ds.RemotingFormat = SerializationFormat.Binary;
  22. MemoryStream ms = new MemoryStream();
  23. IFormatter bf = new BinaryFormatter();
  24. bf.Serialize(ms, ds);
  25. buffer = ms.ToArray();
  26. ms.Close();
  27. ms.Dispose();
  28. ms = null;
  29. ds.Dispose();
  30. ds = null;
  31. return buffer;
  32. }
  33. /// <summary>
  34. ///
  35. /// </summary>
  36. /// <param name="buffer"></param>
  37. /// <returns></returns>
  38. public static DataSet ReSerializable(byte[] buffer)
  39. {
  40. MemoryStream ms = new MemoryStream(buffer);
  41. IFormatter bf = new BinaryFormatter();
  42. object obj = bf.Deserialize(ms);
  43. DataSet ds = (DataSet)obj;
  44. ms.Close();
  45. ms.Dispose();
  46. return ds;
  47. }
  48. #endregion
  49. #region 压缩、解压缩
  50. /// <summary>
  51. /// 压缩
  52. /// </summary>
  53. /// <param name="data"></param>
  54. /// <returns></returns>
  55. public static byte[] Compress(byte[] data)
  56. {
  57. MemoryStream ms = new MemoryStream();
  58. Stream zipStream = null;
  59. zipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress, true);
  60. //System.IO.Compression.CompressionMode:指定是否压缩和解压缩基础流(Compress:压缩,DeCompress:解压缩)
  61. zipStream.Write(data, 0, data.Length);
  62. zipStream.Close();
  63. ms.Position = 0;//获取或设置流中当前位置
  64. byte[] compressed_data = new byte[ms.Length];
  65. ms.Read(compressed_data, 0, int.Parse(ms.Length.ToString()));
  66. return compressed_data;
  67. }
  68. /// <summary>
  69. /// 解压缩
  70. /// </summary>
  71. /// <param name="data"></param>
  72. /// <returns></returns>
  73. public static byte[] Decompress(byte[] data)
  74. {
  75. try
  76. {
  77. MemoryStream ms = new MemoryStream(data);
  78. Stream zipStream = null;
  79. zipStream = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Decompress);
  80. byte[] dc_data = null;
  81. dc_data = EtractBytesFormStream(zipStream, data.Length);
  82. return dc_data;
  83. }
  84. catch
  85. {
  86. return null;
  87. }
  88. }
  89. public static byte[] EtractBytesFormStream(Stream zipStream, int dataBlock)
  90. {
  91. try
  92. {
  93. byte[] data = null;
  94. int totalBytesRead = 0;
  95. while (true)
  96. {
  97. Array.Resize(ref data, totalBytesRead + dataBlock + 1);
  98. int bytesRead = zipStream.Read(data, totalBytesRead, dataBlock);
  99. if (bytesRead == 0)
  100. {
  101. break;
  102. }
  103. totalBytesRead += bytesRead;
  104. }
  105. Array.Resize(ref data, totalBytesRead);
  106. return data;
  107. }
  108. catch
  109. {
  110. return null;
  111. }
  112. }
  113. #endregion
  114. }
  115. }