Beispiel #1
0
 public static byte[] Compress(object source)
 {
     try
     {
         XmlSerializer xmlSerializer = new XmlSerializer(source.GetType());
         MemoryStream  memoryStream  = new MemoryStream();
         xmlSerializer.Serialize((Stream)memoryStream, source);
         byte[] source1 = memoryStream.ToArray();
         memoryStream.Close();
         int    length     = source1.Length;
         int    destLength = source1.Length + 1;
         byte[] dest       = new byte[destLength];
         if (ZLib.compress2(dest, ref destLength, source1, source1.Length, ZLib.ZLibCompressionLevel.Z_BEST_COMPRESSION) != ZLib.ZLibError.Z_OK)
         {
             return(new byte[0]);
         }
         byte[] numArray = new byte[destLength + 4];
         Array.Copy((Array)dest, 0, (Array)numArray, 4, destLength);
         numArray[0] = (byte)length;
         numArray[1] = (byte)(length >> 8);
         numArray[2] = (byte)(length >> 16);
         numArray[3] = (byte)(length >> 24);
         return(numArray);
     }
     catch (Exception ex)
     {
         return(new byte[0]);
     }
 }
Beispiel #2
0
 public static byte[] Compress(object source)
 {
     byte[] numArray;
     try
     {
         XmlSerializer xmlSerializer = new XmlSerializer(source.GetType());
         MemoryStream  memoryStream  = new MemoryStream();
         xmlSerializer.Serialize(memoryStream, source);
         byte[] array = memoryStream.ToArray();
         memoryStream.Close();
         int    length    = (int)array.Length;
         int    num       = (int)array.Length + 1;
         byte[] numArray1 = new byte[num];
         if (ZLib.compress2(numArray1, ref num, array, (int)array.Length, ZLib.ZLibCompressionLevel.Z_BEST_COMPRESSION) == ZLib.ZLibError.Z_OK)
         {
             byte[] numArray2 = new byte[num + 4];
             Array.Copy(numArray1, 0, numArray2, 4, num);
             numArray2[0] = (byte)length;
             numArray2[1] = (byte)(length >> 8);
             numArray2[2] = (byte)(length >> 16);
             numArray2[3] = (byte)(length >> 24);
             numArray     = numArray2;
         }
         else
         {
             numArray = new byte[0];
         }
     }
     catch (Exception exception)
     {
         numArray = new byte[0];
     }
     return(numArray);
 }