/// <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();
                }
        }
Exemplo n.º 2
0
 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));
 }
Exemplo n.º 3
0
        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());
        }
Exemplo n.º 4
0
        /// <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();
            }
        }
Exemplo n.º 5
0
        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);
            }
        }