internal static void CompressToStream(Stream inStream, Stream outStream) { #if iOS lock (staticEncoderLocker) { LZMA.Encoder encoder = staticEncoder; #else LZMA.Encoder encoder = new LZMA.Encoder(); #endif encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1); #if iOS } #endif }
/// <summary> /// 调用7Z的压缩数据接口 /// </summary> /// <param name="inpbuf">将要压缩的二进制数据</param> /// <returns></returns> public static byte[] LzmaCompress(byte[] inpbuf) { try { CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { 23, 2, 3, 2, 1, 128, "bt4", true }; var enc = new LZMA.Encoder(); enc.SetCoderProperties(propIDs, properties); //压缩的数据文件 var msInp = new MemoryStream(inpbuf); //输出的压缩数据 var msOut = new MemoryStream(); enc.WriteCoderProperties(msOut); enc.Code(msInp, msOut, -1, -1, null); return(msOut.ToArray()); } catch (Exception) { return(null); } }
internal static byte[] Compress(byte[] inputBytes) { #if iOS lock (staticEncoderLocker) { LZMA.Encoder encoder = staticEncoder; #else LZMA.Encoder encoder = new LZMA.Encoder(); #endif MemoryStream inStream = new MemoryStream(inputBytes); MemoryStream outStream = new MemoryStream(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize = inStream.Length; for (int i = 0; i < 8; i++) outStream.WriteByte((Byte)(fileSize >> (8 * i))); encoder.Code(inStream, outStream, -1, -1); return outStream.ToArray(); #if iOS } #endif }
public static Stream CompressStreamLZMA(Stream inStream) { Int32 dictionary = 1 << 23; Int32 posStateBits = 2; Int32 litContextBits = 3; // for normal files // UInt32 litContextBits = 0; // for 32-bit data Int32 litPosBits = 0; // UInt32 litPosBits = 2; // for 32-bit data Int32 algorithm = 2; Int32 numFastBytes = 128; string mf = "bt4"; bool eos = true; bool stdInMode = false; CoderPropID[] propIDs = { CoderPropID.DictionarySize, CoderPropID.PosStateBits, CoderPropID.LitContextBits, CoderPropID.LitPosBits, CoderPropID.Algorithm, CoderPropID.NumFastBytes, CoderPropID.MatchFinder, CoderPropID.EndMarker }; object[] properties = { (Int32)(dictionary), (Int32)(posStateBits), (Int32)(litContextBits), (Int32)(litPosBits), (Int32)(algorithm), (Int32)(numFastBytes), mf, eos }; var outStream = new MemoryStream(); LZMA.Encoder encoder = new LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); Int64 fileSize; if (eos || stdInMode) { fileSize = -1; } else { fileSize = inStream.Length; } for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inStream, outStream, -1, -1, null); outStream.Seek(0, SeekOrigin.Begin); return(outStream); }