Esempio n. 1
0
        static void encode(string input, string fileName)
        {
            using (var file = new FileStream(fileName, FileMode.Create))
            {
            }

            var coder = new Coder();
            // input = input.Replace("\r","");
            var encodeStr = new StringBuilder(coder.Encode(input + "\n\0\n", fileName));
            var builder   = new StringBuilder();

            foreach (var freq in coder.getFrequencies())
            {
                builder.Append(freq.Key);
                builder.Append(" ");
                builder.Append(freq.Value);
                builder.Append("\n");
            }

            builder.Append("\n\0\n");

            var writer = new BinStream(fileName);

            writer.WriteBits(new BitArray(Encoding.GetEncoding(1251).GetBytes(builder.ToString())), FileMode.Append);

            OutputBits(new BinStream(fileName), encodeStr.ToString(), fileName);
        }
Esempio n. 2
0
        static void decode(string fileName)
        {
            var coder = new Coder();

            BitArray fileBits;
            var      tree = new StringBuilder();

            using (var file = new FileStream(fileName, FileMode.Open))
            {
                using (var reader = new BinaryReader(file))
                {
                    reader.BaseStream.Seek(0, SeekOrigin.Begin);
                    var tmp = new char[3];
                    while (true)
                    {
                        if ((tmp[0] = Encoding.GetEncoding(1251).GetString(new byte[1] {
                            reader.ReadByte()
                        })[0]) == '\n'
                            & (tmp[1] = Encoding.GetEncoding(1251).GetString(new byte[1] {
                            reader.ReadByte()
                        })[0]) ==
                            '\0'
                            & (tmp[2] = Encoding.GetEncoding(1251).GetString(new byte[1] {
                            reader.ReadByte()
                        })[0]) ==
                            '\n')
                        {
                            break;
                        }
                        reader.BaseStream.Seek(-3, SeekOrigin.Current);
                        while ((tmp[0] = Encoding.GetEncoding(1251).GetString(new byte[1] {
                            reader.ReadByte()
                        })[0]) !=
                               ' ')
                        {
                            tree.Append((char)tmp[0]);
                        }
                        if ((tmp[1] = Encoding.GetEncoding(1251).GetString(new byte[1] {
                            reader.ReadByte()
                        })[0]) ==
                            ' ')
                        {
                            tree.Append((char)tmp[0]);
                        }
                        reader.BaseStream.Seek(-1, SeekOrigin.Current);

                        while ((tmp[0] = Encoding.GetEncoding(1251).GetString(new byte[1] {
                            reader.ReadByte()
                        })[0]) !=
                               '\n')
                        {
                            tree.Append((char)tmp[0]);
                        }

                        tree.Append('\n');
                    }

                    reader.BaseStream.Seek(1, SeekOrigin.Current);
                    fileBits = new BinStream().ReadBits((reader.ReadBytes((int)(file.Length - reader.BaseStream.Position))));
                    var dict = new Dictionary <char, decimal>();
                    for (int i = 0; i < tree.Length - 1; i++)
                    {
                        var num = new StringBuilder();
                        var c   = tree[i];
                        i++;
                        char n;
                        while ((n = tree[i]) != '\n')
                        {
                            num.Append(n);
                            i++;
                        }
                        // if(c != '\r')
                        dict.Add(c, int.Parse(num.ToString()));
                    }

                    var builder = new StringBuilder();
                    for (int i = 0; i < fileBits.Length; i++)
                    {
                        builder.Append(fileBits.Get(i) ? '1' : '0');
                    }

                    var encoded = FromBinary(builder.ToString()).ToString();
                    var decoded = coder.Decode(encoded.ToString(), dict);
                    File.WriteAllText("DEC" + fileName.Remove(fileName.LastIndexOf(".")),
                                      decoded.Remove(decoded.Length - 2),
                                      Encoding.GetEncoding(1251));
                }
            }
        }