예제 #1
0
        public void WriteFile(byte[] text, string newName, string name)
        {
            //escribir archivo binario
            string folder   = @"C:\Compressions\";
            string fullPath = folder + newName;

            // crear el directorio
            DirectoryInfo directory = Directory.CreateDirectory(folder);

            string content = "";

            foreach (var item in text)
            {
                content += huffCode[item];
            }

            byte[] compressed = ConvertToByte(content);
            using (FileStream writer = new FileStream(fullPath, FileMode.Open))
            {
                byte[] ToWrite = ConvertToByte(content);
                for (int i = 0; i < ToWrite.Length; i++)
                {
                    byte[] temp = { ToWrite[i] };
                    writer.Seek(0, SeekOrigin.End);
                    writer.Write(temp, 0, 1);
                }
            }

            double compressedBytes = compressed.Length;
            double originalBytes   = text.Length;
            double rc         = compressedBytes / originalBytes;
            double fc         = originalBytes / compressedBytes;
            double percentage = rc * 100;

            CompressionsCollection newElement = new CompressionsCollection(name, fullPath, rc, fc, percentage.ToString("N2") + "%");

            Data.Instance.archivos.Insert(0, newElement);
        }
예제 #2
0
        public void Compress(byte[] text, string newName, string name)
        {
            previous = "";
            for (int i = 0; i < allText.Length; i++)
            {
                actual = allText[i].ToString();
                string aux = previous + actual;
                if (alphabet.Values.Contains(aux)) // if wk is on the dictionary
                {
                    previous = previous + actual;
                }
                else
                {
                    List <string> cadenas = alphabet.Values.ToList();
                    //agregar codigo de w
                    for (int j = 0; j < alphabet.Count; j++)
                    {
                        if (cadenas[j] == previous)
                        {
                            output.Add(alphabet.Keys.ToList()[j]);
                            j = alphabet.Count; //parar de comparar.
                        }
                    }

                    //agregar wk al diccionario
                    cont++;
                    aux = previous + actual;
                    alphabet.Add(cont, aux);

                    //w = k
                    previous = actual;
                }
            }

            //imprimir codigo de w
            List <string> values = alphabet.Values.ToList();

            for (int j = 0; j < alphabet.Count; j++)
            {
                if (values[j] == previous)
                {
                    output.Add(alphabet.Keys.ToList()[j]);
                    j = alphabet.Count; //parar de comparar.
                }
            }

            //OBTENCION DE CODIGOS DE SALIDA TERMINADA, imprimir en el archivo

            string folder   = @"C:\Compressions\";
            string fullPath = folder + newName;

            // crear el directorio
            DirectoryInfo directory = Directory.CreateDirectory(folder);

            List <byte> allBytes = new List <byte>();

            for (int j = 0; j < output.Count; j++)
            {
                //byte[] ToWrite = Encoding.ASCII.GetBytes(output[j].ToString());
                string binary = Convert.ToString(output[j], 2);
                binary = binary.PadLeft(8, '0');
                byte[] sequence = binary.Select(c => Convert.ToByte(c.ToString())).ToArray();
                allBytes.AddRange(sequence);
            }

            string content = string.Join("", allBytes.ToArray());

            byte[] compressed = ConvertToByte(content);
            using (FileStream writer = new FileStream(fullPath, FileMode.OpenOrCreate))
            {
                byte[] ToWrite = ConvertToByte(content);
                for (int i = 0; i < ToWrite.Length; i++)
                {
                    byte[] temp = { ToWrite[i] };
                    writer.Seek(0, SeekOrigin.End);
                    writer.Write(temp, 0, 1);
                }
            }


            double compressedBytes = compressed.Length;
            double originalBytes   = text.Length;
            double rc         = compressedBytes / originalBytes;
            double fc         = originalBytes / compressedBytes;
            double percentage = rc * 100;

            CompressionsCollection newElement = new CompressionsCollection(name, fullPath, rc, fc, percentage.ToString("N2") + "%");

            Data.Instance.archivos.Insert(0, newElement);
        }