Changeset 12694 for branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
- Timestamp:
- 07/09/15 13:07:30 (9 years ago)
- Location:
- branches/HeuristicLab.Problems.Orienteering
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.Problems.Orienteering
- Property svn:mergeinfo changed
-
Property
svn:global-ignores
set to
*.nuget
packages
-
branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Persistence
- Property svn:mergeinfo changed
-
branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r11185 r12694 1 1 #region License Information 2 2 /* HeuristicLab 3 * Copyright (C) 2002-201 4Heuristic and Evolutionary Algorithms Laboratory (HEAL)3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL) 4 4 * 5 5 * This file is part of HeuristicLab. … … 30 30 using HeuristicLab.Persistence.Interfaces; 31 31 using HeuristicLab.Tracing; 32 using ICSharpCode.SharpZipLib.Zip;33 32 34 33 namespace HeuristicLab.Persistence.Default.Xml { … … 286 285 /// <param name="filename">The filename.</param> 287 286 public static void Serialize(object o, string filename) { 288 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, 5);287 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, CompressionLevel.Optimal); 289 288 } 290 289 … … 297 296 /// <param name="filename">The filename.</param> 298 297 /// <param name="compression">ZIP file compression level</param> 299 public static void Serialize(object o, string filename, intcompression) {298 public static void Serialize(object o, string filename, CompressionLevel compression) { 300 299 Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression); 301 300 } … … 309 308 /// <param name="config">The configuration.</param> 310 309 public static void Serialize(object obj, string filename, Configuration config) { 311 Serialize(obj, filename, config, false, 5); 310 Serialize(obj, filename, config, false, CompressionLevel.Optimal); 311 } 312 313 private static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies 314 , CompressionLevel compression) { 315 Serializer serializer = new Serializer(obj, config); 316 Serialize(stream, includeAssemblies, compression, serializer); 317 } 318 319 private static void Serialize(Stream stream, bool includeAssemblies, CompressionLevel compression, Serializer serializer) { 320 try { 321 DateTime start = DateTime.Now; 322 serializer.InterleaveTypeInformation = false; 323 XmlGenerator generator = new XmlGenerator(); 324 using (ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Create)) { 325 ZipArchiveEntry entry = zipArchive.CreateEntry("data.xml", compression); 326 using (StreamWriter writer = new StreamWriter(entry.Open())) { 327 foreach (ISerializationToken token in serializer) { 328 string line = generator.Format(token); 329 writer.Write(line); 330 } 331 } 332 entry = zipArchive.CreateEntry("typecache.xml", compression); 333 using (StreamWriter writer = new StreamWriter(entry.Open())) { 334 foreach (string line in generator.Format(serializer.TypeCache)) { 335 writer.Write(line); 336 } 337 } 338 if (includeAssemblies) { 339 foreach (string name in serializer.RequiredFiles) { 340 Uri uri = new Uri(name); 341 if (!uri.IsFile) { 342 Logger.Warn("cannot read non-local files"); 343 continue; 344 } 345 entry = zipArchive.CreateEntry(Path.GetFileName(uri.PathAndQuery), compression); 346 using (BinaryWriter bw = new BinaryWriter(entry.Open())) { 347 using (FileStream reader = File.OpenRead(uri.PathAndQuery)) { 348 byte[] buffer = new byte[1024 * 1024]; 349 while (true) { 350 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 351 if (bytesRead == 0) 352 break; 353 bw.Write(buffer, 0, bytesRead); 354 } 355 } 356 } 357 } 358 } 359 } 360 Logger.Info(String.Format("serialization took {0} seconds with compression level {1}", 361 (DateTime.Now - start).TotalSeconds, compression)); 362 } 363 catch (Exception) { 364 Logger.Warn("Exception caught, no data has been serialized."); 365 throw; 366 } 312 367 } 313 368 … … 320 375 /// <param name="includeAssemblies">if set to <c>true</c> include needed assemblies.</param> 321 376 /// <param name="compression">The ZIP compression level.</param> 322 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, intcompression) {377 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression) { 323 378 try { 324 379 string tempfile = Path.GetTempFileName(); 325 DateTime start = DateTime.Now; 380 326 381 using (FileStream stream = File.Create(tempfile)) { 327 Serializer serializer = new Serializer(obj, config); 328 serializer.InterleaveTypeInformation = false; 329 XmlGenerator generator = new XmlGenerator(); 330 using (ZipOutputStream zipStream = new ZipOutputStream(stream)) { 331 zipStream.IsStreamOwner = false; 332 zipStream.SetLevel(compression); 333 zipStream.PutNextEntry(new ZipEntry("data.xml") { DateTime = DateTime.MinValue }); 334 StreamWriter writer = new StreamWriter(zipStream); 335 foreach (ISerializationToken token in serializer) { 336 string line = generator.Format(token); 337 writer.Write(line); 338 } 339 writer.Flush(); 340 zipStream.PutNextEntry(new ZipEntry("typecache.xml") { DateTime = DateTime.MinValue }); 341 foreach (string line in generator.Format(serializer.TypeCache)) { 342 writer.Write(line); 343 } 344 writer.Flush(); 345 if (includeAssemblies) { 346 foreach (string name in serializer.RequiredFiles) { 347 Uri uri = new Uri(name); 348 if (!uri.IsFile) { 349 Logger.Warn("cannot read non-local files"); 350 continue; 351 } 352 zipStream.PutNextEntry(new ZipEntry(Path.GetFileName(uri.PathAndQuery))); 353 FileStream reader = File.OpenRead(uri.PathAndQuery); 354 byte[] buffer = new byte[1024 * 1024]; 355 while (true) { 356 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 357 if (bytesRead == 0) 358 break; 359 zipStream.Write(buffer, 0, bytesRead); 360 } 361 writer.Flush(); 362 } 363 } 364 } 365 } 366 Logger.Info(String.Format("serialization took {0} seconds with compression level {1}", 367 (DateTime.Now - start).TotalSeconds, compression)); 382 Serialize(obj, stream, config, includeAssemblies, compression); 383 } 384 368 385 File.Copy(tempfile, filename, true); 369 386 File.Delete(tempfile); … … 381 398 /// <param name="obj">The object.</param> 382 399 /// <param name="stream">The stream.</param> 383 public static void Serialize(object obj, Stream stream) { 384 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat())); 400 /// <param name="compressionType">Type of compression, default is GZip.</param> 401 public static void Serialize(object obj, Stream stream, CompressionType compressionType = CompressionType.GZip) { 402 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), compressionType); 385 403 } 386 404 … … 392 410 /// <param name="stream">The stream.</param> 393 411 /// <param name="config">The configuration.</param> 394 public static void Serialize(object obj, Stream stream, Configuration config) { 395 Serialize(obj, stream, config, false); 412 /// <param name="compressionType">Type of compression, default is GZip.</param> 413 public static void Serialize(object obj, Stream stream, Configuration config, CompressionType compressionType = CompressionType.GZip) { 414 Serialize(obj, stream, config, false, compressionType); 396 415 } 397 416 … … 403 422 /// <param name="config">The configuration.</param> 404 423 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 405 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies) { 424 /// <param name="compressionType">Type of compression, default is GZip.</param> 425 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, 426 CompressionType compressionType = CompressionType.GZip) { 406 427 try { 407 428 Serializer serializer = new Serializer(obj, config); 408 Serialize(stream, serializer); 409 } catch (PersistenceException) { 429 if (compressionType == CompressionType.Zip) { 430 Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal); 431 } else { 432 Serialize(stream, serializer); 433 } 434 } 435 catch (PersistenceException) { 410 436 throw; 411 } catch (Exception e) { 437 } 438 catch (Exception e) { 412 439 throw new PersistenceException("Unexpected exception during Serialization.", e); 413 440 } … … 422 449 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 423 450 /// <param name="types">The list of all serialized types.</param> 424 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types) { 451 /// <param name="compressionType">Type of compression, default is GZip.</param> 452 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types, 453 CompressionType compressionType = CompressionType.GZip) { 425 454 try { 426 455 Serializer serializer = new Serializer(obj, config); 427 Serialize(stream, serializer); 456 if (compressionType == CompressionType.Zip) { 457 Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer); 458 } else { 459 Serialize(stream, serializer); 460 } 428 461 types = serializer.SerializedTypes; 429 } catch (PersistenceException) { 462 } 463 catch (PersistenceException) { 430 464 throw; 431 } catch (Exception e) { 465 } 466 catch (Exception e) { 432 467 throw new PersistenceException("Unexpected exception during Serialization.", e); 433 468 }
Note: See TracChangeset
for help on using the changeset viewer.