void BuildFreqArray() { for (int i = 0; i < 256; i++) { MeetingBytes[i] = new ByteCount((byte)i, 0); } for (int i = 0; i < Data.Length; i++) { MeetingBytes[Data[i]].Count++; } Array.Sort(MeetingBytes, (A, B) => B.Count.CompareTo(A.Count)); for (int i = 0; i < MeetingBytes.Length; i++) { if (MeetingBytes[i].Count == 0) { ByteCount[] TempAr = new ByteCount[i]; Array.Copy(MeetingBytes, 0, TempAr, 0, i); MeetingBytes = TempAr; break; } } }
public HaffmanTreeNode(ByteCount ByteCount) { this.Value = ByteCount.Byte; this.ByteCount = ByteCount.Count; this.Left = null; this.Right = null; this.Parent = null; }
public bool Decompress(string SourceFile, string DestFile) { int BytePos = 0; byte BitPos = 0; StreamReader Reader = new StreamReader(SourceFile); byte[] CompressData = new byte[Reader.BaseStream.Length]; Reader.BaseStream.Read(CompressData, 0, (int)Reader.BaseStream.Length); Reader.Close(); // чтение длины результирующего файла int Length = 0; Length |= (int)CompressData[BytePos++] << 24; Length |= (int)CompressData[BytePos++] << 16; Length |= (int)CompressData[BytePos++] << 8; Length |= (int)CompressData[BytePos++]; Data = new byte[Length]; // чтение таблицы символов //Tree.ReadHeader(CompressData, ref BytePos, ref BitPos); MeetingBytes = new ByteCount[CompressData[BytePos++]]; for (int i = 0; i < MeetingBytes.Length; i++) { MeetingBytes[i] = new ByteCount(CompressData[BytePos++], (CompressData[BytePos++] << 24) + (CompressData[BytePos++] << 16) + (CompressData[BytePos++] << 8) + CompressData[BytePos++]); } Tree = new HaffmanTree(MeetingBytes); // чтение байтов сжатого массива байтов Tree.DecodeBytes(CompressData, ref Data, ref BytePos, ref BitPos); StreamWriter Writer = new StreamWriter(DestFile); Writer.BaseStream.Write(Data, 0, Length); Writer.Close(); /* * byte[] SavedHash = new byte[16]; * Array.Copy(CompressData, BytePos, SavedHash, 0, 16); * if (MD5.Create().ComputeHash(Data).SequenceEqual(SavedHash)) * return true; * else * return false;*/ return(true); }