Skip to content

Commit

Permalink
add Extract File Command
Browse files Browse the repository at this point in the history
  • Loading branch information
Wladimir Teixeira Neto committed Jan 28, 2020
1 parent ced52b5 commit a9ba832
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 4 deletions.
5 changes: 2 additions & 3 deletions taskt.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26730.10
# Visual Studio Version 16
VisualStudioVersion = 16.0.29613.14
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "taskt", "taskt\taskt.csproj", "{C1BE3204-94D1-4A9A-AE30-C3E302383182}"
EndProject
Expand All @@ -24,7 +24,6 @@ Global
{C1BE3204-94D1-4A9A-AE30-C3E302383182}.Release|x64.ActiveCfg = Release|x64
{C1BE3204-94D1-4A9A-AE30-C3E302383182}.Release|x64.Build.0 = Release|x64
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|Any CPU.ActiveCfg = Release|Any CPU
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|Any CPU.Build.0 = Release|Any CPU
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|x64.ActiveCfg = Debug|x64
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Debug|x64.Build.0 = Debug|x64
{3FC72D0C-D083-44C4-96E9-13FE599F8E0E}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down
208 changes: 208 additions & 0 deletions taskt/Core/Automation/Commands/ExtractFileCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.Core.IO;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("File Operation Commands")]
[Attributes.ClassAttributes.Description("")]
[Attributes.ClassAttributes.UsesDescription("")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class ExtractFileCommand : ScriptCommand
{
[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select file type")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("zip")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("7z")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("xz")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("bzip2")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("tar")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("wim")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("iso")]
[Attributes.PropertyAttributes.InputSpecification("Select source file type")]
[Attributes.PropertyAttributes.SampleUsage("Select **Type file**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_FileType { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the file path or file URL to be extract")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\myfile.zip , [vFilePath] or https://temp.com/myfile.zip")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_FilePathOrigin { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select source file")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("File Path")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("File URL")]
[Attributes.PropertyAttributes.InputSpecification("Select source path")]
[Attributes.PropertyAttributes.SampleUsage("Select **File Path**, **File URL**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_FileSourceType { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please indicate the file path to be send after extract")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowFileSelectionHelper)]
[Attributes.PropertyAttributes.InputSpecification("Enter or Select the path to the applicable file or enter file URL.")]
[Attributes.PropertyAttributes.SampleUsage(@"C:\temp\ or [vFilePath]")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_PathDestination { get; set; }

[XmlAttribute]
[Attributes.PropertyAttributes.PropertyDescription("Please select the variable to receive the list of extract files")]
[Attributes.PropertyAttributes.PropertyUIHelper(Attributes.PropertyAttributes.PropertyUIHelper.UIAdditionalHelperType.ShowVariableHelper)]
[Attributes.PropertyAttributes.InputSpecification("Select or provide a variable from the variable list")]
[Attributes.PropertyAttributes.SampleUsage("**vSomeVariable**")]
[Attributes.PropertyAttributes.Remarks("If you have enabled the setting **Create Missing Variables at Runtime** then you are not required to pre-define your variables, however, it is highly recommended.")]
public string v_applyToVariableName { get; set; }

public ExtractFileCommand()
{
this.CommandName = "ExtractFileCommand";
this.SelectionName = "File Extraction";
this.CommandEnabled = true;
this.CustomRendering = true;
}

public override void RunCommand(object sender)
{
// get type of file either from a physical file or from a URL
var vFileType = v_FileType.ConvertToUserVariable(sender);
//get variable path or URL to source file
var vSourceFilePathOrigin = v_FilePathOrigin.ConvertToUserVariable(sender);
// get source type of file either from a physical file or from a URL
var vSourceFileType = v_FileSourceType.ConvertToUserVariable(sender);
// get file path to destination files
var vFilePathDestination = v_PathDestination.ConvertToUserVariable(sender);
// get file path to destination files
var v_VariableName = v_applyToVariableName.ConvertToUserVariable(sender);

if (vSourceFileType == "File URL")
{
//create temp directory
var tempDir = Core.IO.Folders.GetFolder(Folders.FolderType.TempFolder);
var tempFile = System.IO.Path.Combine(tempDir, $"{ Guid.NewGuid()}." + vFileType);

//check if directory does not exist then create directory
if (!System.IO.Directory.Exists(tempDir))
{
System.IO.Directory.CreateDirectory(tempDir);
}

// Create webClient to download the file for extraction
var webclient = new System.Net.WebClient();
var uri = new Uri(vSourceFilePathOrigin);
webclient.DownloadFile(uri, tempFile);

// check if file is downloaded successfully
if (System.IO.File.Exists(tempFile))
{
vSourceFilePathOrigin = tempFile;
}

// Free not needed resources
uri = null;
if (webclient != null)
{
webclient.Dispose();
webclient = null;
}
}

// Check if file exists before proceeding
if (!System.IO.File.Exists(vSourceFilePathOrigin))
{
throw new System.IO.FileNotFoundException("Could not find file: " + vSourceFilePathOrigin);
}

// Get 7Z app
var zPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), "Resources", "7z.exe");

// If the directory doesn't exist, create it.
if (!Directory.Exists(vFilePathDestination))
Directory.CreateDirectory(vFilePathDestination);

var result = "";
System.Diagnostics.Process process = new System.Diagnostics.Process();

try
{
var temp = Guid.NewGuid();
//Extract in temp to get list files and directories and delete
ProcessStartInfo pro = new ProcessStartInfo();
pro.WindowStyle = ProcessWindowStyle.Hidden;
pro.UseShellExecute = false;
pro.FileName = zPath;
pro.RedirectStandardOutput = true;
pro.Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + "/" + temp + " -aoa";
process.StartInfo = pro;
process.Start();
process.WaitForExit();
string[] dirPaths = Directory.GetDirectories(vFilePathDestination + "/" + temp, "*", SearchOption.TopDirectoryOnly);
string[] filePaths = Directory.GetFiles(vFilePathDestination + "/" + temp, "*", SearchOption.TopDirectoryOnly);

foreach (var item in dirPaths)
{
result = result + item + Environment.NewLine;
}
foreach (var item in filePaths)
{
result = result + item + Environment.NewLine;
}
result = result.Replace("/" + temp, "");
Directory.Delete(vFilePathDestination + "/" + temp, true);

//Extract
pro.Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + " -aoa";
process.StartInfo = pro;
process.Start();
process.WaitForExit();


result.StoreInUserVariable(sender, v_applyToVariableName);
}
catch (System.Exception Ex)
{
process.Kill();
v_applyToVariableName = Ex.Message;
}







}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);

//create standard group controls
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileType", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_FilePathOrigin", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_FileSourceType", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_PathDestination", this, editor));
RenderedControls.Add(CommandControls.CreateDefaultLabelFor("v_applyToVariableName", this));
var VariableNameControl = CommandControls.CreateStandardComboboxFor("v_applyToVariableName", this).AddVariableNames(editor);
RenderedControls.AddRange(CommandControls.CreateUIHelpersFor("v_applyToVariableName", this, new Control[] { VariableNameControl }, editor));
RenderedControls.Add(VariableNameControl);

