Exemple #1
0
        public static byte[] DecompressFile(HuffmanCode code, string path)
        {
            byte[]        compressedFileBytes = File.ReadAllBytes(path);
            List <byte>   originalFileBytes   = new List <byte>();
            StringBuilder binaryCode          = new StringBuilder();
            int           length = 0;

            for (int i = 0; i < compressedFileBytes.Length; i++)
            {
                binaryCode.Append(Convert.ToString(compressedFileBytes[i], 2));
            }

            while (binaryCode.Length > length)
            {
                int index = code.BinaryCodes.ToList().IndexOf(binaryCode.ToString(0, length));
                if (index == -1)
                {
                    length++;
                }
                else
                {
                    originalFileBytes.Add((byte)index);
                    binaryCode.Remove(0, length);
                    length = 0;
                }
            }
            Debug.WriteLine($"Decompressed File Bytes\n{binaryCode}");
            return(originalFileBytes.ToArray());
        }
        private HuffmanCode DeserializeItem(string fileName)
        {
            StreamReader sw          = new StreamReader(fileName);
            string       swr         = sw.ReadToEnd();
            HuffmanCode  huffmanCode = JsonConvert.DeserializeObject <HuffmanCode>(swr);

            return(huffmanCode);
        }
 public bool ImportHuffmanCodes(string path)
 {
     try
     {
         HuffmanCode item = DeserializeItem(path);
         this.BinaryCodes     = item.BinaryCodes;
         this.BinaryCodesName = item.BinaryCodesName;
         return(true);
     }
     catch (Exception e)
     {
         Debug.WriteLine(e.Message);
         return(false);
     }
 }
Exemple #4
0
        public static byte[] CompressFile(HuffmanCode code, string path)
        {
            byte[]        fileBytes      = File.ReadAllBytes(path);
            StringBuilder compressedCode = new StringBuilder();

            for (int i = 0; i < fileBytes.Length; i++)
            {
                compressedCode.Append(code.BinaryCodes[(int)fileBytes[i]]);
            }
            Debug.WriteLine($"Compressed Code: {compressedCode}");
            int numOfBytes = compressedCode.Length / 8;

            byte[] bytes = new byte[numOfBytes];
            for (int i = 0; i < numOfBytes; i++)
            {
                bytes[i] = Convert.ToByte(compressedCode.ToString(0, 8), 2);
                compressedCode.Remove(0, 8);
            }

            return(bytes);
        }
 public MainWindow()
 {
     InitializeComponent();
     HuffmanCode = new HuffmanCode();
     ;
 }