예제 #1
0
        public static string RequestFileName()
        {
            string fileName;

            do
            {
                try
                {
                    Console.WriteLine("Enter the file name:");
                    fileName = Console.ReadLine();

                    FileController.ValidateFile(fileName, FileController.READING_EXTENSION);
                }
                catch (FileNotFoundException e)
                {
                    Console.WriteLine($"Error: {e.Message}");
                    continue;
                }

                break;
            } while (true);

            return(fileName);
        }
예제 #2
0
        public void Decode(byte[] bytes, string fileName)
        {
            string path = Path.Combine(FileController.FILE_PATH, fileName);

            DecodeECC(fileName, bytes);
            var newContent = FileController.ReadFileContent(fileName, FileController.COMPRESSING_EXTENSION);

            path = Path.ChangeExtension(path, FileController.DECOMPRESSING_EXTENSION);

            var K = bytes[1]; //get K from encoding

            using (var stream = File.Create(path))
            {
                var fileContent = new StringBuilder();
                bytes = newContent.Skip(2).ToArray();

                var bits  = new BitArray(bytes);
                var bools = new bool[bits.Length];
                bits.CopyTo(bools, 0);

                var builder = new StringBuilder();

                var qtyBits = (int)Math.Log(K, 2);
                int n = 0, i = 0;
                var countUnary     = true;
                var leftOverBinary = new StringBuilder();

                foreach (var b in bools)
                {
                    if (countUnary)
                    {
                        if (!b)
                        {
                            n++;
                        }
                        else
                        {
                            countUnary = false;
                        }

                        continue;
                    }

                    if (i <= qtyBits)
                    {
                        leftOverBinary.Append(b ? "1" : "0");
                        i++;

                        if (i == qtyBits)
                        {
                            var leftOver = Convert.ToInt32(leftOverBinary.ToString(), 2);
                            var qty      = n * K + leftOver;
                            fileContent.Append(((char)qty).ToString());

                            i              = 0;
                            n              = 0;
                            countUnary     = true;
                            leftOverBinary = new StringBuilder();
                        }
                    }
                }

                var contentBytes = Encoding.ASCII.GetBytes(fileContent.ToString());
                var readOnlySpan = new ReadOnlySpan <byte>(contentBytes);

                stream.Write(readOnlySpan);

                var teste = builder.ToString();
            }
        }
예제 #3
0
        private static void decompressFile(string fileName, IEncoder encoder)
        {
            var fileContent = FileController.ReadFileContent(fileName, FileController.ECC_EXTENSION);

            encoder.Decode(fileContent, fileName);
        }
예제 #4
0
        public void Decode(byte[] bytes, string fileName)
        {
            string path = Path.Combine(FileController.FILE_PATH, fileName);

            DecodeECC(path, bytes);

            var fileContent = FileController.ReadFileContent(fileName, FileController.COMPRESSING_EXTENSION);

            byte[] bytesAux = new byte[fileContent.Length - 3];       // remove heading
            Buffer.BlockCopy(fileContent, 3, bytesAux, 0, bytesAux.Length);

            path = Path.ChangeExtension(path, FileController.DECOMPRESSING_EXTENSION);

            BitArray      bits      = new BitArray(bytesAux);
            List <string> codewords = new List <string>();
            List <int>    intCodes  = new List <int>();
            List <char>   charCodes = new List <char>();

            int[] fib = new int[20];
            fib[0] = 0;
            fib[1] = 1;
            for (int i = 2; i < fib.Length; i++)
            {
                fib[i] = fib[i - 1] + fib[i - 2];
            }

            string codesAux = "";
            bool   a;
            bool   b;

            for (int i = 0; i < bits.Count; i++)
            {
                if (i < bits.Count - 1)
                {
                    a = bits[i];
                    b = bits[i + 1];
                    if (a == true && b == true)
                    {
                        codesAux += "1";                        // codeword with no stop bit
                        codewords.Add(codesAux);
                        codesAux = "";
                        ++i;
                    }
                    else
                    {
                        if (bits[i] == false)
                        {
                            codesAux += "0";
                        }
                        else
                        {
                            codesAux += "1";
                        }
                    }
                }
                else
                {
                    if (bits[i] == false)
                    {
                        codesAux += "0";
                    }
                    else
                    {
                        codesAux += "1";
                    }
                }
            }

            int  sum = 0;
            char numberAux;

            for (int i = 0; i < codewords.Count; i++)
            {
                for (int j = 0; j < codewords[i].Length; j++)
                {
                    numberAux = codewords[i][j];
                    if (numberAux == '1')
                    {
                        sum += fib[j + 2];
                    }
                }
                intCodes.Add(sum);
                sum = 0;
            }

            byte[] ret = new byte[intCodes.Count];
            for (int i = 0; i < intCodes.Count; i++)
            {
                ret[i] = Convert.ToByte(intCodes[i]);
            }

            File.WriteAllBytes(path, ret);
        }