private string topFive(string codeCheck) { var charList = new List <charCount>(); char[] distinct = codeCheck.Distinct().ToArray(); // gets count for each character in the string foreach (char checkChar in distinct) { charCount charCheck = new charCount(); int count = codeCheck.Count(f => f == checkChar); charCheck.count = count; charCheck.listChar = checkChar; charList.Add(charCheck); } // first order alphabetically, then order by letter count, and take only the top 5 var query = charList.OrderBy(charCount => charCount.listChar); var top5 = query.OrderByDescending(charCount => charCount.count).Take(5); // Turn the top 5 into a string that can be compared to the checksum string topChar = ""; int i = 0; foreach (charCount topPick in top5) { topChar += topPick.listChar; i++; } return(topChar); }
public ActionResult GeneraciónDelArchivoOriginal(string rutaDirectorioUsuario, string fileName) { var fileNameNoExtension = fileName.Substring(0, fileName.IndexOf(".")); var binario = string.Empty; var texto = string.Empty; charCount valor = new charCount(); foreach (byte bit in usedChars) { binario = string.Empty; binario = binario + Convertir(bit, binario); foreach (char car in binario) { valor.codPref = valor.codPref + car; foreach (char Key in diccionario.Keys) { charCount valor2 = GetAnyValue <charCount>(Convert.ToByte(Key)); if (valor.codPref == valor2.codPref) { texto = texto + Key; valor.codPref = string.Empty; } } } } using (var writeStream = new FileStream(rutaDirectorioUsuario + "\\..\\Files\\" + fileName, FileMode.Create)) { using (var writer = new BinaryWriter(writeStream)) { int cantidadvecesbuffer = 0; byte[] byteBufferfinal = new byte[100]; int cantidad = 0; foreach (char carfinal in texto) { byteBufferfinal[cantidad] = Convert.ToByte(carfinal); cantidad++; if (cantidad == 100) { if (cantidadvecesbuffer == 0) { writer.Write(byteBufferfinal); byteBufferfinal = new byte[100]; cantidadvecesbuffer++; cantidad = 0; } else { writer.Seek(0, SeekOrigin.End); writer.Write(byteBufferfinal); byteBufferfinal = new byte[100]; cantidad = 0; } } } if ((byteBufferfinal[0] != 0) && (byteBufferfinal[1] != 0)) { int contador0 = 0; List <byte> ListAux = new List <byte>(); foreach (byte bit in byteBufferfinal) { if (bit == 0) { contador0++; } else { contador0 = 0; } if (contador0 != 3) { ListAux.Add(bit); } else { for (int i = 0; i < 2; i++) { ListAux.Remove(ListAux.Last()); } break; } } byteBufferfinal = new byte[ListAux.Count()]; int j = 0; foreach (byte bite in ListAux) { byteBufferfinal[j] = bite; j++; } writer.Seek(0, SeekOrigin.End); writer.Write(byteBufferfinal); } } } return(RedirectToAction("Download", new { rutaDirectorioUsuario, fileName })); }
//Decifrar public ActionResult LecturaDescompresion(string rutaDirectorioUsuario, string fileName) { usedChars = new List <byte>(); diccionario = new Dictionary <char, charCount>(); var fileNameNoExtension = fileName.Substring(0, fileName.IndexOf(".")); using (var stream = new FileStream(rutaDirectorioUsuario + "\\..\\Files\\" + fileNameNoExtension + ".huff", FileMode.Open)) { using (var reader = new BinaryReader(stream)) { string prefijos = string.Empty; char caracter = ' '; byte[] byteBuffer = new byte[bufferLengt]; bool encontrado = false; bool separador = false; while (reader.BaseStream.Position != reader.BaseStream.Length) { byteBuffer = reader.ReadBytes(bufferLengt); for (int i = 0; i < byteBuffer.Count(); i++) { if (separador != true) { if ((byteBuffer[i] == 45)) { if ((byteBuffer[i + 1] == 45)) { separador = true; i = i + 2; } } if (encontrado == false) { if (byteBuffer[i] == 124) { caracter = (char)byteBuffer[i - 1]; encontrado = true; } } else { if ((byteBuffer[i + 1] != 124) && (byteBuffer[i] != 2)) { prefijos += (char)byteBuffer[i]; } else { charCount prefijo = new charCount(); prefijo.codPref = prefijos; if (prefijo.codPref[0] == '|') { string prueba = string.Empty; for (int j = 1; j < prefijo.codPref.Count(); j++) { prueba = prueba + prefijo.codPref[j]; } prefijo.codPref = prueba; } diccionario.Add(caracter, prefijo); encontrado = false; prefijos = ""; } } } else { usedChars.Add(byteBuffer[i]); } } } } } for (int i = 0; i < 2; i++) { usedChars.Remove(usedChars[0]); } return(RedirectToAction("GeneraciónDelArchivoOriginal", new { rutaDirectorioUsuario, fileName })); }
public ActionResult Arbol(string rutaDirectorioUsuario, string fileName) { //creación del árbol Huffman HuffmanProcess = new Huffman(); Tree Arbol = new Tree(); Arbol.root = HuffmanProcess.TreeCreation(lista); string prefíjo = string.Empty; diccionario = Arbol.prefixCodes(Arbol.root, diccionario, prefíjo); //Escritura del compresor códigos prefíjos convertidos a bytes using (var writeStream = new FileStream(rutaDirectorioUsuario + "\\..\\Files\\" + fileName + ".huff", FileMode.Open)) { using (var writer = new BinaryWriter(writeStream)) { byte[] bytebuffer = new byte[500]; List <char> cadena = new List <char>(); int cantidadbuffer = 0; foreach (byte bit in ListaByte) { charCount separación = new charCount(); separación = GetAnyValue <int>(bit); foreach (char caracter in separación.codPref) { cadena.Add(caracter); } } string binario = ""; foreach (char car in cadena) { if (binario.Count() == 8) { byte DECABYTE = new byte(); var pref = binario; decimal x = Convert.ToInt32(pref, 2); DECABYTE = Convert.ToByte(x); bytebuffer[cantidadbuffer] = DECABYTE; cantidadbuffer++; binario = string.Empty; binario = binario + car; } else { binario = binario + car; } if (cantidadbuffer == 500) { writer.Seek(0, SeekOrigin.End); writer.Write(bytebuffer); cantidadbuffer = 0; bytebuffer = new byte[500]; } } if (binario != string.Empty) { while (binario.Count() != 8) { binario = binario + "0"; } byte DECABYTE = new byte(); var pref = binario; decimal x = Convert.ToInt32(pref, 2); DECABYTE = Convert.ToByte(x); bytebuffer[cantidadbuffer] = DECABYTE; int contador0 = 0; List <byte> ListAux = new List <byte>(); foreach (byte bit in bytebuffer) { if (bit == 0) { contador0++; } else { contador0 = 0; } if (contador0 != 10) { ListAux.Add(bit); } else { for (int i = 0; i < 9; i++) { ListAux.Remove(ListAux.Last()); } break; } } bytebuffer = new byte[ListAux.Count()]; int j = 0; foreach (byte bit in ListAux) { bytebuffer[j] = bit; j++; } writer.Seek(0, SeekOrigin.End); writer.Write(bytebuffer); } } } archivoComprimido = 1; return(RedirectToAction("Index")); }