Question

I have finished my Huffman compression/decompression algorithm. I use an string to check my input, like "foo bar" 7x8 = 56 gives me 10010110111011100 = 17 +- 35% from original size compressed back.

But now I want to save it as a file, can anyone explain me an approach how to do this.

If needed I can post the source of my application.

My form is just a CODE (Also have cNode class for walking the tree)

    class cZU
{

    private List<cNode> cNodes = new List<cNode>();
    public cNode Root { get; set; }
    public Dictionary<char, int> Frequencies = new Dictionary<char, int>();

    public void mWalktree(string source)
    {

        for (int i = 0; i < source.Length; i++)
        {

            if (!Frequencies.ContainsKey(source[i]))
            {
                Frequencies.Add(source[i], 0);
            }

            Frequencies[source[i]]++;
        }

        foreach (KeyValuePair<char, int> symbol in Frequencies)
        {
            cNodes.Add(new cNode() { Symbol = symbol.Key, Frequency = symbol.Value });
        }

        while (cNodes.Count > 1)
        {
            List<cNode> orderedcNodes = cNodes.OrderBy(cNode => cNode.Frequency).ToList<cNode>();

            if (orderedcNodes.Count >= 2)
            {
                // Take first two items
                List<cNode> taken = orderedcNodes.Take(2).ToList<cNode>();

                // Create a parent cNode by combining the frequencies
                cNode parent = new cNode()
                {

                    Symbol = '*',
                    Frequency = taken[0].Frequency + taken[1].Frequency,
                    Left = taken[0],
                    Right = taken[1]
                };

                cNodes.Remove(taken[0]);
                cNodes.Remove(taken[1]);
                cNodes.Add(parent);
            }

            this.Root = cNodes.FirstOrDefault();
        }
    }

    public BitArray Encode(string source)
    {
        List<bool> encodedSource = new List<bool>();

        for (int i = 0; i < source.Length; i++)
        {
            List<bool> encodedSymbol = this.Root.Traverse(source[i], new List<bool>());
            encodedSource.AddRange(encodedSymbol);
        }

        BitArray bits = new BitArray(encodedSource.ToArray());

        return bits;
    }

Now i am just doing it like:

string = "foo bar";
ZU.mWalktree(inputstring);

And just output the encoded string to the user, but i need to save the encoded file to .txt and my question is what do i need to save in that .txt file in order to decode the file later.

Hope this clears it out.

Était-ce utile?

La solution

You may need a Base64 encoder (losing compression) to make it preview-able in text editor, to save it then you need only to output it to a file

System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", str_your_base64_encoded_bit_array);

or directly (as it is a compressed file):

BitArray bit_array = . . . .
byte [] bytes = new byte[bit_array.Length / 8 + ( bit_array.Length % 8 == 0 ? 0 : 1 )];
bit_array.CopyTo( bytes, 0 );
File.WriteAllBytes( @"C:\MyFile.bin", bytes );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top