public void Encode(string source_file_or_folder_path, string path_for_decoded_folder, string directory_path_for_encoded) { canc_token.ThrowIfCancellationRequested(); string message = "The file " + source_file_or_folder_path + " was deleted or renamed.\n" + "Please, restore it or skip it's archiving\n"; while (true) { PathChecking checked_correctness = (PathChecking)controller.Invoke(controller.OfferToSkip, source_file_or_folder_path, message); //source_file_or_folder_path); if (checked_correctness == PathChecking.wants_to_skip) { return; } if (checked_correctness == PathChecking.correct_path) { break; } } if (!Directory.Exists(source_file_or_folder_path))//if it is not a directory but a file { byte[] source = File.ReadAllBytes(source_file_or_folder_path); string[] paths = SetPathsEncodingFile(source_file_or_folder_path, path_for_decoded_folder, directory_path_for_encoded); string path_for_decoded_file = paths[0]; string destination_dictionary_path = paths[1]; string destination_file_path = paths[2]; DecodingInfo info = new DecodingInfo(); info.PathForDecoded = path_for_decoded_file; ResetPartial((int)((source.Length / interval + 1) / 0.75), source_file_or_folder_path);//25 процентов времени занимает подсчет приоритетов, далее progress_bar performstep для каждых interval байтов + 1 если длина файла меньше interval info.NumberOfCharacters = (ulong)source.Length; EncodeBytes(source, destination_dictionary_path, destination_file_path, ref info); PartialPerformStep(controller.PartialProgressBar.Maximum - controller.PartialProgressBar.Value); EntirePerformStep(1); } else { string[] sub_dirs = Directory.GetDirectories(source_file_or_folder_path); string[] sub_files = Directory.GetFiles(source_file_or_folder_path); List <string> current_folder_content = new List <string>(); current_folder_content.AddRange(sub_dirs); current_folder_content.AddRange(sub_files); string[] paths = SetPathsEncodingDirectory(source_file_or_folder_path, path_for_decoded_folder, directory_path_for_encoded); string new_directory_path_for_encoded = paths[0]; string directory_path_for_decoded = paths[1]; foreach (var sub_path in current_folder_content) { Encode(sub_path, directory_path_for_decoded, new_directory_path_for_encoded); } } }
private byte[] DecodeBytes(byte[] source, DecodingInfo dictionary) { canc_token.ThrowIfCancellationRequested(); uint processed = 0; Code temp_code = new Code(); uint number_of_bits = 0; uint temp_code_value = 0; List <byte> destination = new List <byte>(); byte temp = 0; foreach (var @byte in source) { for (byte i = 0; i < 8; i++) { temp_code_value = temp_code_value * 2 + @byte.GetBit(i); number_of_bits++; if (number_of_bits < dictionary.MinCodeLength) { continue; } temp_code.Number = temp_code_value; temp_code.BitNumber = number_of_bits; if (dictionary.CodeDict.TryGetValue(temp_code, out temp)) { destination.Add(temp); temp_code_value = 0; number_of_bits = 0; processed++; } if (processed == dictionary.NumberOfCharacters) { return(destination.ToArray()); } if (canc_token.IsCancellationRequested) { canc_token.ThrowIfCancellationRequested(); } } if (processed % interval == 0) { PartialPerformStep(1); } } return(destination.ToArray()); }
public void Decode(string source_directory_path, string desination_folder_path) { canc_token.ThrowIfCancellationRequested(); string message = "The file " + source_directory_path + " was deleted or renamed.\n" + "Please, restore it or skip it's archiving\n"; while (true) { PathChecking checked_correctness = (PathChecking)controller.Invoke(controller.OfferToSkip, source_directory_path, message); //source_file_or_folder_path); if (checked_correctness == PathChecking.wants_to_skip) { return; } if (checked_correctness == PathChecking.correct_path) { break; } } string[] file_to_decode = null; try { file_to_decode = Directory.GetFiles(source_directory_path, "*.bin");//an array of files because it must contain encoded file and a hidden dictionary } catch { throw new Exception("Please,select an archive folder\n"); } while (true) { if (file_to_decode.Length >= 2) { int dictionary_index = 0; Regex dictionary_regex = new Regex(@"(dictionary.bin){1}$"); if (dictionary_regex.Match(file_to_decode[1]).Value != "") { dictionary_index = 1; } byte[] source = File.ReadAllBytes(file_to_decode[1 - dictionary_index]); ResetPartial(source.Length / interval + 1, file_to_decode[1 - dictionary_index]); DecodingInfo info = null; try { IFormatter formatter = new BinaryFormatter(); Stream DictStream = new FileStream(file_to_decode[dictionary_index], FileMode.Open, FileAccess.Read, FileShare.Read); info = (DecodingInfo)formatter.Deserialize(DictStream); DictStream.Close(); } catch { string message2 = "The folder " + source_directory_path + " has been modified and does not contain an archived file\nPlease,restore it or skip it's archiving\n"; PathChecking checked_correctness2 = (PathChecking)controller.Invoke(controller.OfferToSkip, source_directory_path, message); //source_file_or_folder_path); if (checked_correctness2 == PathChecking.wants_to_skip) { return; } if (checked_correctness2 == PathChecking.restored_the_file) { continue; } } byte[] destinationBytes = DecodeBytes(source, info); WriteAll(desination_folder_path + info.PathForDecoded, destinationBytes); PartialPerformStep(controller.PartialProgressBar.Maximum - controller.PartialProgressBar.Value); EntirePerformStep(1); return; } else { string[] subdirs = Directory.GetDirectories(source_directory_path); foreach (var subdir in subdirs) { Decode(subdir, desination_folder_path); } return; } } }
private void EncodeBytes(byte[] source, string dictionary_file_path, string destination_file_path, ref DecodingInfo info) { var priorities = CountPriorities(source); var binaryTree = new HuffmanBinaryTree(priorities); byte[] destinationBytes = FillInByteDestination(source, binaryTree); File.WriteAllBytes(destination_file_path, destinationBytes); info.CodeDict = binaryTree.CodeDict; info.MinCodeLength = binaryTree.MinCodeLength; IFormatter formatter = new BinaryFormatter(); Stream DictStream = new FileStream(dictionary_file_path, FileMode.Create, FileAccess.Write, FileShare.None); formatter.Serialize(DictStream, info); File.SetAttributes(dictionary_file_path, FileAttributes.Hidden); DictStream.Close(); }