public static string[] DecompressFile(string filePath) { LZW Compresor_lzw = new LZW(); FileStream file = new FileStream(filePath, FileMode.OpenOrCreate); BinaryReader Lector = new BinaryReader(file); int Cant_Byte_Read = 10000; int Aumentar_Max = 1; byte[] Text = new byte[Cant_Byte_Read]; Text = Lector.ReadBytes(Cant_Byte_Read); while (file.Position < file.Length) { byte[] Aux = Lector.ReadBytes(Cant_Byte_Read); Array.Resize(ref Text, Text.Length + Aux.Length); Aux.CopyTo(Text, Cant_Byte_Read * Aumentar_Max); Aumentar_Max++; } Lector.Close(); byte[] TextoComprimido = Text; int cant_CName = TextoComprimido[0]; string Name_Original = ""; for (int i = 1; i <= cant_CName; i++) { Name_Original += Convert.ToChar(TextoComprimido[i]); } byte[] Data_retorna = new byte[TextoComprimido.Length - (cant_CName + 1)]; Array.Copy(TextoComprimido, (cant_CName + 1), Data_retorna, 0, Data_retorna.Length); byte[] Impresor = Compresor_lzw.Descompresion(Data_retorna); string FinalFileName = Directory.GetCurrentDirectory() + "\\Decompressed\\" + Name_Original; var extension = FinalFileName.Split('.'); string ext = extension[extension.Length - 1]; string name = Name_Original.Substring(0, Name_Original.Length - ext.Length - 1); int count = 0; while (File.Exists(FinalFileName)) { count++; FinalFileName = Directory.GetCurrentDirectory() + "\\Decompressed\\" + name + count + "." + ext; } FileStream archivo = new FileStream(FinalFileName, FileMode.OpenOrCreate); BinaryWriter Escritor = new BinaryWriter(archivo); Escritor.Write(Impresor); Escritor.Close(); File.Delete(filePath); string[] names = { Name_Original, FinalFileName }; return(names); }
public void Descomprimir() { string descomprimido = ""; string directorio; directorio = root + @"\\Upload\\"; if (!Directory.Exists(directorio + "\\Compresion\\")) { DirectoryInfo di = Directory.CreateDirectory(directorio + "\\Compresion\\"); } string comprimido = System.IO.File.ReadAllText(@path); const int bufferLength = 100; List <int> bytedecompress = new List <int>(); var buffer = new char[bufferLength]; using (var file = new FileStream(path, FileMode.Open)) { using (var reader = new BinaryReader(file)) { while (reader.BaseStream.Position != reader.BaseStream.Length) { buffer = reader.ReadChars(bufferLength); foreach (var item in buffer) { bytedecompress.Add((int)Convert.ToChar(item)); } } } } descomprimido = LZW.Descompresion(bytedecompress); string rootRazonFactor = root; NombreTemporal = NombreTemporal.Replace(".lzw", ""); root = root + @"\\Upload\\Compresion\\" + NombreTemporal + ".txt"; File.WriteAllText(@root, descomprimido); FactorCompresion = Convert.ToDouble(comprimido.Length) / Convert.ToDouble(descomprimido.Length); RazonCompresion = Convert.ToDouble(descomprimido.Length) / Convert.ToDouble(comprimido.Length); PorcentajeCompresion = ((FactorCompresion / RazonCompresion) * 100); double raz = Math.Round(RazonCompresion, 2); double fac = Math.Round(FactorCompresion, 2); double porcent = Math.Round(PorcentajeCompresion, 2); rootRazonFactor = rootRazonFactor + @"\\Upload\\Compresion\\FactorRazon.txt"; File.WriteAllText(@rootRazonFactor, "Razon de compresión: " + raz.ToString() + Environment.NewLine + "Factor de compresión: " + fac.ToString() + Environment.NewLine + "Porcentaje de compresión: " + porcent.ToString() + "%"); }
static void Main(string[] args) { LZW CompresorLZW = new LZW(); Header(); TitleOption1(); bool exit = false; while (!exit) { try { Header(); TitleOption1(); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Ingrese el texto a comprimir:\n\n"); Console.ResetColor(); string Text = Console.ReadLine(); if (String.IsNullOrEmpty(Text)) { throw new FormatException(); } byte[] texto = new byte[Text.Length]; for (int i = 0; i < Text.Length; i++) { texto[i] = Convert.ToByte(Convert.ToChar(Text[i])); } byte[] Comprimido = CompresorLZW.Compresion(texto); string result = ""; foreach (byte bit in Comprimido) { result += Convert.ToString(Convert.ToChar(bit)); } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\n\nEl texto comprimido es el siguiente:\n\n"); Console.ResetColor(); Console.WriteLine(result); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\n\nPresione cualquier tecla para ver el mismo texto descompreso."); Console.ResetColor(); Console.ReadKey(); Console.Clear(); Header(); TitleOption2(); result = ""; byte[] Descomprimido = CompresorLZW.Descompresion(Comprimido); foreach (byte bit in Descomprimido) { result += Convert.ToString(Convert.ToChar(bit)); } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("\n\nEl texto descomprimido es el siguiente:"); Console.ResetColor(); Console.WriteLine("\n" + result + "\n\n"); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Proceso finalizado. Presione una tecla."); Console.ResetColor(); Console.ReadKey(); Header(); Console.ForegroundColor = ConsoleColor.Magenta; string e = "Ingrese S para volver a comprimir o cualquier cosa para salir del programa."; Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + (e.Length / 2)) + "}", e) + "\n"); Console.ResetColor(); if (!Convert.ToString(Console.ReadLine()).Equals("S")) { exit = true; } } catch { Header(); string e = "Ocurrió un error. Presione una tecla para volver a comprimir."; Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + (e.Length / 2)) + "}", e) + "\n"); Console.ReadKey(); } } }
public ActionResult Descompress([FromBody] string path) { lZW.Descompresion(path); return(Ok()); }