public void Write(MyBinaryWriter writer, Action <MyBinaryWriter, T> write) { writer.Write7BitEncodedInt(Width); writer.Write7BitEncodedInt(Height); for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { write(writer, m_Array[x, y]); } } }
public static void CompressFileLZMA(MemoryStream inFile, string outFile) { int dictionary = 33554432; int posStateBits = 2; int litContextBits = 3; int litPosBits = 0; int algorithm = 2; int numFastBytes = 32; 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 = { dictionary, posStateBits, litContextBits, litPosBits, algorithm, numFastBytes, mf, eos }; byte[] TibiaHeader = { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 }; using FileStream outStream = new FileStream(outFile, FileMode.Create); outStream.Write(TibiaHeader); SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder(); encoder.SetCoderProperties(propIDs, properties); encoder.WriteCoderProperties(outStream); long fileSize; if (eos || stdInMode) { fileSize = -1; } else { fileSize = inFile.Length; } for (int i = 0; i < 8; i++) { outStream.WriteByte((Byte)(fileSize >> (8 * i))); } encoder.Code(inFile, outStream, -1, -1, null); MemoryStream tmp = new MemoryStream(); int size = (int)outStream.Length - 32; MyBinaryWriter bw = new MyBinaryWriter(tmp); bw.Write(new byte[] { 0x70, 0x0A, 0xFA, 0x80, 0x24 }); bw.Write7BitEncodedInt(size); if (tmp.Length == 8) { outStream.Position = 0x18; } else { outStream.Position = 0x19; } outStream.Write(tmp.ToArray(), 0, (int)tmp.Length); outStream.Position = 0; outStream.Close(); }