Exemplo n.º 1
0
        public static bool Backup(SetupData setupData, string localFolder)
        {
            if (string.IsNullOrEmpty(localFolder) || !Directory.Exists(localFolder))
            {
                return(false);
            }

            var backupFolder = IO.BackupFolder(setupData, localFolder);

            if (string.IsNullOrEmpty(backupFolder) || !Directory.Exists(backupFolder))
            {
                return(false);
            }

            var          backupFile = Path.Combine(backupFolder, DateTime.Today.ToString("yyyyMMdd") + "_" + setupData.Executable.Version.ToString() + ".zip");
            StringWriter zipLog     = new StringWriter();
            bool         success    = false;
            bool         exception  = false;
            var          zip        = new ZipFile
            {
                CompressionLevel        = Ionic.Zlib.CompressionLevel.BestCompression,
                StatusMessageTextWriter = zipLog,
                ZipErrorAction          = ZipErrorAction.InvokeErrorEvent
            };

            zip.ZipError += (object sender, ZipErrorEventArgs e) =>
            {
                zipLog.WriteLine("ERROR: " + e.ArchiveName + " " + e.Exception.Message);
                exception = true;
            };
            zip.SaveProgress += (object sender, SaveProgressEventArgs e) =>
            {
                switch (e.EventType)
                {
                case ZipProgressEventType.Adding_Started:
                case ZipProgressEventType.Adding_Completed:
                case ZipProgressEventType.Saving_Started:
                    zipLog.WriteLine(e.EventType);
                    break;

                case ZipProgressEventType.Saving_Completed:
                    zipLog.WriteLine(e.EventType);
                    success = true;
                    break;

                case ZipProgressEventType.Error_Saving:
                    zipLog.WriteLine(e.EventType + " " + e.CurrentEntry.FileName);
                    exception = true;
                    break;
                }
            };
            zip.UpdateFile(Path.Combine(localFolder, setupData.Executable.File), "\\");
            foreach (var f in setupData.Files)
            {
                zip.UpdateFile(Path.Combine(localFolder, f.Folder, f.File), f.Folder);
            }


            zip.Save(backupFile);

            Log.Add(zipLog.ToString() + "\nSaved to " + backupFile, "BACKUP");

            return(success && !exception);
        }