public static byte[] Compress(byte[] buffer)
{
using (MemoryStream ms = new MemoryStream())
using (BinaryWriter writer = new BinaryWriter(ms))
{
byte[] crc = CryptoHelper.CRCHash(buffer);
writer.Write(VZipHeader);
writer.Write((byte)Version);
writer.Write(crc);
Int32 dictionary = 1 << 23;
Int32 posStateBits = 2;
Int32 litContextBits = 3;
Int32 litPosBits = 0;
Int32 algorithm = 2;
Int32 numFastBytes = 128;
SevenZip.CoderPropID[] propIDs =
{
SevenZip.CoderPropID.DictionarySize,
SevenZip.CoderPropID.PosStateBits,
SevenZip.CoderPropID.LitContextBits,
SevenZip.CoderPropID.LitPosBits,
SevenZip.CoderPropID.Algorithm,
SevenZip.CoderPropID.NumFastBytes,
SevenZip.CoderPropID.MatchFinder,
SevenZip.CoderPropID.EndMarker
};
object[] properties =
{
(Int32)(dictionary),
(Int32)(posStateBits),
(Int32)(litContextBits),
(Int32)(litPosBits),
(Int32)(algorithm),
(Int32)(numFastBytes),
"bt4",
false
};
SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
encoder.SetCoderProperties(propIDs, properties);
encoder.WriteCoderProperties(ms);
using (MemoryStream input = new MemoryStream(buffer)) {
encoder.Code(input, ms, -1, -1, null);
}
writer.Write(crc);
writer.Write((uint)buffer.Length);
writer.Write(VZipFooter);
return(ms.ToArray());
}
}