Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        ///
        /// <param name="fileStreamIn"></param>
        /// <param name="fileStreamOut"></param>
        /// <param name="prefixTree"></param>
        public static void Decompress(Stream fileStreamIn, Stream fileStreamOut, PrefixTree prefixTree)
        {
            BitStream bitStream = new BitStream(fileStreamIn);

            prefixTree.InitializeTrace();

            int bit;

            while ((bit = bitStream.ReadBit()) != -1)
            {
                byte symbol = prefixTree.Trace(bit);
                if (symbol != 0)
                {
                    fileStreamOut.WriteByte(symbol);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        ///
        /// <param name="fileStreamIn"></param>
        /// <param name="fileStreamOut"></param>
        /// <param name="prefixTree"></param>
        public static void Compress(Stream fileStreamIn, Stream fileStreamOut, PrefixTree prefixTree)
        {
            // Get the symbol codes from the prefix tree.
            string[] symbolCodes = prefixTree.GetSymbolCodes();

            BitStream bitStream = new BitStream(fileStreamOut);

            int symbol;

            while ((symbol = fileStreamIn.ReadByte()) != -1)
            {
                string code = symbolCodes[symbol];
                foreach (char character in code)
                {
                    int bit = (character == '1') ? 1 : 0;
                    bitStream.WriteBit(bit);
                }
            }

            bitStream.Finish();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// The application's entry point.
        /// </summary>
        ///
        /// <param name="args">The commnad line arguments.</param>
        static void Main(string[] args)
        {
            FileStream fileStreamIn  = null;
            FileStream fileStreamOut = null;

            try
            {
                // ===== Build the prefix tree ======

                // Open the input (uncompressed) file.
                fileStreamIn = new FileStream(args[0], FileMode.Open);

                // Count the character weights in the stream.
                long[] symbolWeights = Huffman.CountSymbolWeights(fileStreamIn);

                Console.WriteLine(ArrayToString(symbolWeights));

                // Build the prefix tree from the character weights.
                PrefixTree prefixTree = new PrefixTree(symbolWeights);

                // Close the input (uncompressed) file.
                fileStreamIn.Close();

                // ====== Compress file ======

                // Open the input (uncompressed) file.
                fileStreamIn = new FileStream(args[0], FileMode.Open);

                // Open the output (compressed) file.
                fileStreamOut = new FileStream(args[1], FileMode.Create);

                Huffman.Compress(fileStreamIn, fileStreamOut, prefixTree);

                // Close the input (uncompressed) file.
                fileStreamIn.Close();

                // Close the output (compressed) file.
                fileStreamOut.Close();

                // ===== Decompress the file =====

                // Open the input (compressed) file
                fileStreamIn = new FileStream(args[1], FileMode.Open);

                // Open the output (Uncompressed) file.
                fileStreamOut = new FileStream(args[2], FileMode.Create);

                Huffman.Decompress(fileStreamIn, fileStreamOut, prefixTree);
            }
            catch
            {
            }
            finally
            {
                // Close the input file.
                if (fileStreamIn != null)
                {
                    fileStreamIn.Close();
                }

                // Close the output file.
                if (fileStreamOut != null)
                {
                    fileStreamOut.Close();
                }
            }
        }