private void bBrowse_Click(object sender, EventArgs e) { symbols = new Symbols(); DialogResult result = ofd1.ShowDialog(); if (result == DialogResult.OK) { string file = ofd1.FileName; try { _openedFile = new FileInfo(ofd1.FileName); text = File.ReadAllText(file); lLength.Text = text.Length.ToString(); foreach (char symbol in text) { if (symbols.Contains(symbol)) { symbols.AddPresence(symbol); } else { symbols.Add(symbol); } } symbols.SortDescending(); lAlphabetLength.Text = symbols.List.Count.ToString(); gAlphabet.Visible = true; gAlphabet.DataSource = symbols.List; encodeButton.Visible = true; } catch (IOException) { } } }
public void Build(Symbols symbols) { foreach (Symbol symbol in symbols.List) { nodes.Add(new Node() { Symbol = symbol.symbol, Frequency = symbol.presence }); } while (nodes.Count > 1) { List <Node> orderedNodes = nodes.OrderBy(node => node.Frequency).ToList <Node>(); if (orderedNodes.Count >= 2) { // Take first two items List <Node> taken = orderedNodes.Take(2).ToList <Node>(); // Create a parent node by combining the frequencies Node parent = new Node() { Symbol = '*', Frequency = taken[0].Frequency + taken[1].Frequency, Left = taken[0], Right = taken[1] }; nodes.Remove(taken[0]); nodes.Remove(taken[1]); nodes.Add(parent); } this.Root = nodes.FirstOrDefault(); } }
//przycisk odczytu i dekompresji pliku private void readFileButton_Click(object sender, EventArgs e) { huffmanTree = new HuffmanTree(); symbols = new Symbols(); openFile = new OpenFileDialog(); openFile.DefaultExt = ".compress"; openFile.Filter = "Compressed files|*.compress"; if (openFile.ShowDialog() == DialogResult.OK) { //otworzenie strumienia do odczytu binarnego z pliku using (BinaryReader reader = new BinaryReader(File.Open(openFile.FileName, FileMode.Open))) { //odczytywanie naglowka //odczytanie dlugosci alfabetu int count = reader.ReadInt32(); //odczytanie oraz odtworzenie listy symboli razem z liczba ich wystepowania for (int i = 0; i < count; i++) { symbols.Add(reader.ReadChar(), reader.ReadUInt16()); } //odtworzenie drzewa Huffmana na podstawie listy symboli przekonwertowanej do Dictionary huffmanTree.Build(symbols); //wyznaczenie pozostalej dlugosci pliku po odczytaniu naglowka int length = Convert.ToInt32(reader.BaseStream.Length - reader.BaseStream.Position); byte[] bytes = new byte[length]; //odczyt kolejnych bajtow skompresowanych danych az do konca pliku for (int i = 0; i < length; i++) { bytes[i] = reader.ReadByte(); } //konwersja tablicy bajtow na BitArray BitArray bitText = new BitArray(bytes); //usuniecie zer nieznaczacych celem unikniecia nadmiaru danych int amountOfLeadingZeros = 8 - Convert.ToString(bytes[length - 1], 2).Length; bitText.Length -= amountOfLeadingZeros; //wypisanie odczytanej, zakodowanej tresci pliku fileTextBox.Text = ""; if (bitText.Count < 1000) { foreach (bool bit in bitText) { fileTextBox.Text += ((bit ? 1 : 0) + ""); } } else { fileTextBox.Text = "Sekwencja zbyt długa do wyświetlenia"; } //dekodowanie i wyswietlenie zdekodowanej tresci string decoded = huffmanTree.Decode(bitText); decodedTextBox.Text = decoded; ShowAlphabet(); lLength.Text = decoded.Length.ToString(); lAlphabetLength.Text = symbols.List.Count.ToString(); } } }