Ticket #2247: persistence.patch
File persistence.patch, 10.9 KB (added by ascheibe, 9 years ago) |
---|
-
HeuristicLab.Core.Views/3.3/Clipboard.cs
23 23 using System.Collections; 24 24 using System.Collections.Generic; 25 25 using System.IO; 26 using System.IO.Compression; 26 27 using System.Linq; 27 28 using System.Threading; 28 29 using System.Windows.Forms; … … 197 198 try { 198 199 i++; 199 200 SetEnabledStateOfContentViews(item, false); 200 XmlGenerator.Serialize(item, ItemsPath + Path.DirectorySeparatorChar + i.ToString("00000000") + ".hl", 9);201 XmlGenerator.Serialize(item, ItemsPath + Path.DirectorySeparatorChar + i.ToString("00000000") + ".hl", CompressionLevel.Optimal); 201 202 OnItemSaved(item, progressBar.Maximum / listView.Items.Count); 202 203 } 203 204 catch (Exception) { } -
HeuristicLab.Core/3.3/PersistenceContentManager.cs
19 19 */ 20 20 #endregion 21 21 22 using System.IO.Compression; 22 23 using HeuristicLab.Common; 23 24 using HeuristicLab.Persistence.Default.Xml; 24 25 … … 31 32 } 32 33 33 34 protected override void SaveContent(IStorableContent content, string filename, bool compressed) { 34 XmlGenerator.Serialize(content, filename, compressed ? 9 : 0);35 XmlGenerator.Serialize(content, filename, compressed ? CompressionLevel.Optimal : CompressionLevel.NoCompression); 35 36 } 36 37 } 37 38 } -
HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs
29 29 using HeuristicLab.Persistence.Core.Tokens; 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 { 35 34 … … 285 284 /// <param name="o">The object.</param> 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 291 290 /// <summary> … … 296 295 /// <param name="o">The object.</param> 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 } 302 301 … … 308 307 /// <param name="filename">The filename.</param> 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); 312 311 } 313 312 314 313 /// <summary> … … 319 318 /// <param name="config">The configuration.</param> 320 319 /// <param name="includeAssemblies">if set to <c>true</c> include needed assemblies.</param> 321 320 /// <param name="compression">The ZIP compression level.</param> 322 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, intcompression) {321 public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression) { 323 322 try { 324 323 string tempfile = Path.GetTempFileName(); 325 324 DateTime start = DateTime.Now; … … 327 326 Serializer serializer = new Serializer(obj, config); 328 327 serializer.InterleaveTypeInformation = false; 329 328 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); 329 using (ZipArchive zipArchive = new ZipArchive(stream, ZipArchiveMode.Create)) { 330 ZipArchiveEntry entry = zipArchive.CreateEntry("data.xml", compression); 331 using (StreamWriter writer = new StreamWriter(entry.Open())) { 332 foreach (ISerializationToken token in serializer) { 333 string line = generator.Format(token); 334 writer.Write(line); 335 } 338 336 } 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); 337 entry = zipArchive.CreateEntry("typecache.xml", compression); 338 using (StreamWriter writer = new StreamWriter(entry.Open())) { 339 foreach (string line in generator.Format(serializer.TypeCache)) { 340 writer.Write(line); 341 } 343 342 } 344 writer.Flush();345 343 if (includeAssemblies) { 346 344 foreach (string name in serializer.RequiredFiles) { 347 345 Uri uri = new Uri(name); … … 349 347 Logger.Warn("cannot read non-local files"); 350 348 continue; 351 349 } 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); 350 entry = zipArchive.CreateEntry(Path.GetFileName(uri.PathAndQuery), compression); 351 using (BinaryWriter bw = new BinaryWriter(entry.Open())) { 352 using (FileStream reader = File.OpenRead(uri.PathAndQuery)) { 353 byte[] buffer = new byte[1024 * 1024]; 354 while (true) { 355 int bytesRead = reader.Read(buffer, 0, 1024 * 1024); 356 if (bytesRead == 0) 357 break; 358 bw.Write(buffer, 0, bytesRead); 359 } 360 } 360 361 } 361 writer.Flush();362 362 } 363 363 } 364 364 } … … 406 406 try { 407 407 Serializer serializer = new Serializer(obj, config); 408 408 Serialize(stream, serializer); 409 } catch (PersistenceException) { 409 } 410 catch (PersistenceException) { 410 411 throw; 411 } catch (Exception e) { 412 } 413 catch (Exception e) { 412 414 throw new PersistenceException("Unexpected exception during Serialization.", e); 413 415 } 414 416 } … … 426 428 Serializer serializer = new Serializer(obj, config); 427 429 Serialize(stream, serializer); 428 430 types = serializer.SerializedTypes; 429 } catch (PersistenceException) { 431 } 432 catch (PersistenceException) { 430 433 throw; 431 } catch (Exception e) { 434 } 435 catch (Exception e) { 432 436 throw new PersistenceException("Unexpected exception during Serialization.", e); 433 437 } 434 438 } -
HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs
28 28 using HeuristicLab.Persistence.Core; 29 29 using HeuristicLab.Persistence.Core.Tokens; 30 30 using HeuristicLab.Persistence.Interfaces; 31 using ICSharpCode.SharpZipLib.Zip;32 31 33 32 namespace HeuristicLab.Persistence.Default.Xml { 34 33 … … 192 191 public static object Deserialize(string filename) { 193 192 TimeSpan start = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime; 194 193 try { 195 using (ZipFile file = new ZipFile(filename)) { 196 return Deserialize(file); 194 using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { 195 using (ZipArchive zip = new ZipArchive(fs)) { 196 return Deserialize(zip); 197 } 197 198 } 198 199 } 199 200 finally { … … 246 247 return (T)Deserialize(stream); 247 248 } 248 249 249 private static object Deserialize(Zip File zipFile) {250 private static object Deserialize(ZipArchive zipFile) { 250 251 try { 251 Zip Entry typecache = zipFile.GetEntry("typecache.xml");252 ZipArchiveEntry typecache = zipFile.GetEntry("typecache.xml"); 252 253 if (typecache == null) 253 254 throw new PersistenceException("file does not contain typecache.xml"); 254 Deserializer deSerializer = new Deserializer(ParseTypeCache(new StreamReader( zipFile.GetInputStream(typecache))));255 Zip Entry data = zipFile.GetEntry("data.xml");255 Deserializer deSerializer = new Deserializer(ParseTypeCache(new StreamReader(typecache.Open()))); 256 ZipArchiveEntry data = zipFile.GetEntry("data.xml"); 256 257 if (data == null) 257 258 throw new PersistenceException("file does not contain data.xml"); 258 259 XmlParser parser = new XmlParser( 259 new StreamReader( zipFile.GetInputStream(data)));260 new StreamReader(data.Open())); 260 261 object result = deSerializer.Deserialize(parser); 261 zipFile.Close(); 262 262 263 return result; 263 264 } 264 265 catch (PersistenceException) { -
HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj
127 127 </Reference> 128 128 <Reference Include="System.Data" /> 129 129 <Reference Include="System.Drawing" /> 130 <Reference Include="System.IO.Compression" /> 130 131 <Reference Include="System.Xml" /> 131 132 </ItemGroup> 132 133 <ItemGroup>