Beispiel #1
0
        public static bool ZipDirectory(string zipFileName, string directory, string password = null, string filefilter = null)
        {
            try
            {
                Directory.CreateDirectory(Path.GetDirectoryName(zipFileName));
                SharpZipLib.Zip.FastZipEvents events = new SharpZipLib.Zip.FastZipEvents();
                events.CompletedFile += (object sender, SharpZipLib.Core.ScanEventArgs e) =>
                {
                    LogUtil.Log("Zip Directory{0} Successed.", e.Name);
                };

                SharpZipLib.Zip.FastZip fz = new SharpZipLib.Zip.FastZip(events);
                fz.CreateEmptyDirectories = true;
                fz.Password = password;
                fz.CreateZip(zipFileName, directory, true, filefilter);
                fz = null;
                return(true);
            }
            catch (Exception e)
            {
                LogUtil.LogError(e.Message);
                return(false);
            }
        }
Beispiel #2
0
        public void Backup(BackupMode backupType)
        {
            if (!package.AppDataFolderExists)
            {
                return;
            }

            lock (isBusyLock)
            {
                Directory.CreateDirectory(Path.GetDirectoryName(BackupPath));

                bool exceptionOccured = false;

                var fastZipEvents = new ICSharpCode.SharpZipLib.Zip.FastZipEvents();
                fastZipEvents.FileFailure = (sender, e) =>
                {
                    exceptionOccured  = true;
                    e.ContinueRunning = false;
                };
                var zipFile = new ICSharpCode.SharpZipLib.Zip.FastZip(fastZipEvents)
                {
                    CreateEmptyDirectories = true
                };

                string fileFilter   = null;
                string folderFilter = null;

                switch (backupType)
                {
                case BackupMode.Smart:
                    fileFilter   = @";-Settings\\roaming\.lock$;-Settings\\settings\.dat\.LOG[0-9]*$;-ActivationStore\.dat\.LOG[0-9]*$";
                    folderFilter = @"-AC$;-LocalCache;-TempState";
                    break;

                case BackupMode.Full:
                    break;
                }

                IsBusy = true;
                try
                {
                    zipFile.CreateZip(BackupPath, package.AppDataFolderPath, true, fileFilter, folderFilter);
                }
                finally
                {
                    if (exceptionOccured)
                    {
                        try
                        {
                            File.Delete(BackupPath);
                        }
                        finally
                        {
                            IsBusy = false;
                            throw new InvalidOperationException($"« {package.DisplayName} » must be closed before saving");
                        }
                    }

                    IsBusy = false;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsBackuped)));
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BackupDate)));
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(State)));
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Version)));
                }
            }
        }