/// <summary> /// load and decompress files, and it merge them to single file. /// </summary> /// <param name="pathMessenger">object with stored paths</param> /// <returns>success of operation</returns> private bool decompress(PathMessenger pathMessenger) { List <string> compressedFiles = pathMessenger.getInputFilePaths(); if (compressedFiles.Count() > 0) { using (FileStream fsWrite = new FileStream(pathMessenger.outputPath, FileMode.CreateNew)) { Debug.WriteLine($"Found {compressedFiles.Count} compresed files. "); //TODO byte[] readedFile; foreach (string onefilePath in compressedFiles) { byte[] loadedFile = CompressWorker.simpleDecompress(onefilePath); fsWrite.Write(loadedFile, 0, loadedFile.Length); } } return(true); } else { ErrorMessage.Invoke(this, "Cannot found any compressed files. Please edit path to source file."); return(false); } }
/// <summary> /// decide if need to use multi/single thread for compress and save /// </summary> /// <param name="MainFilePart">Part of the file for compress and save</param> /// <returns></returns> public bool Save(List <byte> MainFilePart) { this.MainFilePart = MainFilePart; if (MainFilePart.Count < MaxFileSize) { CompressWorker.simpleCompress(MainFilePart.ToArray(), pathMessenger.getOutputFilePath(1)); Debug.WriteLine($"Compressed file saved: {pathMessenger.getOutputFilePath(1)}"); return(true); //TODO simpleCompress need return } else { return(ParalelSave()); } }
/// <summary> /// split <see cref="MainFilePart"/> to multiple small ones. /// then parallel compress and save them. /// </summary> /// <returns>success of operation</returns> private bool ParalelSave() { List <byte> currentListPart; int mainFilePartCount = MainFilePart.Count(); int currentStartIndex = 0; int currentrequiredSize = MaxFileSize; //calculate number of parst. That orginal list will be splited. int numberOfPart = (int)(Math.Ceiling((double)mainFilePartCount / (double)MaxFileSize)); Task <bool>[] compressTasks = new Task <bool> [numberOfPart]; for (int Count = 0; Count < numberOfPart; Count++) { CycleCount++; //start position for reading in list currentStartIndex = (Count * MaxFileSize); //requested size. currentrequiredSize = ((currentStartIndex + MaxFileSize) > mainFilePartCount) ? (mainFilePartCount - currentStartIndex) : MaxFileSize; //get part of the list currentListPart = MainFilePart.GetRange(currentStartIndex, currentrequiredSize); CompressWorker compressWorker = new CompressWorker(currentListPart.ToArray(), pathMessenger.getOutputFilePath(CycleCount)); compressTasks[Count] = Task <bool> .Factory.StartNew((item) => { return(compressWorker.compressTask().Result); }, compressWorker); } //check if all Task are succesfully finished if (compressTasks.Where(x => x.Result == false).ToArray().Count() > 0) { return(false); } else { Debug.WriteLine($"Compressed files saved. Files: {compressTasks.Length}"); return(true); } }
/// <summary> /// save and compres files in single thread /// </summary> /// <param name="pathMessenger">object with stored paths</param> /// <returns>success of operation</returns> private bool compressSingleThread(PathMessenger pathMessenger) { //start reading the file using (FileLoader fl = new FileLoader(pathMessenger.inputPath)) { List <byte> loadedFilePart; fl.MaxListSize = HardwareExplorer.GetSizeOfByteList(); fl.errorMessage = ErrorMessage; while (fl.canRead == 1) { bool successfulLoad = fl.LoadNextPatr(); if (successfulLoad) { //load part of the file loadedFilePart = fl.LoadedFilePart; try { //Save compress file CompressWorker.simpleCompress(loadedFilePart.ToArray(), pathMessenger.getOutputFilePath(fl.readCount)); } catch (Exception ex) { ErrorMessage?.Invoke(this, $"Error during compress file save. {pathMessenger.getOutputFilePath(fl.readCount)}"); return(false); } Debug.WriteLine($"Readed part: {fl.readCount}"); //TODO } else { ErrorMessage?.Invoke(this, $"Error during file read. Part {fl.readCount}"); return(false); } } } return(true); }