ZipHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.IO;
  2. using ICSharpCode.SharpZipLib.Zip.Compression;
  3. namespace ET
  4. {
  5. public static class ZipHelper
  6. {
  7. public static byte[] Compress(byte[] content)
  8. {
  9. //return content;
  10. Deflater compressor = new Deflater();
  11. compressor.SetLevel(Deflater.BEST_COMPRESSION);
  12. compressor.SetInput(content);
  13. compressor.Finish();
  14. using (MemoryStream bos = new MemoryStream(content.Length))
  15. {
  16. var buf = new byte[1024];
  17. while (!compressor.IsFinished)
  18. {
  19. int n = compressor.Deflate(buf);
  20. bos.Write(buf, 0, n);
  21. }
  22. return bos.ToArray();
  23. }
  24. }
  25. public static byte[] Decompress(byte[] content)
  26. {
  27. return Decompress(content, 0, content.Length);
  28. }
  29. public static byte[] Decompress(byte[] content, int offset, int count)
  30. {
  31. //return content;
  32. Inflater decompressor = new Inflater();
  33. decompressor.SetInput(content, offset, count);
  34. using (MemoryStream bos = new MemoryStream(content.Length))
  35. {
  36. var buf = new byte[1024];
  37. while (!decompressor.IsFinished)
  38. {
  39. int n = decompressor.Inflate(buf);
  40. bos.Write(buf, 0, n);
  41. }
  42. return bos.ToArray();
  43. }
  44. }
  45. }
  46. }
  47. /*
  48. using System.IO;
  49. using System.IO.Compression;
  50. namespace ET
  51. {
  52. public static class ZipHelper
  53. {
  54. public static byte[] Compress(byte[] content)
  55. {
  56. using (MemoryStream ms = new MemoryStream())
  57. using (DeflateStream stream = new DeflateStream(ms, CompressionMode.Compress, true))
  58. {
  59. stream.Write(content, 0, content.Length);
  60. return ms.ToArray();
  61. }
  62. }
  63. public static byte[] Decompress(byte[] content)
  64. {
  65. return Decompress(content, 0, content.Length);
  66. }
  67. public static byte[] Decompress(byte[] content, int offset, int count)
  68. {
  69. using (MemoryStream ms = new MemoryStream())
  70. using (DeflateStream stream = new DeflateStream(new MemoryStream(content, offset, count), CompressionMode.Decompress, true))
  71. {
  72. byte[] buffer = new byte[1024];
  73. while (true)
  74. {
  75. int bytesRead = stream.Read(buffer, 0, 1024);
  76. if (bytesRead == 0)
  77. {
  78. break;
  79. }
  80. ms.Write(buffer, 0, bytesRead);
  81. }
  82. return ms.ToArray();
  83. }
  84. }
  85. }
  86. }
  87. */