DataSetSerializerDeserialize.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Linq;
  7. using System.Runtime.Serialization;
  8. using System.Runtime.Serialization.Formatters.Binary;
  9. using System.Text;
  10. namespace LYFZ.WinAPI
  11. {
  12. /// <summary>
  13. /// DataSet对象序列化并压缩和解压缩及反序列化
  14. /// </summary>
  15. public class DataSetSerializerDeserialize
  16. {
  17. public DataSetSerializerDeserialize() {
  18. }
  19. #region 2017-02-22 重新整理
  20. /// <summary>
  21. /// 不压缩直接序列化object到byte[]
  22. /// </summary>
  23. /// <param name="ds"></param>
  24. public static byte[] ObjectSerializerRetBytes(object obj)
  25. {
  26. if (obj != null)
  27. {
  28. IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
  29. using (MemoryStream ms = new MemoryStream())//创建内存流对象
  30. {
  31. formatter.Serialize(ms, obj);//把DataSet对象序列化到内存流
  32. byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
  33. ms.Close();//关闭内存流对象
  34. ms.Dispose();//释放资源
  35. return buffer;
  36. }
  37. }
  38. else
  39. {
  40. return null;
  41. }
  42. }
  43. /// <summary>
  44. /// 反序列化未压缩的object
  45. /// </summary>
  46. /// <param name="data"></param>
  47. /// <returns></returns>
  48. public static object ObjectDeserialize(byte[] data)
  49. {
  50. if (data != null)
  51. {
  52. using (MemoryStream gzipms = new MemoryStream())//创建内存流对象
  53. {
  54. gzipms.Write(data, 0, data.Length);
  55. gzipms.Position = 0;
  56. BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
  57. try
  58. {
  59. if (gzipms.Length > 0)
  60. {
  61. return sfFormatter.Deserialize(gzipms);//反序列化
  62. }
  63. else
  64. {
  65. return null;
  66. }
  67. }
  68. catch
  69. {
  70. throw;
  71. }
  72. finally
  73. {
  74. gzipms.Close();//关闭内存流
  75. gzipms.Dispose();//释放资源
  76. }
  77. }
  78. }
  79. else
  80. {
  81. return null;
  82. }
  83. }
  84. /// <summary>
  85. /// 序列化object对象并压缩到byte[]
  86. /// </summary>
  87. /// <param name="ds"></param>
  88. public static byte[] ObjectSerializerCompressionRetBytes(object obj)
  89. {
  90. IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
  91. using (MemoryStream ms = new MemoryStream(), gzipms = new MemoryStream())//创建内存流对象
  92. {
  93. formatter.Serialize(ms, obj);//把DataSet对象序列化到内存流
  94. byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
  95. ms.Close();//关闭内存流对象
  96. ms.Dispose();//释放资源
  97. // MemoryStream gzipms = new MemoryStream();//创建内存流对象
  98. using (GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Compress, true))//创建压缩对象
  99. {
  100. gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件
  101. gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
  102. gzipStream.Dispose();//释放对象
  103. }
  104. gzipms.Position = 0;
  105. byte[] retbuffer = gzipms.ToArray();
  106. gzipms.Close();
  107. gzipms.Dispose();
  108. return retbuffer;
  109. }
  110. }
  111. /// <summary>
  112. /// 反序列化压缩的Object
  113. /// </summary>
  114. /// <param name="_filePath"></param>
  115. /// <returns></returns>
  116. public static object ObjectDeserializeDecompress(byte[] data)
  117. {
  118. using (MemoryStream gzipms = new MemoryStream(), decompressms = new MemoryStream())//创建内存流对象
  119. {
  120. gzipms.Write(data, 0, data.Length);
  121. gzipms.Position = 0;
  122. using (GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Decompress))//创建解压对象
  123. {
  124. byte[] buffer = new byte[4096];
  125. int offset = 0;//定义读取位置
  126. // MemoryStream decompressms = new MemoryStream();//定义解压的内存流
  127. try
  128. {
  129. while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
  130. {
  131. decompressms.Write(buffer, 0, offset);//解压后的数据写入内存流
  132. }
  133. }
  134. catch (Exception e)
  135. {
  136. throw e;
  137. }
  138. finally
  139. {
  140. gzipms.Close();
  141. gzipms.Dispose();
  142. gzipStream.Close();//关闭解压缩流
  143. gzipStream.Dispose();//释放资源
  144. }
  145. }
  146. decompressms.Position = 0;
  147. decompressms.Seek(0, SeekOrigin.Begin);
  148. BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
  149. try
  150. {
  151. return sfFormatter.Deserialize(decompressms);//反序列化
  152. }
  153. catch
  154. {
  155. throw;
  156. }
  157. finally
  158. {
  159. decompressms.Close();//关闭内存流
  160. decompressms.Dispose();//释放资源
  161. }
  162. }
  163. }
  164. #endregion
  165. /// <summary>
  166. /// 序列化DataSet对象并压缩
  167. /// </summary>
  168. /// <param name="ds"></param>
  169. public static void DataSetSerializerCompression(DataSet ds)
  170. {
  171. IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
  172. MemoryStream ms = new MemoryStream();//创建内存流对象
  173. formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流
  174. byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
  175. ms.Close();//关闭内存流对象
  176. ms.Dispose();//释放资源
  177. FileStream fs = File.Create("datasetCompression.dat");//创建文件
  178. GZipStream gzipStream = new GZipStream(fs, CompressionMode.Compress, true);//创建压缩对象
  179. gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件
  180. gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
  181. gzipStream.Dispose();//释放对象
  182. fs.Close();//关闭流
  183. fs.Dispose();//释放对象
  184. }
  185. /// <summary>
  186. /// 把byte[]压缩并返回压缩后的byte[]
  187. /// </summary>
  188. /// <param name="dataArray"></param>
  189. public static byte[] DataCompressionRetBytes(byte[] dataArray)
  190. {
  191. try
  192. {
  193. MemoryStream gzipms = new MemoryStream();//创建内存流对象
  194. GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Compress, true);//创建压缩对象
  195. gzipStream.Write(dataArray, 0, dataArray.Length);//把压缩后的数据写入文件
  196. gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
  197. byte[] retDataArray = gzipms.ToArray();
  198. gzipStream.Dispose();//释放对象
  199. gzipms.Position = 0;
  200. return gzipms.ToArray();
  201. }
  202. catch {
  203. return new byte[0];
  204. }
  205. }
  206. /// <summary>
  207. /// 解压缩并返回解压缩后的byte[]
  208. /// </summary>
  209. /// <param name="dataArray"></param>
  210. /// <returns></returns>
  211. public static byte[] DataDecompressRetBytes(byte[] dataArray)
  212. {
  213. try
  214. {
  215. MemoryStream gzipms = new MemoryStream();//创建内存流对象
  216. gzipms.Write(dataArray, 0, dataArray.Length);
  217. gzipms.Position = 0;
  218. GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Decompress);//创建解压对象
  219. byte[] buffer = new byte[4096];
  220. int offset = 0;//定义读取位置
  221. MemoryStream decompressms = new MemoryStream();//定义解压的内存流
  222. while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
  223. {
  224. decompressms.Write(buffer, 0, offset);//解压后的数据写入内存流
  225. }
  226. gzipms.Close();
  227. gzipms.Dispose();
  228. decompressms.Position = 0;
  229. byte[] retDataArray = decompressms.ToArray();
  230. decompressms.Close();//关闭内存流
  231. decompressms.Dispose();//释放资源
  232. gzipStream.Close();//关闭解压缩流
  233. gzipStream.Dispose();//释放资源
  234. return retDataArray;
  235. }
  236. catch
  237. {
  238. return new byte[0];
  239. }
  240. }
  241. /// <summary>
  242. /// 序列化DataSet对象并压缩到byte[]
  243. /// </summary>
  244. /// <param name="ds"></param>
  245. public static byte[] DataSetSerializerCompressionRetBytes(DataSet ds)
  246. {
  247. IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
  248. MemoryStream ms = new MemoryStream();//创建内存流对象
  249. formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流
  250. byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
  251. ds.Clear();
  252. ds.Dispose();
  253. ds = null;
  254. ms.Close();//关闭内存流对象
  255. ms.Dispose();//释放资源
  256. MemoryStream gzipms = new MemoryStream();//创建内存流对象
  257. GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Compress, true);//创建压缩对象
  258. gzipStream.Write(buffer, 0, buffer.Length);//把压缩后的数据写入文件
  259. gzipStream.Close();//关闭压缩流,这里要注意:一定要关闭,要不然解压缩的时候会出现小于4K的文件读取不到数据,大于4K的文件读取不完整
  260. gzipStream.Dispose();//释放对象
  261. gzipms.Position = 0;
  262. byte[] retbuffer = gzipms.ToArray();
  263. gzipms.Close();
  264. gzipms.Dispose();
  265. return retbuffer;
  266. }
  267. /// <summary>
  268. /// 不压缩直接序列化DataSet到byte[]
  269. /// </summary>
  270. /// <param name="ds"></param>
  271. public static byte[] DataSetSerializerRetBytes(DataSet ds)
  272. {
  273. IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
  274. MemoryStream ms = new MemoryStream();//创建内存流对象
  275. formatter.Serialize(ms, ds);//把DataSet对象序列化到内存流
  276. byte[] buffer = ms.ToArray();//把内存流对象写入字节数组
  277. ms.Close();//关闭内存流对象
  278. ms.Dispose();//释放资源
  279. ds.Clear();
  280. ds.Dispose();
  281. ds = null;
  282. return buffer;
  283. }
  284. /// <summary>
  285. /// 不压缩直接序列化DataSet
  286. /// </summary>
  287. /// <param name="ds"></param>
  288. public static void DataSetSerializer(DataSet ds)
  289. {
  290. IFormatter formatter = new BinaryFormatter();//定义BinaryFormatter以序列化DataSet对象
  291. FileStream fs = File.Create("dataset.dat");//创建文件
  292. formatter.Serialize(fs, ds);//把DataSet对象序列化到文件
  293. fs.Close();//关闭流
  294. fs.Dispose();//释放对象
  295. }
  296. /// <summary>
  297. /// 反序列化压缩的DataSet
  298. /// </summary>
  299. /// <param name="_filePath"></param>
  300. /// <returns></returns>
  301. public static DataSet DataSetDeserializeDecompress(byte[] data)
  302. {
  303. MemoryStream gzipms = new MemoryStream();//创建内存流对象
  304. gzipms.Write(data, 0, data.Length);
  305. gzipms.Position = 0;
  306. GZipStream gzipStream = new GZipStream(gzipms, CompressionMode.Decompress);//创建解压对象
  307. byte[] buffer = new byte[4096];
  308. int offset = 0;//定义读取位置
  309. MemoryStream decompressms = new MemoryStream();//定义解压的内存流
  310. try
  311. {
  312. while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
  313. {
  314. decompressms.Write(buffer, 0, offset);//解压后的数据写入内存流
  315. }
  316. }
  317. catch (Exception e)
  318. {
  319. throw e;
  320. }
  321. finally {
  322. gzipms.Close();
  323. gzipms.Dispose();
  324. }
  325. decompressms.Position = 0;
  326. decompressms.Seek(0, SeekOrigin.Begin);
  327. BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
  328. DataSet ds;
  329. try
  330. {
  331. ds = (DataSet)sfFormatter.Deserialize(decompressms);//反序列化
  332. }
  333. catch
  334. {
  335. throw;
  336. }
  337. finally
  338. {
  339. decompressms.Close();//关闭内存流
  340. decompressms.Dispose();//释放资源
  341. }
  342. gzipStream.Close();//关闭解压缩流
  343. gzipStream.Dispose();//释放资源
  344. return ds;
  345. }
  346. /// <summary>
  347. /// 反序列化压缩的DataSet
  348. /// </summary>
  349. /// <param name="_filePath"></param>
  350. /// <returns></returns>
  351. public static DataSet DataSetDeserializeDecompress(string _filePath)
  352. {
  353. FileStream fs = File.OpenRead(_filePath);//打开文件
  354. fs.Position = 0;//设置文件流的位置
  355. GZipStream gzipStream = new GZipStream(fs, CompressionMode.Decompress);//创建解压对象
  356. byte[] buffer = new byte[4096];//定义数据缓冲
  357. int offset = 0;//定义读取位置
  358. MemoryStream ms = new MemoryStream();//定义内存流
  359. while ((offset = gzipStream.Read(buffer, 0, buffer.Length)) != 0)
  360. {
  361. ms.Write(buffer, 0, offset);//解压后的数据写入内存流
  362. }
  363. BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
  364. ms.Position = 0;//设置内存流的位置
  365. DataSet ds;
  366. try
  367. {
  368. ds = (DataSet)sfFormatter.Deserialize(ms);//反序列化
  369. }
  370. catch
  371. {
  372. throw;
  373. }
  374. finally
  375. {
  376. ms.Close();//关闭内存流
  377. ms.Dispose();//释放资源
  378. }
  379. fs.Close();//关闭文件流
  380. fs.Dispose();//释放资源
  381. gzipStream.Close();//关闭解压缩流
  382. gzipStream.Dispose();//释放资源
  383. return ds;
  384. }
  385. /// <summary>
  386. /// 反序列化未压缩的DataSet
  387. /// </summary>
  388. /// <param name="_filePath"></param>
  389. /// <returns></returns>
  390. public static DataSet DataSetDeserialize(byte[] data)
  391. {
  392. MemoryStream gzipms = new MemoryStream();//创建内存流对象
  393. gzipms.Write(data, 0, data.Length);
  394. gzipms.Position = 0;
  395. BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
  396. DataSet ds;
  397. try
  398. {
  399. ds = (DataSet)sfFormatter.Deserialize(gzipms);//反序列化
  400. }
  401. catch
  402. {
  403. throw;
  404. }
  405. finally
  406. {
  407. gzipms.Close();//关闭内存流
  408. gzipms.Dispose();//释放资源
  409. }
  410. return ds;
  411. }
  412. /// <summary>
  413. /// 反序列化未压缩的DataSet
  414. /// </summary>
  415. /// <param name="_filePath"></param>
  416. /// <returns></returns>
  417. public static DataSet DataSetDeserialize(string _filePath)
  418. {
  419. FileStream fs = File.OpenRead(_filePath);//打开文件
  420. fs.Position = 0;//设置文件流的位置
  421. BinaryFormatter sfFormatter = new BinaryFormatter();//定义BinaryFormatter以反序列化DataSet对象
  422. DataSet ds;
  423. try
  424. {
  425. ds = (DataSet)sfFormatter.Deserialize(fs);//反序列化
  426. }
  427. catch
  428. {
  429. throw;
  430. }
  431. finally
  432. {
  433. fs.Close();//关闭内存流
  434. fs.Dispose();//释放资源
  435. }
  436. fs.Close();//关闭文件流
  437. fs.Dispose();//释放资源
  438. return ds;
  439. }
  440. /* static void Main(string[] args)
  441. {
  442. DataSet dataSet = new DataSet();
  443. DataSetSerializer(dataSet);
  444. DataSetSerializerCompression(dataSet);
  445. } */
  446. }
  447. }