LZMAHelper.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.IO;
  3. public static class LZMAHelper
  4. {
  5. public enum LZMADictionarySize : Int32
  6. {
  7. Dict256KiB = 262144, // 2^18 = 262144 = 256 KiB
  8. Dict512KiB = 524288, // 2^19
  9. Dict1MiB = 1048576, // 2^20
  10. Dict2MiB = 2097152, // 2^21
  11. Dict4MiB = 4194304 // 2^22
  12. };
  13. public static LZMADictionarySize DefaultDictionarySize = LZMADictionarySize.Dict512KiB;
  14. public static int Compress(string inFile, string outFile)
  15. {
  16. var coder = new SevenZip.Compression.LZMA.Encoder();
  17. var input = new FileStream(inFile, FileMode.Open);
  18. var output = new FileStream(outFile, FileMode.Create);
  19. // Write the encoder properties
  20. coder.WriteCoderProperties(output);
  21. coder.SetCoderProperties(
  22. new SevenZip.CoderPropID[] { SevenZip.CoderPropID.DictionarySize },
  23. new object[] { (Int32)DefaultDictionarySize }
  24. );
  25. // Write the decompressed file size.
  26. output.Write(BitConverter.GetBytes(input.Length), 0, 4);
  27. // Encode the file.
  28. coder.Code(input, output, input.Length, -1, null);
  29. var ret = output.Length;
  30. output.Flush();
  31. output.Close();
  32. input.Close();
  33. return (int)ret;
  34. }
  35. public static int Compress(byte[] data, string outFile)
  36. {
  37. var coder = new SevenZip.Compression.LZMA.Encoder();
  38. var input = new MemoryStream(data);
  39. var output = new FileStream(outFile, FileMode.Create);
  40. coder.SetCoderProperties(
  41. new SevenZip.CoderPropID[] { SevenZip.CoderPropID.DictionarySize },
  42. new object[] { (Int32)DefaultDictionarySize }
  43. );
  44. // Write the encoder properties
  45. coder.WriteCoderProperties(output);
  46. // Write the decompressed file size.
  47. output.Write(BitConverter.GetBytes(input.Length), 0, 4);
  48. // Encode the file.
  49. coder.Code(input, output, input.Length, -1, null);
  50. var ret = output.Length;
  51. output.Flush();
  52. output.Close();
  53. input.Close();
  54. return (int)ret;
  55. }
  56. public static byte[] Compress(byte[] data)
  57. {
  58. var coder = new SevenZip.Compression.LZMA.Encoder();
  59. var input = new MemoryStream(data);
  60. var output = new MemoryStream();
  61. coder.SetCoderProperties(
  62. new SevenZip.CoderPropID[] { SevenZip.CoderPropID.DictionarySize },
  63. new object[] { (Int32)DefaultDictionarySize }
  64. );
  65. // Write the encoder properties
  66. coder.WriteCoderProperties(output);
  67. // Write the decompressed file size.
  68. output.Write(BitConverter.GetBytes(input.Length), 0, 4);
  69. // Encode the file.
  70. coder.Code(input, output, input.Length, -1, null);
  71. var ret = output.Length;
  72. output.Flush();
  73. output.Close();
  74. input.Close();
  75. return output.ToArray();
  76. }
  77. private static readonly ObjectPool<SevenZip.Compression.LZMA.Decoder> CoderPool = new ObjectPool<SevenZip.Compression.LZMA.Decoder>(
  78. () =>
  79. {
  80. return new SevenZip.Compression.LZMA.Decoder();
  81. });
  82. public static void Decompress(string inFile, string outFile)
  83. {
  84. var coder = CoderPool.Get();
  85. //var coder = new SevenZip.Compression.LZMA.Decoder();
  86. var input = new FileStream(inFile, FileMode.Open);
  87. var output = new FileStream(outFile, FileMode.Create);
  88. // Read the decoder properties
  89. var properties = new byte[5];
  90. input.Read(properties, 0, 5);
  91. // Read in the decompress file size.
  92. var fileLengthBytes = new byte[4];
  93. input.Read(fileLengthBytes, 0, 4);
  94. var fileLength = BitConverter.ToInt32(fileLengthBytes, 0);
  95. // Decompress the file.
  96. coder.SetDecoderProperties(properties);
  97. coder.Code(input, output, input.Length, fileLength, null);
  98. output.Flush();
  99. output.Close();
  100. input.Close();
  101. CoderPool.Put(coder);
  102. }
  103. public static void Decompress(byte[] data, string outFile)
  104. {
  105. var coder = CoderPool.Get();
  106. //var coder = new SevenZip.Compression.LZMA.Decoder();
  107. var input = new MemoryStream(data);
  108. var output = new FileStream(outFile, FileMode.Create);
  109. // Read the decoder properties
  110. var properties = new byte[5];
  111. input.Read(properties, 0, 5);
  112. // Read in the decompress file size.
  113. var fileLengthBytes = new byte[4];
  114. input.Read(fileLengthBytes, 0, 4);
  115. var fileLength = BitConverter.ToInt32(fileLengthBytes, 0);
  116. // Decompress the file.
  117. coder.SetDecoderProperties(properties);
  118. coder.Code(input, output, input.Length, fileLength, null);
  119. output.Flush();
  120. output.Close();
  121. input.Close();
  122. CoderPool.Put(coder);
  123. }
  124. }