Exemplo n.º 1
0
        private void FileBT_Click(object sender, RoutedEventArgs e)
        {
            byte[]         file;
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.InitialDirectory = @"K:\PROJEKTY VS\GolombCoding\GolombCoding\bin";
            openFileDialog.DefaultExt       = ".txt";
            openFileDialog.Filter           = "Text documents (.txt)|*.txt";
            openFileDialog.Multiselect      = false;

            if (openFileDialog.ShowDialog() == true)
            {
                file = File.ReadAllBytes(openFileDialog.FileName);
            }
            else
            {
                file = new byte[] { }
            };

            LZ78 lz = new LZ78();

            List <List <Tuple <byte, byte> > > result = lz.Encode(file);

            TextWriter tw = new StreamWriter(@"..\LZ78Encode.txt");

            foreach (var list in result)
            {
                if (list.Count != 0)
                {
                    byte[] b = new byte[list.Count * 2];

                    for (int i = 0; i < list.Count; i++)
                    {
                        tw.Write(list[i].Item1 + " ");
                        tw.Write(list[i].Item2 + " ");

                        b[2 * i]     = list[i].Item1;
                        b[2 * i + 1] = list[i].Item2;
                    }

                    using (var stream = new FileStream(@"..\LZ78Encode.bin", FileMode.Append))
                    {
                        stream.Write(b, 0, b.Length);
                    }
                }
                else
                {
                    break;
                }
            }
            tw.Close();
        }
Exemplo n.º 2
0
        private void EncodeBT_Click(object sender, RoutedEventArgs e)
        {
            string encode = EncodeTB.Text;

            LZ78 lz = new LZ78();

            byte[] bytes = Encoding.ASCII.GetBytes(encode);

            List <List <Tuple <byte, byte> > > list = lz.Encode(bytes);

            EncodeLB.Text = "";

            foreach (var item in list)
            {
                for (int i = 0; i < item.Count; i++)
                {
                    EncodeLB.Text += string.Format("({0}, {1}) ", item[i].Item1, item[i].Item2);
                }
            }
        }