public void Compress(CompressorOption option) { bool ignore_all = false; PrimeArchiverType type = PrimeArchiverType.Nothing; processors.Clear(); processors.Add(new CompressFile()); if (!String.IsNullOrEmpty(option.Password)) { type |= PrimeArchiverType.Password; EncryptFile ef = new EncryptFile(); ef.SetKey(option.Password); processors.Add(ef); } header = new Header.Header(); HeaderItemPath hip = new HeaderItemPath(); try { string temp_archive = TempNameGenerator.GenerateTempNameFromFile(option.Output); using (FileStream archiveStream = new FileStream(temp_archive, FileMode.Create, FileAccess.Write)) { IncludesPathCreate(option); for (int i = 0; i < header.Items.Count; i++) { HeaderItemPath hip_file = new HeaderItemPath(); try { Process(header.Items[i].AbsolutePath); if (header.Items[i].Length != 0) { hip_file.UpdateCurrentPath(header.Items[i].AbsolutePath); foreach (IProcessFile processor in processors) { hip_file.UpdateCurrentPath(processor.ProcessExecute(hip_file.GetCurrentPath())); } using (FileStream fr = new FileStream(hip_file.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { header.Items[i].SetLentgh(fr.Length); transfer.Transfer(fr, archiveStream); } } } catch (Exception ex) { if (ignore_all) { continue; } OperationErrorAction action = OperationErrorAction.Abort; switch (action) { case OperationErrorAction.Abort: throw ex; case OperationErrorAction.Ignore: continue; case OperationErrorAction.IgnoreAll: ignore_all = true; continue; case OperationErrorAction.Replay: i--; break; } } finally { hip_file.ClearTemporeryPathes(false); } } } string header_path = TempNameGenerator.GenerateTempNameFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "header.dat")); hip.UpdateCurrentPath(header_path); using (FileStream fs = new FileStream(hip.GetCurrentPath(), FileMode.Create, FileAccess.Write)) { new StreamTransfer().Transfer(new MemoryStream(header.ToArray()), fs); } foreach (IProcessFile processor in processors) { hip.UpdateCurrentPath(processor.ProcessExecute(hip.GetCurrentPath())); } using (FileStream endArchiveStream = new FileStream(option.Output, FileMode.Create, FileAccess.Write)) { using (FileStream fr = new FileStream(hip.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { endArchiveStream.WriteByte((byte)type); int after_processors_header_length = (int)fr.Length; endArchiveStream.Write(BitConverter.GetBytes(after_processors_header_length), 0, sizeof(int)); transfer.Transfer(fr, endArchiveStream); } using (FileStream fr = new FileStream(temp_archive, FileMode.Open, FileAccess.Read)) { transfer.Transfer(fr, endArchiveStream); } Operations.DeleteFile(temp_archive); } } catch (Exception ex) { Process(ex.Message); } finally { hip.ClearTemporeryPathes(true); processors.Clear(); } }
/// <summary> /// Создание архива /// </summary> /// <param name="option">Опции создания архива</param> public void Compress(CompressorOption option) { bool ignore_all = false; //Определим параметры сжатия PrimeArchiverType type = PrimeArchiverType.Nothing; processors.Clear(); if (option.WithoutCompress) { type |= PrimeArchiverType.NoCompression; } else { processors.Add(new CompressFile()); } if (!String.IsNullOrEmpty(option.Password)) { type |= PrimeArchiverType.Password; EncryptFile ef = new EncryptFile(); ef.SetKey(option.Password); processors.Add(ef); } //Создаем заголовок архива header = new Header(); //Пути для заголовка HeaderItemPath hip = new HeaderItemPath(); try { string temp_archive = TempNameGenerator.GenerateTempNameFromFile(option.Output); using (FileStream archiveStream = new FileStream(temp_archive, FileMode.Create, FileAccess.Write)) { //Собираем заголовок IncludesPathCreate(option); //Перебираем элементы архива for (int i = 0; i < header.Items.Count; i++) { HeaderItemPath hip_file = new HeaderItemPath(); try { Process(header.Items[i].AbsolutePath); //Выбираем только файлы if (header.Items[i].Length != 0) { hip_file.UpdateCurrentPath(header.Items[i].AbsolutePath); //Прогоняем файл через процессоры foreach (IProcessFile processor in processors) { hip_file.UpdateCurrentPath(processor.ProcessExecute(hip_file.GetCurrentPath())); } //Записываем в конечный файл архива using (FileStream fr = new FileStream(hip_file.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { header.Items[i].SetLentgh(fr.Length); transfer.Transfer(fr, archiveStream); } } } catch (Exception ex) { if (ignore_all) { continue; } OperationErrorAction action = OperationErrorAction.Abort; if (ErrorProcessing != null) { action = ErrorProcessing(header.Items[i].RelativePath, ex.Message); } switch (action) { case OperationErrorAction.Abort: throw ex; case OperationErrorAction.Ignore: continue; case OperationErrorAction.IgnoreAll: ignore_all = true; continue; case OperationErrorAction.Replay: i--; break; } } finally { hip_file.ClearTemporeryPathes(option.RemoveSource); } } } //Сохраняем заголовок string header_path = TempNameGenerator.GenerateTempNameFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "header.dat")); hip.UpdateCurrentPath(header_path); using (FileStream fs = new FileStream(hip.GetCurrentPath(), FileMode.Create, FileAccess.Write)) { new StreamTransfer().Transfer(new MemoryStream(header.ToArray()), fs); } //Прогоняем файл заголовка через процессоры foreach (IProcessFile processor in processors) { hip.UpdateCurrentPath(processor.ProcessExecute(hip.GetCurrentPath())); } //Собираем архив using (FileStream endArchiveStream = new FileStream(option.Output, FileMode.Create, FileAccess.Write)) { using (FileStream fr = new FileStream(hip.GetCurrentPath(), FileMode.Open, FileAccess.Read)) { //Запишем тип архива endArchiveStream.WriteByte((byte)type); //Запишем длину заголовка после обработок int after_processors_header_length = (int)fr.Length; endArchiveStream.Write(BitConverter.GetBytes(after_processors_header_length), 0, sizeof(int)); //Запишем обработанный заголовок transfer.Transfer(fr, endArchiveStream); } using (FileStream fr = new FileStream(temp_archive, FileMode.Open, FileAccess.Read)) { transfer.Transfer(fr, endArchiveStream); } Operations.DeleteFile(temp_archive); } } catch (Exception ex) { Process(ex.Message); } finally { //Удаляем временные файлы и при необходимости источники архива hip.ClearTemporeryPathes(true); processors.Clear(); } }
private void button3_Click(object sender, EventArgs e) { Result = OperationErrorAction.Replay; Close(); }
private void bIgnoreAll_Click(object sender, EventArgs e) { Result = OperationErrorAction.IgnoreAll; Close(); }
private void button1_Click(object sender, EventArgs e) { Result = OperationErrorAction.Abort; Close(); }