Ejemplo n.º 1
0
        private static ReadOnlyCollection <FileInfo> SelectFilesToArchive(ArchivizerConfiguration configuration)
        {
            var result = new List <FileInfo>();

            foreach (var configurationForDirectory in configuration.ArchivizerConfigurationsForDirectory.Values)
            {
                var files = Directory.GetFiles(configurationForDirectory.DirectoryFullName);

                var archiveFiles = new HashSet <FileInfo>();
                var sourceFiles  = new HashSet <FileInfo>();

                foreach (var file in files)
                {
                    var fileInfo = new FileInfo(file);
                    if (fileInfo.Extension == $".{configurationForDirectory.FormatArchiwum}")
                    {
                        archiveFiles.Add(fileInfo);
                    }
                    else if (fileInfo.Extension == $".{configurationForDirectory.FileExtensionToCompression}")
                    {
                        sourceFiles.Add(fileInfo);
                    }
                }

                var fileToArchive = sourceFiles.Except(archiveFiles, new FileInfoComparer()).ToList();
                result.AddRange(fileToArchive);
            }
            return(new ReadOnlyCollection <FileInfo>(result));
        }
Ejemplo n.º 2
0
 private static void DeleteSourceFile(ArchivizerConfiguration configuration, FileInfo fileInfo)
 {
     if (configuration.DeleteSourceFileAfterArchiving)
     {
         File.Delete(fileInfo.FullName);
     }
 }
Ejemplo n.º 3
0
        private static ReadOnlyCollection <FileInfo> SelectArchiveToDelete(ArchivizerConfiguration configuration)
        {
            var archiveToDelete = new List <FileInfo>();

            if (!configuration.MaxNumberOfLatestArchiveFilesInKept.HasValue)
            {
                return(new ReadOnlyCollection <FileInfo>(archiveToDelete));
            }

            foreach (var configurationForDirectory in configuration.ArchivizerConfigurationsForDirectory.Values)
            {
                var subResult = new SortedDictionary <DateTime, List <FileInfo> >();
                var files     = Directory.GetFiles(configurationForDirectory.DirectoryFullName);

                foreach (var file in files)
                {
                    var fileInfo = new FileInfo(file);
                    if (fileInfo.Extension == $".{configurationForDirectory.FormatArchiwum}")
                    {
                        var match = Regex.Match(fileInfo.Name, @"\d{4}_\d{2}_\d{2}");
                        if (match.Success)
                        {
                            var archiveCreateDate = DateTime.ParseExact(match.Value, @"yyyy_MM_dd",
                                                                        new System.Globalization.CultureInfo("pl-PL"));

                            if (subResult.ContainsKey(archiveCreateDate))
                            {
                                subResult[archiveCreateDate].Add(fileInfo);
                            }
                            else
                            {
                                subResult.Add(archiveCreateDate, new List <FileInfo> {
                                    fileInfo
                                });
                            }
                        }
                    }
                }

                var i = 1;
                foreach (var item in subResult.Reverse())
                {
                    if (i > configuration.MaxNumberOfLatestArchiveFilesInKept.Value)
                    {
                        archiveToDelete.AddRange(item.Value);
                    }
                    i++;
                }
            }
            return(new ReadOnlyCollection <FileInfo>(archiveToDelete));
        }
 private void ValidateConfiguration(ArchivizerConfiguration configuration)
 {
     if (!File.Exists(configuration.Achivizer7zFullName))
     {
         throw new ConfigurationErrorsException("Configuration is incorrect. Archiver not found in the indicated path");
     }
     foreach (var item in configuration.ArchivizerConfigurationsForDirectory.Values)
     {
         if (!Directory.Exists(item.DirectoryFullName))
         {
             throw new ConfigurationErrorsException($"Configuration is incorrect. Directory {item.DirectoryFullName} not exist");
         }
     }
 }
Ejemplo n.º 5
0
        private static void ArchiveFile(ArchivizerConfiguration configuration, FileInfo fileInfo)
        {
            var configurationForDirectory = configuration.ArchivizerConfigurationsForDirectory[fileInfo.DirectoryName];
            var targetName = Path.Combine(fileInfo.DirectoryName, Path.GetFileNameWithoutExtension(fileInfo.FullName) + $".{configurationForDirectory.FormatArchiwum}");

            var p = new ProcessStartInfo()
            {
                FileName = configuration.Achivizer7zFullName, WindowStyle = ProcessWindowStyle.Hidden
            };

            p.Arguments = $"a -t{configurationForDirectory.FormatArchiwum} \"" + targetName + "\" \"" + fileInfo.FullName + $"\" -mx={(int)configurationForDirectory.CompressionLevel}";

            Process x = Process.Start(p);

            x.WaitForExit();
        }
        public ArchivizerConfiguration ReadConfiguration()
        {
            ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap()
            {
                ExeConfigFilename = "ArchivizerConfiguration.config"
            };
            var config = SystemConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

            var globalConfiguration = config.GetSection("GlobalConfigurationSection") as GlobalConfigurationSection;
            var supportedDirectory  = config.GetSection("SupportedArchivizerConfigurationForDirectorySection") as SupportedArchivizerConfigurationForDirectorySection;

            var configurations = supportedDirectory.GetConfiguration();

            var configuration = new ArchivizerConfiguration
            {
                Achivizer7zFullName                  = globalConfiguration.Archivizer7zFullName,
                DeleteSourceFileAfterArchiving       = globalConfiguration.DeleteSourceFileAfterArchiving,
                ArchivizerConfigurationsForDirectory = new ReadOnlyDictionary <string, ArchivizerConfigurationForDirectory>(configurations),
                MaxNumberOfLatestArchiveFilesInKept  = globalConfiguration.MaxNumberOfLatestArchiveFilesInKept
            };

            ValidateConfiguration(configuration);
            return(configuration);
        }