Esempio n. 1
0
        // Восстановить файлы/каталоги с помощью зеркального типа резервирования
        public static bool RestoreMirror(ref Scenario scenario, string version = "")
        {
            string Destination = scenario.Destination + exclude(scenario.Title);

            if (scenario.Zip)
            {
                Destination = Path.GetDirectoryName(scenario.Destination) + "\\" + MakeCopy.exclude(scenario.Title) + version + "\\" + Path.GetFileName(scenario.Destination);
            }

            ZipTarget = false;
            if (scenario.Zip)
            {
                ZipSource = true;

                // Проверить контрольную сумму
                if (!SHA512X.CheckSHA512(scenario.Destination))
                {
                    if (MessageBox.Show("Продолжить?", "Контрольная сумма архива повреждена или отсутствует", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    {
                        return(false);
                    }
                }
                try
                {
                    zip = ZipFile.Open(scenario.Destination, ZipArchiveMode.Read);
                }
                catch
                {
                    MessageBox.Show("Файл копии не найден", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }
                Destination = "";
            }
            else
            {
                ZipSource = false;
            };

            // Для всех файлов и каталогов из списка сделать точную копию (с созданием соответствующего подкаталога)
            bool result = true;

            foreach (var filename in scenario.Source)
            {
                if (filename[0] == '*')
                {
                    result &= RestoreDir(filename.Substring(1), Destination, version);
                }
                else
                {
                    result &= RestoreFile(filename, Destination, version, true);
                }
            }

            if (zip != null)
            {
                zip.Dispose();
            }

            return(result);
        }
Esempio n. 2
0
        // Восстановить файл
        static bool RestoreFile(string sourcefilename, string root, string version, bool AlarmCheckSum)
        // filename - имя восстанавливаемого файла
        {
            string destinationfilename = root + "\\" + version + "\\" + sourcefilename.Replace(@":\", @"\");

            destinationfilename = destinationfilename.Replace(@"\\", "\\");

            bool OK = !AlarmCheckSum; // Не проверять контрольную сумму

            OK |= ZipSource;          // Архив это
            if (!OK)
            {
                OK = SHA512X.CheckSHA512(destinationfilename); // Проверить контрольную сумму
            }
            if (!OK)
            {
                OK = MessageBox.Show("Выполнять восстановление?", "Не совпадает или отсутствует контрольная сумма", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                     == DialogResult.Yes;
            }
            if (OK)
            {
                return(CopyFile(destinationfilename, sourcefilename));
            }

            return(false);
        }
Esempio n. 3
0
        // Скопировать файл
        private static bool CopyFile(string sourcefilename, string root, string version, bool CreateCheckSum)
        {
            string destinationfilename = root + "\\" + version + "\\" + sourcefilename.Replace(@":\", @"\");

            destinationfilename = destinationfilename.Replace(@"\\", "\\");

            bool result = CopyFile(sourcefilename, destinationfilename);

            if (!ZipTarget)
            {
                SHA512X.CreateSHA512(destinationfilename);
            }

            return(result);
        }
Esempio n. 4
0
        //-----------------------------------------------------------------------------------------//

        // Копировать файлы/каталоги с помощью зеркального типа резервирования
        public static bool CopyMirror(ref Scenario scenario, string version = "")
        {
            string Destination = scenario.Destination + exclude(scenario.Title);

            if (scenario.Zip)
            {
                Destination = Path.GetDirectoryName(scenario.Destination) + "\\" + MakeCopy.exclude(scenario.Title) + version + "\\" + Path.GetFileName(scenario.Destination);
            }

            ZipSource = false;
            zip       = null;
            if (scenario.Zip)
            {
                ZipTarget = true;
                if (File.Exists(scenario.Destination))
                {
                    File.Delete(scenario.Destination);
                }
                try
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(Destination));
                    zip = ZipFile.Open(Destination, ZipArchiveMode.Create);
                }
                catch
                {
                    return(false);
                }
                Destination = "";
            }
            else
            {
                ZipTarget = false;
            };

            // Для всех файлов и каталогов из списка сделать точную копию (с созданием соответствующего подкаталога)
            bool result = true;

            foreach (var filename in scenario.Source)
            {
                if (filename[0] == '*')
                {
                    result &= CopyDir(filename.Substring(1), Destination, version, true);
                }
                else
                {
                    result &= CopyFile(filename, Destination, version, true);
                }
            }
            if (zip != null)
            {
                zip.Dispose();
            }
            try
            {
                if (scenario.Zip) // Создать контрольную сумму для всего архива
                {
                    SHA512X.CreateSHA512(scenario.Destination);
                }
            }
            catch
            {
                return(false);
            }

            return(result);
        }
Esempio n. 5
0
        // Скопировать каталог
        static bool CopyDir(string src, string dst, bool CreateSHA, bool CheckSHA)
        {
            if (ZipSource)
            {
                // Получить список всех файлов из ZIP, которые находятся "внутри" src
                // (в зависимости от их сорта CopyDir или CopyFile)
                src = ToZIPpath(src) + "/";

                foreach (var z in zip.Entries)
                {
                    if (z.ToString().IndexOf(src) >= 0)
                    {
                        string filename = dst + @"\";
                        filename = filename + z.Name; //z.ToString().Substring(src.Length);
                        if (File.Exists(filename))
                        {
                            File.Delete(filename);
                        }
                        z.ExtractToFile(filename);
                    }
                }
                return(true);
            }

            // Получить список файлов и подкаталогов
            bool          result = true;
            DirectoryInfo dir    = new DirectoryInfo(src);

            try
            {
                // Скопировать файлы
                var dirGetFiles = dir.GetFiles();
                foreach (var file in dirGetFiles)
                {
                    if (Path.GetExtension(file.Name) == ".sha512")
                    {
                        continue; // Не копировать контрольные суммы
                    }
                    string newdst = dst + "\\" + file.FullName.Substring(src.Length);
                    newdst = newdst.Replace(@"\\", @"\");

                    if (CheckSHA)
                    {
                        if (!ZipSource || SHA512X.CheckSHA512(file.FullName))
                        {
                            result &= CopyFile(file.FullName, newdst); // Все файлы
                            continue;
                        }
                    }
                    result &= CopyFile(file.FullName, newdst); // Все файлы
                    if (CreateSHA && !ZipTarget)
                    {
                        SHA512X.CreateSHA512(newdst);
                    }
                }

                // Скопировать подкаталоги
                foreach (var d in dir.GetDirectories())
                {
                    string newdst = dst + "\\" + d.FullName.Substring(src.Length);
                    newdst  = newdst.Replace(@"\\", @"\");
                    result &= CopyDir(d.FullName, newdst, CreateSHA, CheckSHA); // Все каталоги
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(src + " -> " + dst, "CopyDir: не могу скопировать");
                MessageBox.Show(ee.Message);
            }
            return(result);
        }