private static String CreateZipFile(String source, BaseOptions opts) { DirectoryInfo pathSource = new DirectoryInfo(source); if (!pathSource.Exists) { return(String.Empty); } String pathTemp = Path.Combine(Path.GetTempPath(), "SimplyUpdate"); if (!Directory.Exists(pathTemp)) { Directory.CreateDirectory(pathTemp); } String zipFile = Path.Combine(pathTemp, "software.zip"); if (File.Exists(zipFile)) { File.Delete(zipFile); } List <String> files = new List <string>(); var excludeFiles = opts.ExcludeFiles .SelectMany(p => pathSource.GetFiles(p, SearchOption.AllDirectories)) .Select(f => f.FullName) .ToArray(); var includeFiles = opts.IncludeFiles .SelectMany(p => pathSource.GetFiles(p, SearchOption.AllDirectories)) .Select(f => f.FullName) .ToArray(); files.AddRange(includeFiles.Except(excludeFiles)); Console.WriteLine("Zip files."); using (ZipArchive archive = ZipFile.Open(zipFile, ZipArchiveMode.Create)) files.ForEach(f => { var filename = f.Remove(0, pathSource.FullName.Length + 1); Console.Write("Zip file " + filename); archive.CreateEntryFromFile(f, filename, CompressionLevel.Optimal); Console.WriteLine(" .. OK"); }); return(zipFile); }
private static XDocument CreateOrUpdateVersionFile(XDocument currentFile, String zipFile, BaseOptions opts) { XDocument retVal = new XDocument( new XDeclaration("1.0", "UTF-8", "yes"), new XComment("\nSimply Update version file\nGenerated by SimplyUpdate.BuildUpdate (https://github.com/Artusoft/SimplyUpdate.BuildUpdate)\n"), new XElement("Liveupdate", new XElement("Version", 0) )); Int32 previousVer = 0; if (currentFile != null) { var ndVersion = currentFile.Descendants("Version").FirstOrDefault(); Int32.TryParse(ndVersion.Value, out previousVer); } var nd = retVal.Descendants("Version").FirstOrDefault(); Int32 currentVer = 0; if (nd != null) { if (opts.Version != 0) { nd.Value = (currentVer = opts.Version).ToString(); } else { nd.Value = (currentVer = previousVer + 1).ToString(); } } Console.WriteLine("Hashing zip file."); var MD5Hash = ComputeHash(zipFile); nd = retVal.Descendants("MD5").FirstOrDefault(); if (nd == null) { retVal.Element("Liveupdate").Add(new XElement("MD5", Convert.ToBase64String(MD5Hash))); } else { nd.Value = Convert.ToBase64String(MD5Hash); } nd = retVal.Descendants("FileLenght").FirstOrDefault(); if (nd == null) { retVal.Element("Liveupdate").Add(new XElement("FileLenght", (new System.IO.FileInfo(zipFile)).Length)); } else { nd.Value = (new System.IO.FileInfo(zipFile)).Length.ToString(); } nd = retVal.Descendants("FileVersion").FirstOrDefault(); if (nd == null && !String.IsNullOrEmpty(opts.FileVersion)) { retVal.Element("Liveupdate").Add(new XElement("FileVersion", opts.FileVersion)); } else { if (!String.IsNullOrEmpty(opts.FileVersion)) { nd.Value = opts.FileVersion; } else { nd?.Remove(); } } Console.WriteLine($"Version {previousVer} -> {currentVer}"); return(retVal); }