return RenderedControls;

}

public override string GetDisplayValue()
{
return base.GetDisplayValue() + " [Extract From '" + v_FilePathOrigin + " to " + v_PathDestination + "' and apply result to '" + v_applyToVariableName + "'";
}
}
}
3 changes: 2 additions & 1 deletion taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(ParseJsonModelCommand))]
[XmlInclude(typeof(EncryptionCommand))]
[XmlInclude(typeof(MathCalculationCommand))]
[XmlInclude(typeof(SetEnginePreferenceCommand))]
[XmlInclude(typeof(SetEnginePreferenceCommand))]
[XmlInclude(typeof(ExtractFileCommand))]
public abstract class ScriptCommand
{
[XmlAttribute]
Expand Down
Binary file added taskt/Resources/7z.dll
Binary file not shown.
Binary file added taskt/Resources/7z.exe
Binary file not shown.
7 changes: 7 additions & 0 deletions taskt/taskt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@
<Compile Include="Core\Automation\Commands\AddDictionaryCommand.cs" />
<Compile Include="Core\Automation\Commands\CreateDictionaryCommand.cs" />
<Compile Include="Core\Automation\Commands\BeginMutliIfCommand.cs" />
<Compile Include="Core\Automation\Commands\ExtractFileCommand.cs" />
<Compile Include="Core\Automation\Commands\NextLoopCommand.cs" />
<Compile Include="Core\Automation\Commands\OutlookEmailCommand.cs" />
<Compile Include="Core\Automation\Commands\GetRegexMatchesCommand.cs" />
Expand Down Expand Up @@ -738,6 +739,12 @@
<None Include="Resources\command-remote.png" />
<None Include="Resources\command-begin-multi-if.png" />
<None Include="Resources\command-nextloop.png" />
<Content Include="Resources\7z.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Resources\7z.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="Resources\IEDriverServer.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down

0 comments on commit a9ba832

Please sign in to comment.