Пример #1
0
        static void Main(string[] args)
        {
            Huffman Procesar = new Huffman();

            string cadena = "ddabdccedchafbadgdcgabgccddbcdgg";

            string cadena_cifrada    = Procesar.Cifrado(cadena);
            string cadena_descifrada = Procesar.Descifrado(cadena_cifrada);

            Console.ReadLine();
        }
Пример #2
0
        public async Task <IActionResult> CompressFile([FromForm] IFormFile file, string name)
        {
            try
            {
                // Escribir archivo subido hacia el servidor para trabajar con ese
                var path = _env.ContentRootPath;
                path = Path.Combine(path, "Files");

                string pathInfo = Path.Combine(path, "Info");


                if (System.IO.File.Exists($"{pathInfo}/archivoCargado.txt"))
                {
                    System.IO.File.Delete($"{pathInfo}/archivoCargado.txt");
                }

                using var saver = new FileStream($"{pathInfo}/archivoCargado.txt", FileMode.OpenOrCreate);
                await file.CopyToAsync(saver);

                saver.Close();


                // Leer el archivo en el servidor para trabajar sobre él
                using var fileWritten = new FileStream($"{pathInfo}/archivoCargado.txt", FileMode.OpenOrCreate);
                using var reader      = new BinaryReader(fileWritten);
                var buffer = new byte[2000000];
                while (fileWritten.Position < fileWritten.Length)
                {
                    buffer = reader.ReadBytes(2000000);
                }
                reader.Close();
                fileWritten.Close();

                var nodeString = ByteGenerator.ConvertToString(buffer);

                Huffman huff           = new Huffman();
                string  cadena_cifrada = huff.Cifrado(nodeString);


                // Escribir el archivo {name}.huff en el servidor

                using (var fs = new FileStream($"{path}/{name}.huff", FileMode.OpenOrCreate))
                {
                    fs.Write(ByteGenerator.ConvertToBytes(cadena_cifrada), 0, cadena_cifrada.Length);
                }


                // Obtener la info de compresión
                CompressionInfo cInfo = new CompressionInfo();
                cInfo.originalName        = file.FileName;
                cInfo.compressedFilePath  = Path.Combine(path, $"{name}.huff");
                cInfo.compressionRatio    = Math.Round(Convert.ToDouble(cadena_cifrada.Length) / Convert.ToDouble(nodeString.Length), 2);
                cInfo.compressionFactor   = Math.Round(1 / cInfo.compressionRatio, 2);
                cInfo.reductionPercentage = Math.Round((1 - cInfo.compressionRatio) * 100, 2);


                // Escribir en el archivo Historial.txt
                string       pathHistorial   = Path.Combine(pathInfo, "Historial.txt");
                string       cadenaHistorial = System.IO.File.ReadAllText(pathHistorial);
                StreamWriter newfile         = new StreamWriter(pathHistorial);

                if (cadenaHistorial.Length != 0)
                {
                    newfile.Write(cadenaHistorial);
                    newfile.Write("/");
                }
                newfile.Write(cInfo.originalName + ";" + cInfo.compressedFilePath + ";" + (cInfo.compressionRatio).ToString() + ";" + (cInfo.compressionFactor).ToString() + ";" + (cInfo.reductionPercentage).ToString());

                newfile.Close();


                //Archivo a mandar de regreso
                //var fileStream = new FileStream($"{path}/{name}.huff", FileMode.OpenOrCreate);
                string mensaje = "Archivo comprimido exitosamente";
                return(Ok(new { mensaje }));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }