/// <summary> /// Creates a new backup file /// </summary> /// <param name="path">The virtual path</param> /// <returns></returns> public Stream CreateFile(string path) { if (path == null) throw new ArgumentNullException("path"); if (!path.StartsWith("./")) throw new ArgumentOutOfRangeException("Argument 'path' is out of range.", new FormatException("The path must begin with './")); var tempFolderPath = Path.Combine(_repository.Path, "temp"); Directory.CreateDirectory(tempFolderPath); string tempPath; do { tempPath = Path.Combine(tempFolderPath, _random.Next().ToString(CultureInfo.InvariantCulture)); } while (File.Exists(tempPath)); var stream = new CloseEventStream(new FileStream(tempPath, FileMode.Create, FileAccess.Write)); stream.Closed += delegate { var actualPath = "data" + path.Substring(1); actualPath += "." + FileHelpers.CalculateHash(tempPath); var entry = new ZipEntry(actualPath); _outputStream.PutNextEntry(entry); using (var sourceStream = File.OpenRead(tempPath)) { sourceStream.CopyTo(_outputStream); } _outputStream.CloseEntry (); File.Delete(tempPath); }; return stream; }
protected Stream InternalOpenFile(string path) { var archive = new ZipFile(FilePath); var entry = archive.Cast<ZipEntry> ().Single(a=>a.Name == path); if (entry == null) { throw new FileNotFoundException("File does not exist."); } var eventStream = new CloseEventStream(archive.GetInputStream(entry)); eventStream.Closed += (s, e) => archive.Close (); return eventStream; }