public void CompressedStream_SerializePersonA()
        {
            AddressA addressA = new AddressA(@"CornerStreet", @"00501", @"New york");
            PersonA  personA  = new PersonA(@"Bob", @"Bones", addressA);

            byte[] memory;

            using (MemoryStream stream = new MemoryStream())
            {
                // compressing stream only forced to flush when the stream is closed
                using (Stream compressed = Compression.CompressingStream(stream))
                {
                    SerializationHelper.Serialize(compressed, personA);
                    Assert.IsTrue(stream.Length > 0);
                }

                // copy only after the compressing stream has been closed
                memory = stream.ToArray();
            }

            PersonA deserializedPersonA;

            using (Stream stream = new MemoryStream(memory))
                using (Stream decompressed = Compression.DecompressingStream(stream))
                {
                    deserializedPersonA = SerializationHelper.Deserialize <PersonA>(decompressed);
                }

            Assert.AreEqual(personA, deserializedPersonA);
        }
예제 #2
0
 /// <summary>
 ///     Serializes an object to a file.
 /// </summary>
 /// <param name="fileName">The name of the file.</param>
 /// <param name="obj">The object to serialize.</param>
 /// <param name="requiredCompress">Whether the file needs to be compressed.</param>
 public static void SerializeToFile(string fileName, object obj, bool requiredCompress)
 {
     if (requiredCompress)
     {
         using (Stream str = Compression.CompressingStream(new FileStream(fileName, FileMode.Create)))
         {
             Serialize(str, obj);
         }
     }
     else
     {
         using (Stream str = new FileStream(fileName, FileMode.Create))
         {
             Serialize(str, obj);
         }
     }
 }