Exemplo n.º 1
0
        public IEnumerable <MisCompresiones> DescargarCompresiones()
        {
            var                    memory     = new MemoryStream();
            MisCompresiones        xd         = new MisCompresiones();
            List <MisCompresiones> ListaChida = new List <MisCompresiones>();

            string[] split;
            try
            {
                using (var stream = new StreamReader(_environment.ContentRootPath + "\\ArchivosCompresos\\" + "Compresiones.txt"))
                {
                    while (!stream.EndOfStream)
                    {
                        split                     = stream.ReadLine().Split(",");
                        xd.nombreOriginal         = split[0];
                        xd.razonDeCompresion      = Convert.ToDouble(split[1]);
                        xd.factorDeCompresion     = Convert.ToDouble(split[2]);
                        xd.porcentajeDeCompresion = Convert.ToDouble(split[3]);
                        xd.RutaO                  = (split[4]);

                        ListaChida.Add(xd);
                    }
                }

                return(ListaChida);
            }
            catch (Exception)
            {
                return(ListaChida);
            }
        }
Exemplo n.º 2
0
        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
        }
Exemplo n.º 3
0
        public static void comprimir(string path)
        {
            #region Variables

            int maxDictionaryLenght = 256;
            int byteLenght          = 8;

            #endregion

            #region Crear_Archivo

            string nombreNuevoArchivo = Path.GetFileNameWithoutExtension(path) + ".lzw";
            string rutaComprimido     = Path.Combine(HomeController.directorioLZW, nombreNuevoArchivo);
            Archivo.crearArchivo(rutaComprimido);

            #endregion

            //Crear dicionario
            #region Caracteres

            var diccionario = obtenerDiccionarioCompresion();
            maxDictionaryLenght = diccionario.Count;

            #endregion

            //Analizar texto, creando diccionario y escribiendo en archivo
            #region Algoritmo

            int bufferLength = 1024;

            //Empezar concatenar
            string     c         = string.Empty;
            List <int> comprimir = new List <int>();

            string bits     = "";
            int    contador = 0;

            //Buffer para comprimir
            using (var file = new FileStream(path, FileMode.Open))
            {
                using (var reader = new BinaryReader(file))
                {
                    while (reader.BaseStream.Position != reader.BaseStream.Length)
                    {
                        var buffer = reader.ReadBytes(count: bufferLength);

                        foreach (var t in buffer)
                        {
                            string ct = c + ((char)t);
                            if (diccionario.ContainsKey(ct))
                            {
                                c = ct;
                            }
                            else
                            {
                                //sacarlo
                                string ByteString = Convert.ToString(diccionario[c], 2).PadLeft(byteLenght, '0'); // produce cadena "00111111";
                                bits += ByteString;

                                while (bits.Length >= 8) //Escribir bytes en archivo
                                {
                                    string Byte = bits.Substring(0, 8);
                                    bits = bits.Remove(0, 8);

                                    ByteArrayToFile(rutaComprimido, new[] { Convert.ToByte(Convert.ToInt32(Byte, 2)) });
                                }

                                comprimir.Add(diccionario[c]);
                                //Aqui ya lo concatena y lo agrega
                                diccionario.Add(ct, diccionario.Count);
                                c = ((char)t).ToString();
                                //Verificar tamaño de los bits
                                if (diccionario.Count >= maxDictionaryLenght)
                                {
                                    byteLenght++;
                                    maxDictionaryLenght = (int)Math.Pow(2, byteLenght);
                                }
                            }
                        }
                    }
                }
            }

            //Ultima cadena del archivo
            if (!string.IsNullOrEmpty(c))
            {
                string ByteString = Convert.ToString(diccionario[c], 2).PadLeft(byteLenght, '0'); // produce cadena "00111111";
                bits += ByteString;

                comprimir.Add(diccionario[c]);
            }

            if (bits != "") //Bits restantes
            {
                while (bits.Length % 8 != 0)
                {
                    bits += "0";
                }
                while (bits.Length >= 8) //Escribir bytes en archivo
                {
                    string Byte = bits.Substring(0, 8);
                    bits = bits.Remove(0, 8);

                    ByteArrayToFile(rutaComprimido, new[] { Convert.ToByte(Convert.ToInt32(Byte, 2)) });
                }
            }

            #endregion

            #region FileInfo

            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

            #endregion
        }