コード例 #1
0
ファイル: ScadaInstance.cs プロジェクト: xxq860725/scada
        /// <summary>
        /// Упаковать конфигурацию в архив
        /// </summary>
        public bool PackConfig(string destFileName, ConfigOptions configOptions)
        {
            try
            {
                List <RelPath> configPaths      = GetConfigPaths(configOptions.ConfigParts);
                PathDict       excludedPathDict = PrepareExcludedPaths(configOptions.ExcludedPaths);

                using (FileStream fileStream =
                           new FileStream(destFileName, FileMode.Create, FileAccess.Write, FileShare.Read))
                {
                    using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Create))
                    {
                        foreach (RelPath relPath in configPaths)
                        {
                            PackDir(zipArchive, relPath, excludedPathDict);
                        }

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteException(ex, Localization.UseRussian ?
                                   "Ошибка при упаковке конфигурации в архив" :
                                   "Error packing configuration into archive");
                return(false);
            }
        }
コード例 #2
0
ファイル: ScadaInstance.cs プロジェクト: xxq860725/scada
        /// <summary>
        /// Распаковать архив конфигурации
        /// </summary>
        public bool UnpackConfig(string srcFileName, ConfigOptions configOptions)
        {
            try
            {
                // удаление существующей конфигурации
                List <RelPath> configPaths = GetConfigPaths(configOptions.ConfigParts);
                PathDict       pathDict    = PrepareExcludedPaths(configOptions.ExcludedPaths);

                foreach (RelPath relPath in configPaths)
                {
                    ClearDir(relPath, pathDict);
                }

                // определение допустимых директорий для распаковки
                ConfigParts   configParts    = configOptions.ConfigParts;
                List <string> allowedEntries = new List <string>(AllConfigParts.Length);

                foreach (ConfigParts configPart in AllConfigParts)
                {
                    if (configParts.HasFlag(configPart))
                    {
                        allowedEntries.Add(GetConfigPartDir(configPart, '/'));
                    }
                }

                // распаковка новой конфигурации
                using (FileStream fileStream =
                           new FileStream(srcFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (ZipArchive zipArchive = new ZipArchive(fileStream, ZipArchiveMode.Read))
                    {
                        string instanceDir = Settings.Directory;

                        foreach (ZipArchiveEntry entry in zipArchive.Entries)
                        {
                            if (StartsWith(entry.FullName, allowedEntries, StringComparison.Ordinal))
                            {
                                string relPath      = entry.FullName.Replace('/', Path.DirectorySeparatorChar);
                                string destFileName = instanceDir + relPath;
                                Directory.CreateDirectory(Path.GetDirectoryName(destFileName));
                                entry.ExtractToFile(destFileName, true);
                            }
                        }

                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                log.WriteException(ex, Localization.UseRussian ?
                                   "Ошибка при распаковке конфигурации из архива" :
                                   "Error unpacking configuration from archive");
                return(false);
            }
        }