123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.IO;
- public static class LZMAHelper
- {
- public enum LZMADictionarySize : Int32
- {
- Dict256KiB = 262144,
- Dict512KiB = 524288,
- Dict1MiB = 1048576,
- Dict2MiB = 2097152,
- Dict4MiB = 4194304
- };
- public static LZMADictionarySize DefaultDictionarySize = LZMADictionarySize.Dict512KiB;
- public static int Compress(string inFile, string outFile)
- {
- var coder = new SevenZip.Compression.LZMA.Encoder();
- var input = new FileStream(inFile, FileMode.Open);
- var output = new FileStream(outFile, FileMode.Create);
-
- coder.WriteCoderProperties(output);
- coder.SetCoderProperties(
- new SevenZip.CoderPropID[] { SevenZip.CoderPropID.DictionarySize },
- new object[] { (Int32)DefaultDictionarySize }
- );
-
- output.Write(BitConverter.GetBytes(input.Length), 0, 4);
-
- coder.Code(input, output, input.Length, -1, null);
- var ret = output.Length;
- output.Flush();
- output.Close();
- input.Close();
- return (int)ret;
- }
- public static int Compress(byte[] data, string outFile)
- {
- var coder = new SevenZip.Compression.LZMA.Encoder();
- var input = new MemoryStream(data);
- var output = new FileStream(outFile, FileMode.Create);
- coder.SetCoderProperties(
- new SevenZip.CoderPropID[] { SevenZip.CoderPropID.DictionarySize },
- new object[] { (Int32)DefaultDictionarySize }
- );
-
- coder.WriteCoderProperties(output);
-
- output.Write(BitConverter.GetBytes(input.Length), 0, 4);
-
- coder.Code(input, output, input.Length, -1, null);
- var ret = output.Length;
- output.Flush();
- output.Close();
- input.Close();
- return (int)ret;
- }
- public static byte[] Compress(byte[] data)
- {
- var coder = new SevenZip.Compression.LZMA.Encoder();
- var input = new MemoryStream(data);
- var output = new MemoryStream();
- coder.SetCoderProperties(
- new SevenZip.CoderPropID[] { SevenZip.CoderPropID.DictionarySize },
- new object[] { (Int32)DefaultDictionarySize }
- );
-
- coder.WriteCoderProperties(output);
-
- output.Write(BitConverter.GetBytes(input.Length), 0, 4);
-
- coder.Code(input, output, input.Length, -1, null);
- var ret = output.Length;
- output.Flush();
- output.Close();
- input.Close();
- return output.ToArray();
- }
- private static readonly ObjectPool<SevenZip.Compression.LZMA.Decoder> CoderPool = new ObjectPool<SevenZip.Compression.LZMA.Decoder>(
- () =>
- {
- return new SevenZip.Compression.LZMA.Decoder();
- });
- public static void Decompress(string inFile, string outFile)
- {
- var coder = CoderPool.Get();
-
- var input = new FileStream(inFile, FileMode.Open);
- var output = new FileStream(outFile, FileMode.Create);
-
- var properties = new byte[5];
- input.Read(properties, 0, 5);
-
- var fileLengthBytes = new byte[4];
- input.Read(fileLengthBytes, 0, 4);
- var fileLength = BitConverter.ToInt32(fileLengthBytes, 0);
-
- coder.SetDecoderProperties(properties);
- coder.Code(input, output, input.Length, fileLength, null);
- output.Flush();
- output.Close();
- input.Close();
- CoderPool.Put(coder);
- }
- public static void Decompress(byte[] data, string outFile)
- {
- var coder = CoderPool.Get();
-
- var input = new MemoryStream(data);
- var output = new FileStream(outFile, FileMode.Create);
-
- var properties = new byte[5];
- input.Read(properties, 0, 5);
-
- var fileLengthBytes = new byte[4];
- input.Read(fileLengthBytes, 0, 4);
- var fileLength = BitConverter.ToInt32(fileLengthBytes, 0);
-
- coder.SetDecoderProperties(properties);
- coder.Code(input, output, input.Length, fileLength, null);
- output.Flush();
- output.Close();
- input.Close();
- CoderPool.Put(coder);
- }
- }
|