public void Upgrade() { if (NewVersionUrl == null) { throw new ProgramUpgraderException("Can't get info from new version file"); } if (!HasNewVersionAvailable()) { throw new ProgramUpgraderException("No call of upgrade if HasNewVersionAvailable is false"); } string temporyDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString().ToUpperInvariant()); byte[] array = _webaccess.GetFile(NewVersionUrl); Zipper.UnZipAll(new MemoryStream(array), temporyDirectory); if (!Directory.Exists(temporyDirectory)) { throw new DirectoryNotFoundException("Can't upgrade, unzipped directory not found"); } Assembly entryAssembly = Assembly.GetEntryAssembly(); //From http://www.codeproject.com/Articles/31454/How-To-Make-Your-Application-Delete-Itself-Immedia ProcessStartInfo info = new ProcessStartInfo(); info.Arguments = string.Format("/C choice /C Y /N /D Y /T 5 & copy /Y \"{0}\" \"{1}\"", Path.Combine(temporyDirectory, "*.*"), Path.GetDirectoryName(entryAssembly.Location)); info.WindowStyle = ProcessWindowStyle.Hidden; info.CreateNoWindow = true; info.FileName = "cmd.exe"; Process.Start(info); }
internal string GeneratePictures(bool tempDir = false) { Assembly executingAssembly = Assembly.GetExecutingAssembly(); string outDir = tempDir ? Path.GetTempPath() : Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string name = executingAssembly.GetManifestResourceNames().First(s => s.EndsWith("MagicPicture.zip")); using (Stream stream = executingAssembly.GetManifestResourceStream(name)) { Zipper.UnZipAll(stream, outDir); return(outDir); } }
internal string Generate(bool tempDir = false) { string resourceName = DatabaseGenerator.GetResourceName(); Assembly executingAssembly = Assembly.GetExecutingAssembly(); string outDir = tempDir ? Path.GetTempPath() : Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string name = executingAssembly.GetManifestResourceNames().First(s => s.EndsWith(resourceName.Replace(".sqlite", ".zip"))); using (Stream stream = executingAssembly.GetManifestResourceStream(name)) { Zipper.UnZipAll(stream, outDir); return(outDir); } }
public void TestUnzipAll([Values(true, false)] bool fromStream) { Assert.IsTrue(File.Exists(ZipFileName), "Can't find the test file"); string temporyDirectory = Path.Combine(Path.GetTempPath(), "TestUnzip"); string[] fileNames = { "File1.txt", "File2.txt" }; const string subDirName = "Subdir"; try { if (Directory.Exists(temporyDirectory)) { Directory.Delete(temporyDirectory, true); } if (fromStream) { using (Stream fs = new FileStream(ZipFileName, FileMode.Open)) { Zipper.UnZipAll(fs, temporyDirectory); } } else { byte[] bytes = File.ReadAllBytes(ZipFileName); Zipper.UnZipAll(bytes, temporyDirectory); } string[] files = Directory.GetFiles(temporyDirectory); Assert.IsTrue(files.Length == 2, "The unzipped direcory must have 2 files in the root"); List <string> expectedfiles = new List <string>(fileNames); foreach (string file in files) { using (StreamReader sr = new StreamReader(file)) { string fileName = Path.GetFileName(file); Assert.IsTrue(expectedfiles.Contains(fileName), "The file name is not in the expected list"); expectedfiles.Remove(fileName); string content = sr.ReadToEnd(); Assert.IsTrue(content == fileName, "The file {0} doesn't content the expected text", file); } } string[] directories = Directory.GetDirectories(temporyDirectory); Assert.IsTrue(directories.Length == 1, "The unzipped direcory must have 1 sub directory in the root"); string subDirectory = directories[0]; Assert.IsTrue(subDirectory == Path.Combine(temporyDirectory, subDirName), "The sub direcory has not the expected name"); files = Directory.GetFiles(subDirectory); Assert.IsTrue(files.Length == 2, "The unzipped direcory must have 2 files in the sub directory"); expectedfiles = new List <string>(fileNames); foreach (string file in files) { using (StreamReader sr = new StreamReader(file)) { string fileName = Path.GetFileName(file); Assert.IsTrue(expectedfiles.Contains(fileName), "The file name is not in the expected list"); expectedfiles.Remove(fileName); string content = sr.ReadToEnd(); Assert.IsTrue(content == string.Format("{0}\\{1}", subDirName, fileName), "The file {0} doesn't content the expected text", file); } } Assert.IsTrue(Directory.GetDirectories(subDirectory).Length == 0, "The unzipped direcory must not have any sub-sub directory in the root"); } finally { if (Directory.Exists(temporyDirectory)) { Directory.Delete(temporyDirectory, true); } } }