Exemple #1
0
 private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer)
 {
     try {
         DateTime start = DateTime.Now;
         serializer.InterleaveTypeInformation = false;
         XmlGenerator generator = new XmlGenerator();
         using (ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Create)) {
             ZipArchiveEntry entry = zipArchive.CreateEntry("data.xml", compression);
             using (StreamWriter writer = new StreamWriter(entry.Open())) {
                 foreach (ISerializationToken token in serializer)
                 {
                     string line = generator.Format(token);
                     writer.Write(line);
                 }
             }
             entry = zipArchive.CreateEntry("typecache.xml", compression);
             using (StreamWriter writer = new StreamWriter(entry.Open())) {
                 foreach (string line in generator.Format(serializer.TypeCache))
                 {
                     writer.Write(line);
                 }
             }
             if (includeAssemblies)
             {
                 foreach (string name in serializer.RequiredFiles)
                 {
                     Uri uri = new Uri(name);
                     if (!uri.IsFile)
                     {
                         Logger.Warn("cannot read non-local files");
                         continue;
                     }
                     entry = zipArchive.CreateEntry(Path.GetFileName(uri.PathAndQuery), compression);
                     using (BinaryWriter bw = new BinaryWriter(entry.Open())) {
                         using (FileStream reader = File.OpenRead(uri.PathAndQuery)) {
                             byte[] buffer = new byte[1024 * 1024];
                             while (true)
                             {
                                 int bytesRead = reader.Read(buffer, 0, 1024 * 1024);
                                 if (bytesRead == 0)
                                 {
                                     break;
                                 }
                                 bw.Write(buffer, 0, bytesRead);
                             }
                         }
                     }
                 }
             }
         }
         Logger.Info(String.Format("serialization took {0} seconds with compression level {1}",
                                   (DateTime.Now - start).TotalSeconds, compression));
     }
     catch (Exception) {
         Logger.Warn("Exception caught, no data has been serialized.");
         throw;
     }
 }
Exemple #2
0
 private static void Serialize(Stream stream, Serializer serializer)
 {
     using (StreamWriter writer = new StreamWriter(new GZipStream(stream, CompressionMode.Compress))) {
         serializer.InterleaveTypeInformation = true;
         XmlGenerator generator = new XmlGenerator();
         foreach (ISerializationToken token in serializer)
         {
             string line = generator.Format(token);
             writer.Write(line);
         }
         writer.Flush();
     }
 }
Exemple #3
0
 private static void Serialize(Stream stream, Core.Serializer serializer, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (StreamWriter writer = new StreamWriter(new GZipStream(stream, CompressionMode.Compress))) {
         serializer.InterleaveTypeInformation = true;
         XmlGenerator generator = new XmlGenerator();
         foreach (ISerializationToken token in serializer)
         {
             cancellationToken.ThrowIfCancellationRequested();
             string line = generator.Format(token);
             writer.Write(line);
         }
         writer.Flush();
     }
 }