예제 #1
0
        public static void agregarNuevaCompresion(MisCompresiones nuevo)
        {
            string path = nuevo.RutaO;

            using (StreamWriter sw = File.AppendText(path))
            {
                string text =
                    $"Nombre Original: {nuevo.nombreOriginal}, Razon de Compresion: {nuevo.razonDeCompresion}, Factor de Compresion: {nuevo.factorDeCompresion}, Porcentaje de Compresion: {nuevo.porcentajeDeCompresion}";
                sw.WriteLine(text);
            }
        }
예제 #2
0
        public static void Comprimir(string Rpath, string WPath, string WPath2)
        {
            #region Variables

            int maxDictionaryLenght = 256;
            int byteLenght          = 8;

            #endregion

            #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(Rpath, 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(WPath, 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(WPath, new[] { Convert.ToByte(Convert.ToInt32(Byte, 2)) });
                }
            }

            #endregion

            #region FileInfo

            FileInfo originalFile   = new FileInfo(Rpath);
            FileInfo compressedFile = new FileInfo(WPath);
            MisCompresiones.agregarNuevaCompresion(new MisCompresiones(Path.GetFileName(Rpath), originalFile.Length, compressedFile.Length, WPath2)); //Anadir a mis compresiones

            #endregion
        }