/** * Returns a byte stream representation of a Request starting from the task */ public byte[] generateRequestStream(FileTransfer.Task task) { RequestPacket request = new RequestPacket(task); byte[] requestStream = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(request) + "<EOF>"); return(requestStream); }
/** * Given a Task as parameter, creates a related Job in the receiving jobs list, * prepares a file coherent with what specified by the Task and returns a FileIterator * for this file. */ public FileIterator createJob(FileTransfer.Task task, string receivePath) { // Retrieves current date and computes a string to uniquely identify the task string zipTempFolder = Settings.Instance.AppDataPath + "\\temp"; Directory.CreateDirectory(Path.Combine(zipTempFolder, task.Id)); String path = Path.Combine(zipTempFolder, task.Id, task.Id + ".tmp"); ReceivingJob job = new ReceivingJob(task, receivePath); // Creates or retrieves the directory selected for the reception DirectoryInfo di = Directory.CreateDirectory(receivePath); // Creates the new file. Note: the file is zipped // This file is created in the temp directory File.Create(path).Close(); // Creates a new iterator to the file JobFileIterator iterator = (JobFileIterator)getIterator(path); iterator.Job = job; // Defines the behavior when the iterator is going to be closed // registering a callback on the related event. iterator.BeforeIteratorClosed += () => { // Job not completed: nothing to extract if (job.SentByte != job.Task.Size) { return; } List <String> createdFiles = new List <String>(); List <String> createdDirs = new List <String>(); string tempPath; try { // If there is only one file in the archive if (job.Task.Info.Count == 1 /* && job.Task.Info[0].Type == FileTransfer.FileInfo.FType.DIRECTORY*/) { //ZipFile.ExtractToDirectory(path, GetUniqueFilePath(receivePath + "\\" + job.Task.Info.Last().Name)); if (job.Task.Info.Last().Type == FileTransfer.FileInfo.FType.DIRECTORY) { tempPath = GetUniqueFilePath(receivePath + "\\" + job.Task.Info.Last().Name); createdDirs.Add(tempPath); ZipFile.ExtractToDirectory(path, tempPath); } else { using (ZipArchive archive = ZipFile.OpenRead(path)) { foreach (ZipArchiveEntry entry in archive.Entries) { tempPath = GetUniqueFilePath(Path.Combine(job.DestinationPath, entry.Name)); createdFiles.Add(tempPath); entry.ExtractToFile(tempPath); } } } } else // more than one file { using (ZipArchive archive = ZipFile.OpenRead(path)) { foreach (FileTransfer.FileInfo fileInfo in job.Task.Info) { foreach (ZipArchiveEntry entry in archive.Entries) { if (fileInfo.Name == entry.Name) { if (fileInfo.Type == FileTransfer.FileInfo.FType.DIRECTORY) { // This temporary unique path will be used as the path of the zip file that will be then unzipped again to obtain the final file string uniqueFileName = GetUniqueFilePath(Path.Combine(Path.GetDirectoryName(path), entry.Name)); try { entry.ExtractToFile(uniqueFileName + ".zip"); // extraxt entry to the temp zip archive // Now we perform the real extraction to unzip the directory in the selcted location tempPath = GetUniqueFilePath(Path.Combine(receivePath, fileInfo.Name)); createdDirs.Add(tempPath); ZipFile.ExtractToDirectory(uniqueFileName + ".zip", tempPath); } finally { // we need to delete the temporary file we created // this must be done even if an exception occours File.Delete(uniqueFileName + ".zip"); } } else { tempPath = GetUniqueFilePath(Path.Combine(job.DestinationPath, entry.Name)); createdFiles.Add(tempPath); entry.ExtractToFile(tempPath); } } } } } } } catch (Exception e) { // In the case any exception occours, we // must delete any file we have created // Delete all files foreach (String s in createdFiles) { if (File.Exists(s)) { File.Delete(s); } } // Delete all directories foreach (String d in createdDirs) { if (Directory.Exists(d)) { Directory.Delete(d, true); // delete recursive } } // Re-throw exception so that can be catched on upper levels throw e; } //JobsList.Receiving.remove(job.Id); }; return(iterator); }