Exemplo n.º 1
0
        public void Encode()
        {
            var map   = CountChars();
            var tree  = new HTree(map.OrderBy(pair => pair.Value).ThenByDescending(pair => pair.Key));
            var table = tree.Table;

            Encrypt(map, table);
        }
Exemplo n.º 2
0
        public void Decode()
        {
            var stream = new FileStream(_path, FileMode.Open, FileAccess.Read);
            var map    = CreateCountMap(stream);

            var tree        = new HTree(map.OrderBy(pair => pair.Value).ThenByDescending(pair => pair.Key));
            var table       = tree.Table;
            var path        = Path.GetFullPath(_path);
            var destination = Path.GetDirectoryName(path) + "/" + Path.GetFileNameWithoutExtension(Path.GetFileNameWithoutExtension(path)) +
                              "_decoded" + Path.GetExtension(Path.GetFileNameWithoutExtension(path));

            int size = (int)(stream.Length - stream.Position);

            var outputStream = new BinaryWriter(new FileStream(destination, FileMode.Create, FileAccess.Write));

            StringBuilder builder = new StringBuilder();

            for (long remaining = stream.Length - stream.Position; remaining > 0; remaining -= size)
            {
                size = (int)Math.Min(size, remaining);
                var buffer = new byte[size];
                stream.Read(buffer, 0, size);
                HNode node;
                for (int i = 0; i < size; i++)   // idx in buffer in block
                {
                    var bits = new BitSet(buffer[i]);
                    for (int j = 0; j < 8;)   // idx in byte
                    {
                        for (node = tree.Root; !node.IsLeaf;)
                        {
                            if (j >= 8 && !node.IsLeaf)
                            {
                                j = 0;
                                if (i + 1 >= size)
                                {
                                    break;
                                }

                                bits = new BitSet(buffer[++i]);
                            }

                            if (bits[j] == 0)
                            {
                                node = node.Left;
                            }
                            else if (bits[j] == 1)
                            {
                                node = node.Right;
                            }
                            j++;
                        }

                        outputStream.Write(node.Data);
                        map[node.Data]--;
                        if (map[node.Data] == 0)
                        {
                            map.Remove(node.Data);
                        }
                        if (map.Count == 0)
                        {
                            outputStream.Close();
                            return;
                        }
                    }
                }
            }

            outputStream.Close();
        }