public void TestCompression() { string inStr = SerializationManager.Serialize(gateway.FindArray <Order>()); string outGZip = CompressionManager.Compress(inStr); string out7Zip = CompressionManager.Compress7Zip(inStr); Console.WriteLine("Input Size: " + inStr.Length.ToString()); Console.WriteLine("GZip Output Size: " + outGZip.Length.ToString()); Console.WriteLine("7Zip Output Size: " + out7Zip.Length.ToString()); Assert.AreEqual(inStr.Length, CompressionManager.Decompress7Zip(out7Zip).Length); Assert.AreEqual(inStr.Length, CompressionManager.Decompress(outGZip).Length); }
static void Main(string[] args) { //string msg = "This is a sentence to be compressed, converted to Base64 encoding,\r\nthen converted back to compressed form, then decompressed."; string msg = FoeServerCatalog.GetRssCache("CKXX"); //PrintTitle("Testing CompressionManager"); // Show original message //Console.WriteLine("Original message:"); //Console.WriteLine(msg); //Console.WriteLine(); // Show Base64 without compression string base64 = Convert.ToBase64String(Encoding.UTF8.GetBytes(msg)); //Console.WriteLine("Base64 without compression:"); //Console.WriteLine(base64); //Console.WriteLine(); // Show compressed base64 byte[] compressed = CompressionManager.Compress(Encoding.UTF8.GetBytes(msg)); base64 = Convert.ToBase64String(compressed); //Console.WriteLine("Compressed + Base64:"); //Console.WriteLine(base64); //Console.WriteLine(); // Show decompressed //compressed = Convert.FromBase64String(base64); compressed = Convert.FromBase64String("XQAAgAAQAAAAAAAAAAArlMVWcCtFDcKRGJnu/tFfaO6aVg=="); //compressed = Convert.FromBase64String(base64); byte[] decompressed = CompressionManager.Decompress(compressed); string originalMsg = Encoding.UTF8.GetString(decompressed); //Console.WriteLine("Decompressed:"); //Console.WriteLine(originalMsg); //Console.WriteLine(); //PrintFooter(); }
public static void generate(string path) { switch (Properties.Settings.Default.selectedLang) { case 0: conversion = conversionES; break; case 1: conversion = conversionRU; break; } List <string> filesTranslated = new List <string>(); filesTranslated.AddRange(Directory.GetFiles("./DSSE-files/translation/", "*.xml")); fileNumber = filesTranslated.Count(); List <string> filesTranslatedNames = new List <string>(); for (int i = 0; i < fileNumber; i++) { filesTranslatedNames.Add(Path.GetFileNameWithoutExtension(filesTranslated[i])); } //Console.WriteLine(filesTranslatedNames[0]); for (int i = 0; i < fileNumber; i++) { FileStream fs = new FileStream(path + filesTranslatedNames[i] + ".cmp", FileMode.Open); int hexIn; string hex = ""; StringBuilder hex2 = new StringBuilder(); for (int j = 0; (hexIn = fs.ReadByte()) != -1; j++) { hex2.Append(string.Format("{0:X2}", hexIn)); } hex = hex2.ToString(); fs.Dispose(); byte[] compressedFile = strToByte.StringToByteArray(hex); byte[] decompressedFile = CompressionManager.Decompress(compressedFile); hex = strToByte.ByteArrayToString(decompressedFile); string cabeceraDatos = ""; if (filesTranslatedNames[i].Equals("ds_event")) { cabeceraDatos = hex[212].ToString() + hex[213].ToString() + hex[210].ToString() + hex[211].ToString() + hex[208].ToString() + hex[209].ToString(); } else if (filesTranslatedNames[i].Contains("ds_eventm")) { cabeceraDatos = "0"; } else { cabeceraDatos = hex[210].ToString() + hex[211].ToString() + hex[208].ToString() + hex[209].ToString(); } int cabeceraDatosInt = Convert.ToInt32(cabeceraDatos, 16); string binaryData = ""; StringBuilder binaryData2 = new StringBuilder(); int k = 0; while (k < cabeceraDatosInt * 2) { binaryData2.Append(string.Format("{0:X2}", hex[k])); k++; } binaryData = binaryData2.ToString(); //Console.WriteLine("Datos binarios:" + binaryData); XDocument document = XDocument.Load("./DSSE-files/translation/" + filesTranslatedNames[i] + ".xml"); XElement root = document.Root; List <string> lines = new List <string>(); lines.AddRange(from el in root.Elements("Segment") where el.Element("Translated") != null select el.Element("Translated").Value); int numberofTextSegments = 0; for (int m = 0; m < lines.Count; m++) { lines[m] = lines[m].Replace("\n", "[br]"); if (!lines[m].Contains("[new]")) { lines[m] = "[00]" + lines[m]; numberofTextSegments++; } } string allLines = string.Join("", lines); allLines += "[00]"; //añadir último 00 //Console.WriteLine(allLines); var conversionRegex = new Regex(string.Join("|", conversion.Keys.Select(key => Regex.Escape(key)))); var textoConvertido = conversionRegex.Replace(allLines, n => conversion[n.Value]); //Console.WriteLine(textoConvertido); string punterosyTexto = ""; StringBuilder punterosyTexto2 = new StringBuilder(); for (int m = cabeceraDatosInt * 2; m < hex.Length; m++) { //punterosyTexto += string.Format("{0:X2}", hex[m]); punterosyTexto2.Append(string.Format("{0:X2}", hex[m])); } punterosyTexto = punterosyTexto2.ToString(); MatchCollection punterosMatch; if (filesTranslatedNames[i].Contains("ds_eventm")) { punterosMatch = Regex.Matches(punterosyTexto, "(?:....0000)", RegexOptions.RightToLeft); } else { punterosMatch = Regex.Matches(punterosyTexto, "....0{4}(0{2})(?!0*?$)", RegexOptions.RightToLeft); } //Console.WriteLine(4 + punterosMatch[0].Groups[0].Value); string punteros = ""; StringBuilder punteros2 = new StringBuilder(); for (int p = 0; p < 4 + punterosMatch[0].Groups[0].Index; p++) { punteros2.Append(string.Format("{0:X2}", punterosyTexto[p])); } punteros = punteros2.ToString(); //Console.WriteLine(punteros); MatchCollection punterosDiv = Regex.Matches(punteros, "(....)0000"); //Console.WriteLine(punterosDiv[0].Groups[1] + "\n\r"); //Console.WriteLine(punterosDiv[1].Groups[1]); //El primer valor que hay en punteros es la longitud de punteros + texto. [!] Hay que adaptarlo. //El segundo valor no sirve, solo resta -1 respecto al siguiente. //El tercer valor (2) es el primer puntero base. //En lines[] tengo las líneas convertidas en HEX ya para saber la longitud. var pointBase = ""; if (filesTranslatedNames[i].Contains("ds_eventm")) { pointBase = punterosDiv[1].Groups[1].Value; } else { pointBase = punterosDiv[2].Groups[1].Value; } //Console.WriteLine("Pointbase: "+pointBase); string pointers = ""; List <string> lineasHex = Regex.Split(textoConvertido.Substring(2), "00(?!0)").ToList(); //Console.WriteLine(lineasHex[0]); StringBuilder pointers2 = new StringBuilder(); for (int q = 0; q < lineasHex.Count() - 2; q++) //-2 porque el último texto no va punteado { var _pointBaseLE = pointBase.ToCharArray(); var _pointBaseBE = _pointBaseLE[2].ToString() + _pointBaseLE[3].ToString() + _pointBaseLE[0].ToString() + _pointBaseLE[1].ToString(); var _pointBaseBEDecimal = Convert.ToInt32(_pointBaseBE, 16); int lineSize = ((lineasHex[q].Length / 2) + 1); //Console.WriteLine(lineSize + "\n\r"); //Console.WriteLine("Point base: "+pointBase.ToString() + "\r\n"); var newPointerBE = Convert.ToInt32((_pointBaseBEDecimal + lineSize)).ToString("x").PadLeft(4, '0').ToCharArray(); string newPointerLE = newPointerBE[2].ToString() + newPointerBE[3].ToString() + newPointerBE[0].ToString() + newPointerBE[1].ToString(); //pointers += newPointerLE.ToString().PadRight(8, '0'); pointers2.Append(newPointerLE.ToString().PadRight(8, '0')); pointBase = newPointerLE; } if (filesTranslatedNames[i].Contains("ds_eventm")) { numberofTextSegments = Convert.ToInt32(numberofTextSegments); string numberofTextSegmentsS = numberofTextSegments.ToString("x").PadRight(8, '0'); //Console.WriteLine(numberofTextSegmentsS); pointers = numberofTextSegmentsS + punterosDiv[1].Groups[1].Value + "0000" + pointers2.ToString(); } else { pointers = punterosDiv[0].Groups[1].Value + "0000" + punterosDiv[1].Groups[1].Value + "0000" + punterosDiv[2].Groups[1].Value + "0000" + pointers2.ToString(); } //Console.WriteLine(pointers); //string archivoFinal = pointers + "00" + textoConvertido; string archivoFinal = ""; if ((filesTranslatedNames[i].Equals("ds_event") == false) && (filesTranslatedNames[i].Contains("ds_eventm") == false)) { archivoFinal = pointers + textoConvertido; } else if (filesTranslatedNames[i].Contains("ds_eventm") == true) { archivoFinal = pointers + textoConvertido.Substring(2); //Elimina los 00 añadidos antes al principio, en este tipo de ficheros sobra. } else { //Este fixedData es para el archivo "ds_event.cmp", tiene esos datos invariables. Para ahorrar su captura los pongo aquí directamente. string fixedData = "7E0000000000000062010000BD0100005E020000DB02000046030000B2030000020400005C040000FC04000077050000D50500004B06000082060000D706000045070000A5070000FC0700003A0800008F080000D80800003F090000E9090000520A00008E0A0000D80A0000370B0000930B0000E80B0000440C0000E50C00001B0D00007B0D0000E30D00004C0E0000A60E0000F70E0000420F0000840F0000F10F000086100000BB1000001F110000E01100003E12000093120000DA1200005F130000C613000079140000F314000071150000BE1500000F1600004D16000099160000EE1600006D170000CD1700001D18000082180000CB1800000919000058190000A5190000E31900004E1A0000D41A0000641B0000DA1B00001D1C0000691C0000AE1C00002A1D0000861D0000F01D00004C1E0000921E0000D21E0000141F0000551F0000F61F00003A2000007D200000CA2000003521000093210000EB21000046220000B8220000362300007F230000D3230000412400008F240000BE2400000425000039250000BC2500001A26000080260000D926000028270000772700001628000075280000B82800000D29000065290000D6290000382A0000AF2A00000D2B0000662B0000C92B00000D2C00005B2C0000B12C0000042D00003B2D00007F2D0000E72D0000232E00006E2E0000C82E00000E2F0000"; archivoFinal = pointers + fixedData + textoConvertido; } var datosLengthArray = Convert.ToInt32((archivoFinal.Length / 2)).ToString("x").PadLeft(4, '0').ToCharArray(); var datosLength = datosLengthArray[2].ToString() + datosLengthArray[3].ToString() + datosLengthArray[0].ToString() + datosLengthArray[1].ToString(); if (filesTranslatedNames[i].Contains("ds_eventm") == false) { archivoFinal = archivoFinal.Remove(0, 4); archivoFinal = archivoFinal.Insert(0, datosLength.ToString()); archivoFinal = binaryData + archivoFinal; } byte[] archivoFinalBin = strToByte.StringToByteArray(archivoFinal); //File.WriteAllBytes("./DSSE-files/output/" + filesTranslatedNames[i] + ".decmp", archivoFinalBin); byte[] compressedFileOutput = CompressionManager.Compress(archivoFinalBin); File.WriteAllBytes("./DSSE-files/output/" + filesTranslatedNames[i] + ".cmp", compressedFileOutput); Stream myStreamF = File.Open("./DSSE-files/output/" + filesTranslatedNames[i] + ".cmp", FileMode.Open); myStreamF.Seek(myStreamF.Length, SeekOrigin.Begin); while ((myStreamF.Length) % 4 != 0) { myStreamF.WriteByte(0x00); } myStreamF.Dispose(); //Console.WriteLine(archivoFinal); //Console.WriteLine("Progreso: " + (i + 1) + " de " + fileNumber); MainWindow.generateOutputBGW.ReportProgress(i + 1); } }
private void button1_Click(object sender, EventArgs e) { byte[] compressed = CompressionManager.Compress(Encoding.UTF8.GetBytes(tbxSource.Text)); tbxTarget.Text = Convert.ToBase64String(compressed); }