/// <summary> /// Creates a zip archive from a directory. /// </summary> /// <param name="sourceAml">The directory you want to be zipped.</param> /// <param name="destination">The directory you want to store the zip archive in.</param> /// <param name="resources">An array of paths to the resources to be part of the .amlx package.</param> /// <param name="overwriteFile">A flag which indicates if the file should be overwritten if it exists.</param> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="IOException"></exception> /// <exception cref="Exception"></exception> private static void Zip(string sourceAml, string destination, IEnumerable <string> resources, bool overwriteFile) { if (string.IsNullOrEmpty(sourceAml)) { throw new ArgumentNullException(sourceAml, "Source Aml was null or empty."); } if (string.IsNullOrEmpty(destination)) { throw new ArgumentNullException(destination, "Destination was null or empty."); } if (overwriteFile) { File.Delete(destination); } else if (File.Exists(destination)) { throw new IOException("Could not create the .amlx compressed file because the file should not be overwritten."); } try { using (var ac = new Aml.Engine.AmlObjects.AutomationMLContainer(destination)) { var root = ac.AddRoot(sourceAml, new Uri("/" + Path.GetFileName(sourceAml), UriKind.Relative)); List <string> fileNames = new List <string>(); foreach (var resource in resources) { var fileName = Path.GetFileName(resource); if (fileNames.Contains(fileName)) { continue; } fileNames.Add(fileName); if (string.IsNullOrEmpty(fileName)) { continue; } var fileTmpPath = Path.Combine(Path.GetTempPath(), Dd2AmlName, fileName); if (!File.Exists(fileTmpPath)) { continue; } var fileUri = new Uri("/" + fileName, UriKind.Relative); ac.AddAnyContent(root, fileTmpPath, fileUri); } ac.Close(); } } catch (IOException e) { throw new IOException("Could not create the .amlx package while using the AutomationML container.", e); } catch (Exception e) { throw new Exception("An exception occured while creating the .amlx package while using the AutomationML container.", e); } }
public static void Compress(string sourceAMLFile, string file_target, List <string> OtherFiles) { try { PrintHelper.printCentredLine("What should be the Name of the Compressed AMLX-File?\n"); string FileName = Path.Combine(file_target, Console.ReadLine()); PrintHelper.printCentredLine("\n"); while (File.Exists(@FileName)) { PrintHelper.printCentredLine("File already exists in Directory. Please Choose another name.\n"); FileName = Path.Combine(file_target, Console.ReadLine()); PrintHelper.printCentredLine("\n"); } // creates empty AMLX / ZIP in target directory // Create AML Container var container = new Aml.Engine.AmlObjects.AutomationMLContainer(FileName, FileMode.Create); // Add Root-Element of AMLX-File var Root = container.AddRoot(sourceAMLFile, new Uri("/" + Path.GetFileName(sourceAMLFile), UriKind.Relative)); //Add More Files if User wants more foreach (string NextFile in OtherFiles) { if (Path.GetExtension(NextFile).ToUpper() == ".XSD") { container.AddCAEXSchema(NextFile, new Uri("/" + Path.GetFileName(NextFile), UriKind.Relative)); } else { container.AddAnyContent(Root, NextFile, new Uri("/" + Path.GetFileName(NextFile), UriKind.Relative)); } } // Finally Close File container.Close(); PrintHelper.printCentredLine("Sucessfully Created AMLX-File \n \n"); } catch (Exception e) { Console.WriteLine("Exception: " + e.ToString()); PrintHelper.println("Could not Create AMLX-File", ConsoleColor.Red); } PrintHelper.Exit("Returning to Main Menu"); }
public static void DeCompress(string file_path_src, string folder_path_target) { try { var container = new Aml.Engine.AmlObjects.AutomationMLContainer(file_path_src); container.ExtractAllFiles(folder_path_target); PrintHelper.printCentredLine("\nSuccessfully Extracted Files"); } catch (IOException ex) { PrintHelper.println($"Error occured ( {ex})", ConsoleColor.Red); PrintHelper.println("\n Cannot find file..", ConsoleColor.Red); } catch (Exception e) { Console.WriteLine("Exception: " + e.ToString()); PrintHelper.println("\nCould not Extract Files", ConsoleColor.Red); } PrintHelper.Exit("\n\nReturning to Main Menu"); }