/// <summary> Decompress the existing file to the specified target. </summary> public static void Decompress(string source, string target) { using (ReplaceFile replace = new ReplaceFile(target)) using (FileStream sourceStream = File.Open(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (Stream targetStream = replace.Create()) IOStream.Decompress(sourceStream, targetStream); if (sourceStream.Length != sourceStream.Position) { throw new System.IO.IOException(Resources.IOStreamCompressionFailed); } sourceStream.Dispose(); replace.Commit(); } }
public void TestIsFullPath() { string tmpname = Guid.NewGuid().ToString(); Assert.IsFalse(File.Exists(tmpname)); using (ReplaceFile f = new ReplaceFile(tmpname)) { Assert.IsTrue(Path.IsPathRooted(f.TargetFile)); Assert.AreEqual(Environment.CurrentDirectory, Path.GetDirectoryName(f.TargetFile)); } Assert.IsFalse(File.Exists(tmpname)); using (TransactFile f = new TransactFile(tmpname)) { Assert.IsTrue(Path.IsPathRooted(f.TargetFile)); Assert.AreEqual(Environment.CurrentDirectory, Path.GetDirectoryName(f.TargetFile)); } Assert.IsFalse(File.Exists(tmpname)); }
public void TestReplaceFiles() { string testdata = Guid.NewGuid().ToString(); string tmpPath; TempFile replace = new TempFile(); using (ReplaceFile temp = new ReplaceFile(replace.TempPath)) { Assert.AreEqual(temp.TargetFile, replace.TempPath); tmpPath = temp.TempPath; Assert.IsTrue(temp.Exists); temp.WriteAllText(testdata); //missing commit: //temp.Commit(); } Assert.AreEqual(0, replace.Length); Assert.IsFalse(File.Exists(tmpPath)); string backupfile = Path.ChangeExtension(replace.TempPath, ".bak"); File.Delete(Path.ChangeExtension(replace.TempPath, ".bak")); Assert.IsFalse(File.Exists(backupfile)); replace.WriteAllText("backup"); //now for real using (ReplaceFile temp = new ReplaceFile(replace.TempPath, ".bak")) { tmpPath = temp.TempPath; Assert.IsTrue(temp.Exists); temp.WriteAllText(testdata); temp.Commit(); } Assert.IsFalse(File.Exists(tmpPath)); Assert.AreEqual(testdata, replace.ReadAllText()); Assert.IsTrue(File.Exists(backupfile)); using (TempFile fbackup = TempFile.Attach(backupfile)) Assert.AreEqual("backup", fbackup.ReadAllText()); }
/// <summary> Decompress the existing file to the specified target. </summary> public static void Decompress(string source, string target) { using (ReplaceFile replace = new ReplaceFile(target)) using (FileStream sourceStream = File.Open(source, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (Stream targetStream = replace.Create()) IOStream.Decompress(sourceStream, targetStream); if (sourceStream.Length != sourceStream.Position) throw new System.IO.IOException(Resources.IOStreamCompressionFailed); sourceStream.Dispose(); replace.Commit(); } }
public void TestReplaceRollback() { string testdata = Guid.NewGuid().ToString(); using (TempFile replace = new TempFile()) using (ReplaceFile temp = new ReplaceFile(replace.TempPath)) { temp.WriteAllText(testdata); Assert.IsTrue(temp.Exists); temp.Rollback(); Assert.IsFalse(temp.Exists); } }