コード例 #1
0
ファイル: FormMain.cs プロジェクト: giorgizek/ZekV1
        private void bsDeploy_DoWork(object sender, DoWorkEventArgs e)
        {
            var updateDir      = txtDirectoryUpdate.Text.Trim();
            var destinationDir = txtDirectoryDestination.Text.Trim();
            var xmlFile        = Path.Combine(destinationDir, "update.txt");

            var xml = (XmlUpdate)e.Argument;

            //add files into xml
            foreach (var item in _fileInfo.Where(x => x.Checked))
            {
                xml.Files.Add(new XmlUpdate.XmlFile(item.Path, item.Path.Substring(updateDir.Length + 1), item.Hash));
            }
            var compressDir = Path.Combine(destinationDir, xml.CompressFolderName);

            if (!Directory.Exists(compressDir))
            {
                Directory.CreateDirectory(compressDir);
            }

            var deleteFiles = Directory.GetFiles(destinationDir).ToList();

            if (deleteFiles.Contains(xmlFile))
            {
                deleteFiles.Remove(xmlFile);
            }
            var deleteCompressedFiles = Directory.GetFiles(compressDir);

            var comparer = new PropertyComparer <XmlUpdate.XmlFile>("Hash");
            var distinct = xml.Files.Distinct(comparer).ToList();

            decimal max = deleteFiles.Count + deleteCompressedFiles.Length + distinct.Count;


            var oldCompressEqualsCurrent = false;

            if (File.Exists(xmlFile))
            {
                XmlUpdate oldXml = null;//Creating old serialized XmlUpdate from xml file
                try { oldXml = Ext.DeserializeXml <XmlUpdate>(File.ReadAllBytes(xmlFile)); }
                catch { }
                oldCompressEqualsCurrent = oldXml != null && oldXml.Compress == xml.Compress;
            }

            for (int i = 0; i < deleteFiles.Count; i++)
            {
                bsDeploy.ReportProgress((int)((1m + i) * progressFiles.Maximum / max), "Deleting: " + Path.GetFileName(deleteFiles[i]));
                File.Delete(deleteFiles[i]);
            }

            for (int i = 0; i < deleteCompressedFiles.Length; i++)
            {
                var file = Path.GetFileName(deleteCompressedFiles[i]);
                //if old compressed file equals current then just skip.
                if (oldCompressEqualsCurrent && distinct.Any(x => x.Hash + xml.Extension == file))
                {
                    continue;
                }

                bsDeploy.ReportProgress((int)((1m + deleteFiles.Count + i) * progressFiles.Maximum / max), "Deleting: " + file);
                File.Delete(deleteCompressedFiles[i]);
            }


            for (int i = 0; i < distinct.Count; i++)
            {
                var item = distinct[i];
                var file = Path.Combine(compressDir, item.Hash + xml.Extension);
                if (File.Exists(file))
                {
                    continue;                   //if old compressed file already exists then skip
                }
                bsDeploy.ReportProgress((int)((1 + deleteFiles.Count + deleteCompressedFiles.Length + i) * progressFiles.Maximum / max), "Compressing: " + Path.GetFileName(item.Path) + "  To: " + item.Hash + xml.Extension);
                switch (xml.Compress.ToLowerInvariant())
                {
                case "":
                    File.Copy(item.Path, file);
                    break;

                case "gzip":
                    GZipHelper.CompressFile(item.Path, file);
                    break;

                //case "zip":
                //    SharpZLibHelper.CompressFile(item.Path, Path.Combine(compressFolder, item.Hash + xml.Extension));
                //    progressFiles.PerformStep();
                //    break;

                case "7zip":
                    SevenZipHelper.CompressFileLZMA(item.Path, file);
                    break;

                default:
                    throw new ArgumentException("Invalid compress type (use: gzip, 7zip).");
                }
            }
            File.WriteAllText(xmlFile, Ext.SerializeXml(xml));
        }