public void DeserializeProfile(string filename, ref Profile p) { FileInfo fi = new FileInfo(filename); if (fi.Exists) { using (ZipFile zip = ZipFile.Read(filename)) { string profFile = "profile.twp"; foreach (string fileInZip in zip.EntryFileNames) { if (fileInZip.Equals(profFile, StringComparison.InvariantCultureIgnoreCase)) { Debug.WriteLine("Found profile file in ZIP archive. Extracting it now"); zip[fileInZip].Extract(profFile); profFile = Path.Combine(DATA_STORE_FOLDER, profFile); TextReader reader = new StreamReader(profFile); try { p = (Profile)profile_serializer.Deserialize(reader); } catch (Exception) { throw new InvalidOperationException("Invalid File Format"); } finally { reader.Close(); } if (dm.isProfileNameUnique(p.Name) && !Directory.Exists(p.GetDataFolder())) { zip.ExtractAll(DATA_STORE_FOLDER, ExtractExistingFileAction.OverwriteSilently); } else { throw new InvalidOperationException("There is already an existing profile named " + p.Name + ". Delete or rename it before importing."); } File.Delete(profFile); } } } } else { throw new FileNotFoundException("Could not locate file: " + filename); } }
public void SerializeProfile(string filename, Profile p) { Stream stream = new MemoryStream(); TextWriter writer = new StreamWriter(stream); try { profile_serializer.Serialize(writer, p); } catch (Exception e) { writer.Close(); string s = e.StackTrace; throw new InvalidOperationException("Error Serialzing Profile"); } try { using (ZipFile zip = new ZipFile()) { zip.AddEntry("profile.twp", stream); zip.AddDirectory(p.GetDataFolder(), p.Name); zip.Save(filename); } } catch (Exception e) { throw new InvalidOperationException("Error Exporting Profile: " + e.Message); } if (writer != null) writer.Close(); }