public static void comprimir(string path) { #region Caracteres //Leer caracteres y contarlos Dictionary <char, int> dictionary = Lectura.obtenerDiccionarioFrecuencias(path); dictionary.Add(EOF, 1); //End of file #endregion #region Codigos_Prefijo //Crear arbol Huffman ArbolHuffman arbol = new ArbolHuffman(dictionary); Dictionary <char, string> diccionario = arbol.obtenerDiccionarioBinario(); //Leer archivo original y sustituir por codigos prefijo string textoBinario = Lectura.textoBinario(path, diccionario); textoBinario += diccionario[EOF]; //End of file #endregion #region Escritura //Escribir nuevo archivo string nombreNuevoArchivo = Path.GetFileNameWithoutExtension(path) + ".huff"; string rutaComprimido = Path.Combine(HomeController.directorioHuffman, nombreNuevoArchivo); Archivo.crearArchivo(rutaComprimido); //Construir texto huffman string Byte = ""; //Valor de 8 bits List <char> binario = textoBinario.ToArray().ToList(); //Arreglo de texto en binario bool completed = false; while (!completed) { if (binario.Count > 0) { //Construir byte Byte += binario[0]; binario.RemoveAt(0); //Remover valor del arreglo if (Byte.Length == 8) { ByteArrayToFile(rutaComprimido, new[] { Convert.ToByte(Convert.ToInt32(Byte, 2)) }); //Escribir en archivo Byte = ""; } } else { //Llenar el resto de espacio con 0's if (Byte != "") { while (Byte.Length != 8) { Byte += "0"; } ByteArrayToFile(rutaComprimido, new[] { Convert.ToByte(Convert.ToInt32(Byte, 2)) }); Byte = ""; } completed = true; } } //Guardar configuracion para descomprimir configuracionParaDescomprimir(path, diccionario); #endregion HomeController.currentFile = rutaComprimido; FileInfo originalFile = new FileInfo(path); FileInfo compressedFile = new FileInfo(rutaComprimido); MisCompresiones.agregarNuevaCompresion(new MisCompresiones(Path.GetFileName(path), originalFile.Length, compressedFile.Length)); //Anadir a mis compresiones }
public static void descomprimir(string path) { #region Crear_Archivo string nombreNuevoArchivo = Path.GetFileNameWithoutExtension(path) + ".txt"; string rutaArchivo = Path.Combine(HomeController.directorioLZW, nombreNuevoArchivo); Archivo.crearArchivo(rutaArchivo); #endregion int bufferLength = 1024; int byteLenght = 8; int maxDictionaryLenght = 256; string bits = ""; Dictionary <int, char> dictionary = new Dictionary <int, char>(); using (var file = new FileStream(path, FileMode.Open)) { using (var reader = new BinaryReader(file)) { //Definir diccionario Dictionary <int, string> diccionario = obtenerDiccionarioDescompresion(); //Operacion inicial int key = 0; string c = diccionario[(int)reader.ReadByte()]; string descomprimir = c; //Buffer para descomprimir while (reader.BaseStream.Position != reader.BaseStream.Length) { var buffer = reader.ReadBytes(count: bufferLength); foreach (var t in buffer) { if (diccionario.Count + 1 >= maxDictionaryLenght) { byteLenght++; maxDictionaryLenght = (int)Math.Pow(2, byteLenght); } string ByteString = Convert.ToString(t, 2).PadLeft(8, '0'); // produce cadena "00111111"; bits += ByteString; if (bits.Length >= byteLenght) { key = Convert.ToInt32(bits.Substring(0, byteLenght), 2); bits = bits.Remove(0, byteLenght); string entry = null; if (diccionario.ContainsKey(key)) { entry = diccionario[key]; } else if (key == diccionario.Count) { entry = c + c[0]; } descomprimir += entry; // Agregar nueva frase al diccionario if (entry != null) { diccionario.Add(diccionario.Count, c + entry[0]); } c = entry; } } Lectura.Escritura(descomprimir, rutaArchivo); descomprimir = ""; } } } HomeController.currentFile = rutaArchivo; //Descargar }