private void OpenForUpdate(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead || !stream.CanWrite || !stream.CanSeek) { throw new InvalidOperationException("Incorrect input stream capabilities in archive update mode"); } _mode = EPFArchiveMode.Update; _MainStream = stream; //Create BackStream from temporary file CreateBackupStream(); //Backup archive stream (MainStream) to BackStream UpdateBackStream(); //Reading will be done from BackStream and writing will be done to MainStream ArchiveReader = new BinaryReader(_BackStream); ReadEntries(); }
public void Save() { if (_mode == EPFArchiveMode.Read) { throw new InvalidOperationException("Unable to save in read-only mode"); } if (_MainStream == null && _mode == EPFArchiveMode.Create) { throw new InvalidOperationException("SaveAs has to be used when archive was created."); } try { using (var binWriter = new BinaryWriter(_MainStream, System.Text.Encoding.UTF8, true)) { _saveProgressEventArgs = new SaveProgressEventArgs(); _saveProgressEventArgs.EventType = SaveProgressEventType.SavingStarted; OnSaveProgress(_saveProgressEventArgs); _saveProgressEventArgs.EntriesTotal = _entries.Count; //Write remaining entries to archive WriteEntries(binWriter); if (_mode == EPFArchiveMode.Create) { CreateBackupStream(); } //Update BackStream with new MainStream content UpdateBackStream(); _saveProgressEventArgs.EventType = SaveProgressEventType.SavingCompleted; OnSaveProgress(_saveProgressEventArgs); } if (_mode == EPFArchiveMode.Create) { //Reading will be done from BackStream and writing will be done to MainStream ArchiveReader = new BinaryReader(_BackStream); _mode = EPFArchiveMode.Update; } } finally { _saveProgressEventArgs = null; ModifiedEntriesNo = 0; } }
private void OpenForCreate(Stream stream) { if (!stream.CanWrite) { throw new InvalidOperationException("Incorrect input stream capabilities in archive create mode"); } _mode = EPFArchiveMode.Create; _MainStream = stream; // (disposeBackStream) // // _BackStream = null; // ArchiveReader = null; // }
/// <summary> /// Open EPF file in read mode. /// Only reading entries will be possible /// </summary> /// <param name="stream"></param> private void OpenForRead(Stream stream) { if (stream == null) { throw new ArgumentNullException(nameof(stream)); } if (!stream.CanRead) { throw new ArgumentException("Can't read from input stream"); } _mode = EPFArchiveMode.Read; //This is the main data stream _MainStream = stream; //There is no back stream necesary in read mode _BackStream = null; ArchiveReader = new BinaryReader(_MainStream); ReadEntries(); }
private void Init(Stream stream, EPFArchiveMode mode) { _entries = new List <EPFArchiveEntry>(); Entries = new ReadOnlyCollection <EPFArchiveEntry>(_entries); _entryDictionary = new Dictionary <string, EPFArchiveEntry>(StringComparer.OrdinalIgnoreCase); // check stream against mode switch (mode) { case EPFArchiveMode.Create: _MainStream = null; _isModified = true; break; case EPFArchiveMode.Read: OpenForRead(stream); break; case EPFArchiveMode.Update: OpenForUpdate(stream); break; } }
private EPFArchive(Stream stream, EPFArchiveMode mode, bool leaveOpen) { _leaveOpen = leaveOpen; PropertyChanged += EPFArchive_PropertyChanged; Init(stream, mode); }