Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/06/15 10:08:54 (10 years ago)
Author:
ascheibe
Message:

#2247 merged r11650, r11651 into stable

Location:
stable
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Persistence

  • stable/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r11170 r11932  
    3030using HeuristicLab.Persistence.Interfaces;
    3131using HeuristicLab.Tracing;
    32 using ICSharpCode.SharpZipLib.Zip;
    3332
    3433namespace HeuristicLab.Persistence.Default.Xml {
     
    286285    /// <param name="filename">The filename.</param>
    287286    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);
    289288    }
    290289
     
    297296    /// <param name="filename">The filename.</param>
    298297    /// <param name="compression">ZIP file compression level</param>
    299     public static void Serialize(object o, string filename, int compression) {
     298    public static void Serialize(object o, string filename, CompressionLevel compression) {
    300299      Serialize(o, filename, ConfigurationService.Instance.GetConfiguration(new XmlFormat()), false, compression);
    301300    }
     
    309308    /// <param name="config">The configuration.</param>
    310309    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);
    312311    }
    313312
     
    320319    /// <param name="includeAssemblies">if set to <c>true</c> include needed assemblies.</param>
    321320    /// <param name="compression">The ZIP compression level.</param>
    322     public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, int compression) {
     321    public static void Serialize(object obj, string filename, Configuration config, bool includeAssemblies, CompressionLevel compression) {
    323322      try {
    324323        string tempfile = Path.GetTempFileName();
     
    328327          serializer.InterleaveTypeInformation = false;
    329328          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              }
    338336            }
    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              }
    343342            }
    344             writer.Flush();
    345343            if (includeAssemblies) {
    346344              foreach (string name in serializer.RequiredFiles) {
     
    350348                  continue;
    351349                }
    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                  }
    360361                }
    361                 writer.Flush();
    362362              }
    363363            }
     
    407407        Serializer serializer = new Serializer(obj, config);
    408408        Serialize(stream, serializer);
    409       } catch (PersistenceException) {
     409      }
     410      catch (PersistenceException) {
    410411        throw;
    411       } catch (Exception e) {
     412      }
     413      catch (Exception e) {
    412414        throw new PersistenceException("Unexpected exception during Serialization.", e);
    413415      }
     
    427429        Serialize(stream, serializer);
    428430        types = serializer.SerializedTypes;
    429       } catch (PersistenceException) {
     431      }
     432      catch (PersistenceException) {
    430433        throw;
    431       } catch (Exception e) {
     434      }
     435      catch (Exception e) {
    432436        throw new PersistenceException("Unexpected exception during Serialization.", e);
    433437      }
  • stable/HeuristicLab.Persistence/3.3/Default/Xml/XmlParser.cs

    r11170 r11932  
    2929using HeuristicLab.Persistence.Core.Tokens;
    3030using HeuristicLab.Persistence.Interfaces;
    31 using ICSharpCode.SharpZipLib.Zip;
    3231
    3332namespace HeuristicLab.Persistence.Default.Xml {
     
    193192      TimeSpan start = System.Diagnostics.Process.GetCurrentProcess().TotalProcessorTime;
    194193      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          }
    197198        }
    198199      }
     
    247248    }
    248249
    249     private static object Deserialize(ZipFile zipFile) {
    250       try {
    251         ZipEntry typecache = zipFile.GetEntry("typecache.xml");
    252         if (typecache == null)
    253           throw new PersistenceException("file does not contain typecache.xml");
    254         Deserializer deSerializer = new Deserializer(ParseTypeCache(new StreamReader(zipFile.GetInputStream(typecache))));
    255         ZipEntry data = zipFile.GetEntry("data.xml");
    256         if (data == null)
    257           throw new PersistenceException("file does not contain data.xml");
    258         XmlParser parser = new XmlParser(
    259           new StreamReader(zipFile.GetInputStream(data)));
    260         object result = deSerializer.Deserialize(parser);
    261         zipFile.Close();
     250    private static object Deserialize(ZipArchive zipFile) {
     251      try {
     252        ZipArchiveEntry typecache = zipFile.GetEntry("typecache.xml");
     253        if (typecache == null) throw new PersistenceException("file does not contain typecache.xml");
     254        Deserializer deSerializer;
     255        using (StreamReader sr = new StreamReader(typecache.Open())) {
     256          deSerializer = new Deserializer(ParseTypeCache(sr));
     257        }
     258
     259        ZipArchiveEntry data = zipFile.GetEntry("data.xml");
     260        if (data == null) throw new PersistenceException("file does not contain data.xml");
     261        object result;
     262        using (StreamReader sr = new StreamReader(data.Open())) {
     263          XmlParser parser = new XmlParser(sr);
     264          result = deSerializer.Deserialize(parser);
     265        }
     266
    262267        return result;
    263268      }
Note: See TracChangeset for help on using the changeset viewer.