- Timestamp:
- 07/07/15 13:02:06 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.