示例#1
0
        public Form1()
        {
            InitializeComponent();
            lzw = new Lzw();

            for (int i = 9; i <= 15; i++)
            {
                indexMenu.Items.Add(i);
            }
            indexMenu.SelectedIndex = 0;

            methodType.Items.Add("Freeze");
            methodType.Items.Add("Empty");
            methodType.SelectedIndex = 0;
        }
示例#2
0
 public void IsLzwCorrect(string name, string encodeName, string decodeName)
 {
     var(source, encoded, decoded) = InitFiles(name, encodeName, decodeName);
     try
     {
         Lzw.Compress(source.OpenRead(), encoded.OpenWrite());
         Assert.Greater(source.Length, encoded.Length);
         Lzw.Decompress(encoded.OpenRead(), decoded.OpenWrite());
         Assert.That(source.Length, Is.EqualTo(decoded.Length));
         Assert.That(CompareFiles(source.OpenRead(), decoded.OpenRead()), Is.True);
     }
     finally
     {
         encoded.Delete();
         decoded.Delete();
     }
 }
示例#3
0
        public IActionResult Upload()
        {
            try
            {
                var    file       = Request.Form.Files[0];
                string file_nameN = Request.Form["name"];
                string metodo     = Request.Form["metodo"]; //Recibe el metodo por el cual se va a comprimir
                var    folderName = Path.Combine("Resources", "Files");
                var    pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
                string extension  = Path.GetExtension(file.FileName);


                if (extension == ".txt")
                {
                    if (file.Length > 0)
                    {
                        //SUBIR ARCHIVO
                        var fileName = System.Net.Http.Headers.ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        var fullPath = Path.Combine(pathToSave, fileName);
                        var dbPath   = Path.Combine(folderName, fileName);
                        var error    = "Escriba metodo para comprimir huffman o lzw";

                        //LECTURA ARCHIVO SUBIDO
                        if (metodo == "huffman")
                        {
                            Huffman arch = new Huffman();
                            using (var stream = new FileStream(fullPath, FileMode.Create))
                            {
                                arch.Name = file.FileName;
                                file.CopyTo(stream);
                            }
                            arch.Size = file.Length;
                            using (var stream = new FileStream(fullPath, FileMode.Open))
                            {
                                using (BinaryReader br = new BinaryReader(stream))
                                {
                                    var buffer = new byte[Data.BufferLength];
                                    while (br.BaseStream.Position != br.BaseStream.Length)
                                    {
                                        buffer = br.ReadBytes(Data.BufferLength); //llenar el buffer
                                        arch.Dictionary_Original(buffer);         //enviar buffer al diccionario
                                    }
                                }

                                stream.Close();
                            }
                            arch.Start_Dictionary(); // diccionario con probabilidades
                            arch.Add_Pr();           //sumar probabilidades



                            arch.OrdenarDic(); //ordena el diccionario y lo agrega a una lista
                            arch.CrearArbol(); //Creacion de arbol

                            //CREACION ARCHIVO .HUFF
                            var fileNameH = file_nameN + ".huff";
                            var fullPathH = Path.Combine(pathToSave, fileNameH);
                            var dbPathH   = Path.Combine(folderName, fileNameH);


                            using (var stream = new FileStream(fullPathH, FileMode.Create))
                            {
                                //Escribimos en el archivo
                            }

                            arch.Write_Buffer = new byte[0];
                            using (var stream = new FileStream(fullPath, FileMode.Open))
                            {
                                using (BinaryReader br = new BinaryReader(stream))
                                {
                                    var buffer = new byte[Data.BufferLength];
                                    while (br.BaseStream.Position != br.BaseStream.Length)
                                    {
                                        buffer = br.ReadBytes(Data.BufferLength); //llenar el buffer
                                        arch.Traduction(buffer);
                                    }
                                    if (arch.pre_restantes.Length != 0)
                                    {
                                        string auxiliar = string.Empty;
                                        arch.BinaryToDecimal(auxiliar);
                                    }
                                }
                                arch.EscribirArch(fullPathH, pathToSave);
                                stream.Close();
                            }


                            var stream1 = new FileStream(fullPathH, FileMode.Open);

                            return(File(stream1, System.Net.Mime.MediaTypeNames.Application.Octet, fileNameH));
                        }
                        if (metodo == "lzw")
                        {
                            Lzw arch = new Lzw();
                            using (var stream = new FileStream(fullPath, FileMode.Create))
                            {
                                arch.Name = file.FileName;
                                file.CopyTo(stream);
                            }
                            arch.Size = file.Length;
                            using (var stream = new FileStream(fullPath, FileMode.Open))
                            {
                                using (BinaryReader br = new BinaryReader(stream))
                                {
                                    var buffer = new byte[Data.BufferLength];
                                    while (br.BaseStream.Position != br.BaseStream.Length)
                                    {
                                        buffer = br.ReadBytes(Data.BufferLength); //llenar el buffer
                                        arch.IniciarDicLZW(buffer);               //enviar buffer al diccionario
                                    }
                                }

                                stream.Close();
                            }
                            //CREACION ARCHIVO .lzw
                            var fileNameH = file_nameN + ".lzw";
                            var fullPathH = Path.Combine(pathToSave, fileNameH);
                            var dbPathH   = Path.Combine(folderName, fileNameH);


                            using (var stream = new FileStream(fullPathH, FileMode.Create))
                            {
                                //Escribimos en el archivo
                            }

                            arch.buffer_escr = new byte[0];
                            using (var stream = new FileStream(fullPath, FileMode.Open))
                            {
                                using (BinaryReader br = new BinaryReader(stream))
                                {
                                    var buffer = new byte[Data.BufferLength];
                                    while (br.BaseStream.Position != br.BaseStream.Length)
                                    {
                                        buffer = br.ReadBytes(Data.BufferLength); //llenar el buffer
                                        arch.TraducirLZW(buffer);                 //traducir en lzw
                                    }
                                }
                                arch.EscribirLZW(fullPathH, pathToSave); //escribo en mi archivo LZW
                                stream.Close();
                            }
                            var stream1 = new FileStream(fullPathH, FileMode.Open);

                            return(File(stream1, System.Net.Mime.MediaTypeNames.Application.Octet, fileNameH));
                        }

                        return(Ok(new { error }));
                    }
                    else
                    {
                        return(BadRequest());
                    }
                }

                return(BadRequest());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }
        public IActionResult Upload()
        {
            try
            {
                var    file       = Request.Form.Files[0];
                var    folderName = Path.Combine("Resources", "Files");
                var    pathToSave = Path.Combine(Directory.GetCurrentDirectory(), folderName);
                string extension  = Path.GetExtension(file.FileName);

                var mensajee  = "Archivo con extensión incorrecta";
                var fileNameH = "";
                var fullPathH = "";

                if (extension == ".huff")
                {
                    if (file.Length > 0)
                    {
                        Huffman arch        = new Huffman();
                        bool    lectura_n   = false; //Bandera de la lectura de la cantidad de bytes del nombre
                        bool    lectura_n1  = false; //Bandera de la lectura del nombre
                        bool    lectura_1   = false; //Bandera de la lectura de los primeros dos bytes;
                        bool    lectura_can = false; //bandera de la lectura del numero de codigos asci
                        bool    lectura_2   = false; // Bandera de lectura de los codigos ascci;
                        int     can_as      = 0;
                        //SUBIR ARCHIVO
                        var    fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        var    fullPath = Path.Combine(pathToSave, fileName);
                        var    dbPath   = Path.Combine(folderName, fileName);
                        string nom      = String.Empty;

                        using (var stream = new FileStream(fullPath, FileMode.Create))
                        {
                            file.CopyTo(stream);
                        }
                        int cantidadle = 0;
                        int cantidadn  = 0;
                        using (var stream = new FileStream(fullPath, FileMode.Open))
                        {
                            using (BinaryReader br = new BinaryReader(stream))
                            {
                                var buffer = new byte[Data.BufferLength];
                                while (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    if (lectura_n == false)
                                    {
                                        buffer    = br.ReadBytes(1);
                                        cantidadn = Convert.ToInt32(buffer[0]);
                                        lectura_n = true;
                                    }
                                    else if (lectura_n1 == false)
                                    {
                                        buffer = br.ReadBytes(cantidadn);

                                        for (int x = 0; x < cantidadn; x++)
                                        {
                                            nom = nom + Convert.ToChar(buffer[x]);
                                        }
                                        lectura_n1 = true;
                                    }
                                    else if (lectura_1 == false)
                                    {
                                        buffer         = br.ReadBytes(2);// Leo los dos primeros bytes
                                        cantidadle     = Convert.ToInt32(buffer[0]);
                                        arch.agregados = Convert.ToInt32(buffer[1]);

                                        lectura_1 = true;
                                    }
                                    else if (lectura_can == false)
                                    {
                                        buffer = br.ReadBytes(cantidadle);
                                        string aux = string.Empty;
                                        for (int x = 0; x < cantidadle; x++)
                                        {
                                            aux = aux + Convert.ToChar(buffer[x]);
                                        }
                                        can_as      = Convert.ToInt32(aux);
                                        lectura_can = true;
                                    }
                                    else if (lectura_2 == false)
                                    {
                                        buffer = br.ReadBytes(can_as); //Leo la cantidad de codigos ascci agregados
                                        arch.GuardarAscci(buffer);
                                        lectura_2 = true;
                                    }
                                    else
                                    {
                                        buffer = br.ReadBytes(Data.BufferLength);//llenar el buffer
                                        arch.IniciarDicODesc(buffer);
                                    }
                                }
                            }

                            stream.Close();
                        }
                        arch.Start_Dictionary(); // diccionario con probabilidades
                        arch.Add_Pr();           //sumar probabilidades


                        arch.OrdenarDic();

                        arch.CrearArbol();
                        //CREACION ARCHIVO .TXT
                        fileNameH = nom + ".txt";
                        fullPathH = Path.Combine(pathToSave, fileNameH);
                        var dbPathH = Path.Combine(folderName, fileNameH);

                        using (var stream = new FileStream(fullPathH, FileMode.Create))
                        {
                            //Escribimos en el archivo
                        }
                        arch.Binarios(fullPathH);
                    }

                    else
                    {
                        return(BadRequest());
                    }
                    var stream1 = new FileStream(fullPathH, FileMode.Open);

                    return(File(stream1, System.Net.Mime.MediaTypeNames.Application.Octet, fileNameH));
                }


                if (extension == ".lzw")
                {
                    if (file.Length > 0)
                    {
                        Lzw  arch        = new Lzw();
                        bool lectura_n   = false; //Bandera de la lectura de la cantidad de bytes del nombre
                        bool lectura_n1  = false; //Bandera de la lectura del nombre
                        bool lectura_1   = false; //Bandera de la lectura de los primeros dos bytes;
                        bool lectura_can = false; //bandera de la lectura del numero de codigos asci
                        bool lectura_2   = false; // Bandera de lectura de los codigos ascci;
                        int  can_as      = 0;
                        //SUBIR ARCHIVO
                        var    fileName   = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                        var    fullPath   = Path.Combine(pathToSave, fileName);
                        var    dbPath     = Path.Combine(folderName, fileName);
                        string nom        = String.Empty;
                        int    cantidadle = 0;

                        using (var stream = new FileStream(fullPath, FileMode.Create))
                        {
                            file.CopyTo(stream);
                        }

                        using (var stream = new FileStream(fullPath, FileMode.Open))
                        {
                            using (BinaryReader br = new BinaryReader(stream))
                            {
                                var buffer = new byte[Data.BufferLength];
                                while (br.BaseStream.Position != br.BaseStream.Length)
                                {
                                    if (lectura_n == false)
                                    {
                                        buffer     = br.ReadBytes(1);
                                        cantidadle = Convert.ToInt32(buffer[0]);
                                        lectura_n  = true;
                                    }
                                    else if (lectura_n1 == false)
                                    {
                                        buffer = br.ReadBytes(cantidadle);
                                        nom    = string.Empty;
                                        for (int x = 0; x < cantidadle; x++)
                                        {
                                            nom = nom + Convert.ToChar(buffer[x]);
                                        }
                                        lectura_n1 = true;
                                    }
                                    else if (lectura_1 == false)
                                    {
                                        buffer     = br.ReadBytes(1);// Leo los dos primeros bytes
                                        cantidadle = Convert.ToInt32(buffer[0]);
                                        lectura_1  = true;
                                    }
                                    else if (lectura_can == false)
                                    {
                                        buffer = br.ReadBytes(cantidadle);
                                        string aux = string.Empty;
                                        for (int x = 0; x < cantidadle; x++)
                                        {
                                            aux = aux + Convert.ToChar(buffer[x]);
                                        }
                                        can_as      = Convert.ToInt32(aux);
                                        lectura_can = true;
                                    }
                                    else if (lectura_2 == false)
                                    {
                                        buffer = br.ReadBytes(can_as); //Leo la cantidad de codigos ascci agregados
                                        arch.GuardarAscci(buffer);
                                        lectura_2 = true;
                                    }
                                    else
                                    {
                                        buffer = br.ReadBytes(Data.BufferLength);//llenar el buffer
                                        arch.IniciarDicODescLZW(buffer);
                                    }
                                }
                            }

                            stream.Close();
                        }
                        fileNameH = nom + ".txt";
                        fullPathH = Path.Combine(pathToSave, fileNameH);
                        var dbPathH = Path.Combine(folderName, fileNameH);

                        using (var stream = new FileStream(fullPathH, FileMode.Create))
                        {
                            //Escribimos en el archivo
                        }
                        arch.EscribirTXTLZW(fullPathH);
                    }
                    else
                    {
                        return(BadRequest());
                    }
                    var stream1 = new FileStream(fullPathH, FileMode.Open);

                    return(File(stream1, System.Net.Mime.MediaTypeNames.Application.Octet, fileNameH));
                }
                return(Ok(new { mensajee }));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error"));
            }
        }