Changeset 12638 for trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml
- Timestamp:
- 07/07/15 13:02:06 (9 years ago)
- Location:
- trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
r12455 r12638 398 398 /// <param name="obj">The object.</param> 399 399 /// <param name="stream">The stream.</param> 400 /// <param name=" useZip">If true, uses zip for compression, otherwise gzip.</param>401 public static void Serialize(object obj, Stream stream, bool useZip = false) {402 Serialize(obj, stream, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), useZip);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); 403 403 } 404 404 … … 410 410 /// <param name="stream">The stream.</param> 411 411 /// <param name="config">The configuration.</param> 412 /// <param name=" useZip">If true, uses zip for compression, otherwise gzip.</param>413 public static void Serialize(object obj, Stream stream, Configuration config, bool useZip = false) {414 Serialize(obj, stream, config, false, useZip);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); 415 415 } 416 416 … … 422 422 /// <param name="config">The configuration.</param> 423 423 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 424 /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param> 425 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, bool useZip = false) { 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) { 426 427 try { 427 428 Serializer serializer = new Serializer(obj, config); 428 if ( useZip) {429 if (compressionType == CompressionType.Zip) { 429 430 Serialize(obj, stream, config, includeAssemblies, CompressionLevel.Optimal); 430 431 } else { … … 448 449 /// <param name="includeAssemblies">if set to <c>true</c> include need assemblies.</param> 449 450 /// <param name="types">The list of all serialized types.</param> 450 /// <param name="useZip">If true, uses zip for compression, otherwise gzip.</param> 451 public static void Serialize(object obj, Stream stream, Configuration config, bool includeAssemblies, out IEnumerable<Type> types, bool useZip = false) { 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) { 452 454 try { 453 455 Serializer serializer = new Serializer(obj, config); 454 if ( useZip) {456 if (compressionType == CompressionType.Zip) { 455 457 Serialize(stream, includeAssemblies, CompressionLevel.Optimal, serializer); 456 458 } else { -
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs
r12456 r12638 31 31 32 32 namespace HeuristicLab.Persistence.Default.Xml { 33 /// <summary> 34 /// Type of compression used for the Xml stream or file. 35 /// </summary> 36 public enum CompressionType { 37 GZip, 38 Zip 39 } 33 40 34 41 /// <summary> … … 216 223 } 217 224 218 219 /// <summary>220 /// Deserializes an object from the specified stream using GZip compression.221 /// </summary>222 /// <param name="stream">The stream.</param>223 /// <returns>A fresh object instance.</returns>224 private static object DeserializeWithGZip(Stream stream) {225 try {226 using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) {227 XmlParser parser = new XmlParser(reader);228 Deserializer deserializer = new Deserializer(new TypeMapping[] { });229 return deserializer.Deserialize(parser);230 }231 }232 catch (PersistenceException) {233 throw;234 }235 catch (Exception x) {236 throw new PersistenceException("Unexpected exception during deserialization", x);237 }238 }239 240 /// <summary>241 /// Deserializes an object from the specified stream using Zip compression.242 /// </summary>243 /// <param name="stream">The stream.</param>244 /// <returns>A fresh object instance.</returns>245 private static object DeserializeWithZip(Stream stream) {246 ZipArchive zipFile = new ZipArchive(stream);247 return Deserialize(zipFile);248 }249 250 225 /// <summary> 251 226 /// Deserializes an object from the specified stream. … … 253 228 /// <typeparam name="T">object type expected from the serialized stream</typeparam> 254 229 /// <param name="stream">The stream.</param> 255 /// <param name=" useZip">If true, uses zip for decompression, otherwise gzip.</param>230 /// <param name="compressionType">Type of compression, default is GZip.</param> 256 231 /// <returns>A fresh object instance.</returns> 257 public static T Deserialize<T>(Stream stream, bool useZip = false) {258 return (T)Deserialize(stream, useZip);232 public static T Deserialize<T>(Stream stream, CompressionType compressionType = CompressionType.GZip) { 233 return (T)Deserialize(stream, compressionType); 259 234 } 260 235 … … 263 238 /// </summary> 264 239 /// <param name="stream">The stream.</param> 265 /// <param name=" useZip">If true, uses zip for decompression, otherwise gzip.</param>240 /// <param name="compressionType">Type of compression, default is GZip.</param> 266 241 /// <returns>A fresh object instance.</returns> 267 public static object Deserialize(Stream stream, bool useZip = false) { 268 if (useZip) { 269 return DeserializeWithZip(stream); 242 public static object Deserialize(Stream stream, CompressionType compressionType = CompressionType.GZip) { 243 if (compressionType == CompressionType.Zip) { 244 ZipArchive zipFile = new ZipArchive(stream); 245 return Deserialize(zipFile); 270 246 } else { 271 return DeserializeWithGZip(stream); 247 try { 248 using (StreamReader reader = new StreamReader(new GZipStream(stream, CompressionMode.Decompress))) { 249 XmlParser parser = new XmlParser(reader); 250 Deserializer deserializer = new Deserializer(new TypeMapping[] { }); 251 return deserializer.Deserialize(parser); 252 } 253 } 254 catch (PersistenceException) { 255 throw; 256 } 257 catch (Exception x) { 258 throw new PersistenceException("Unexpected exception during deserialization", x); 259 } 272 260 } 273 261 }
Note: See TracChangeset
for help on using the changeset viewer.