1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Diagnostics ;
4
+ using System . IO ;
5
+ using System . Windows . Forms ;
6
+ using System . Xml . Serialization ;
7
+ using taskt . Core . IO ;
8
+ using taskt . UI . CustomControls ;
9
+ using taskt . UI . Forms ;
10
+
11
+ namespace taskt . Core . Automation . Commands
12
+ {
13
+ [ Serializable ]
14
+ [ Attributes . ClassAttributes . Group ( "File Operation Commands" ) ]
15
+ [ Attributes . ClassAttributes . Description ( "" ) ]
16
+ [ Attributes . ClassAttributes . UsesDescription ( "" ) ]
17
+ [ Attributes . ClassAttributes . ImplementationDescription ( "" ) ]
18
+ public class ExtractFileCommand : ScriptCommand
19
+ {
20
+ [ XmlAttribute ]
21
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please select file type" ) ]
22
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "zip" ) ]
23
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "7z" ) ]
24
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "xz" ) ]
25
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "bzip2" ) ]
26
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "tar" ) ]
27
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "wim" ) ]
28
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "iso" ) ]
29
+ [ Attributes . PropertyAttributes . InputSpecification ( "Select source file type" ) ]
30
+ [ Attributes . PropertyAttributes . SampleUsage ( "Select **Type file**" ) ]
31
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
32
+ public string v_FileType { get ; set ; }
33
+
34
+ [ XmlAttribute ]
35
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the file path or file URL to be extract" ) ]
36
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFileSelectionHelper ) ]
37
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the path to the applicable file or enter file URL." ) ]
38
+ [ Attributes . PropertyAttributes . SampleUsage ( @"C:\temp\myfile.zip , [vFilePath] or https://temp.com/myfile.zip" ) ]
39
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
40
+ public string v_FilePathOrigin { get ; set ; }
41
+
42
+ [ XmlAttribute ]
43
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please select source file" ) ]
44
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "File Path" ) ]
45
+ [ Attributes . PropertyAttributes . PropertyUISelectionOption ( "File URL" ) ]
46
+ [ Attributes . PropertyAttributes . InputSpecification ( "Select source path" ) ]
47
+ [ Attributes . PropertyAttributes . SampleUsage ( "Select **File Path**, **File URL**" ) ]
48
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
49
+ public string v_FileSourceType { get ; set ; }
50
+
51
+ [ XmlAttribute ]
52
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please indicate the file path to be send after extract" ) ]
53
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowFileSelectionHelper ) ]
54
+ [ Attributes . PropertyAttributes . InputSpecification ( "Enter or Select the path to the applicable file or enter file URL." ) ]
55
+ [ Attributes . PropertyAttributes . SampleUsage ( @"C:\temp\ or [vFilePath]" ) ]
56
+ [ Attributes . PropertyAttributes . Remarks ( "" ) ]
57
+ public string v_PathDestination { get ; set ; }
58
+
59
+ [ XmlAttribute ]
60
+ [ Attributes . PropertyAttributes . PropertyDescription ( "Please select the variable to receive the list of extract files" ) ]
61
+ [ Attributes . PropertyAttributes . PropertyUIHelper ( Attributes . PropertyAttributes . PropertyUIHelper . UIAdditionalHelperType . ShowVariableHelper ) ]
62
+ [ Attributes . PropertyAttributes . InputSpecification ( "Select or provide a variable from the variable list" ) ]
63
+ [ Attributes . PropertyAttributes . SampleUsage ( "**vSomeVariable**" ) ]
64
+ [ 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." ) ]
65
+ public string v_applyToVariableName { get ; set ; }
66
+
67
+ public ExtractFileCommand ( )
68
+ {
69
+ this . CommandName = "ExtractFileCommand" ;
70
+ this . SelectionName = "File Extraction" ;
71
+ this . CommandEnabled = true ;
72
+ this . CustomRendering = true ;
73
+ }
74
+
75
+ public override void RunCommand ( object sender )
76
+ {
77
+ // get type of file either from a physical file or from a URL
78
+ var vFileType = v_FileType . ConvertToUserVariable ( sender ) ;
79
+ //get variable path or URL to source file
80
+ var vSourceFilePathOrigin = v_FilePathOrigin . ConvertToUserVariable ( sender ) ;
81
+ // get source type of file either from a physical file or from a URL
82
+ var vSourceFileType = v_FileSourceType . ConvertToUserVariable ( sender ) ;
83
+ // get file path to destination files
84
+ var vFilePathDestination = v_PathDestination . ConvertToUserVariable ( sender ) ;
85
+ // get file path to destination files
86
+ var v_VariableName = v_applyToVariableName . ConvertToUserVariable ( sender ) ;
87
+
88
+ if ( vSourceFileType == "File URL" )
89
+ {
90
+ //create temp directory
91
+ var tempDir = Core . IO . Folders . GetFolder ( Folders . FolderType . TempFolder ) ;
92
+ var tempFile = System . IO . Path . Combine ( tempDir , $ "{ Guid . NewGuid ( ) } ." + vFileType ) ;
93
+
94
+ //check if directory does not exist then create directory
95
+ if ( ! System . IO . Directory . Exists ( tempDir ) )
96
+ {
97
+ System . IO . Directory . CreateDirectory ( tempDir ) ;
98
+ }
99
+
100
+ // Create webClient to download the file for extraction
101
+ var webclient = new System . Net . WebClient ( ) ;
102
+ var uri = new Uri ( vSourceFilePathOrigin ) ;
103
+ webclient . DownloadFile ( uri , tempFile ) ;
104
+
105
+ // check if file is downloaded successfully
106
+ if ( System . IO . File . Exists ( tempFile ) )
107
+ {
108
+ vSourceFilePathOrigin = tempFile ;
109
+ }
110
+
111
+ // Free not needed resources
112
+ uri = null ;
113
+ if ( webclient != null )
114
+ {
115
+ webclient . Dispose ( ) ;
116
+ webclient = null ;
117
+ }
118
+ }
119
+
120
+ // Check if file exists before proceeding
121
+ if ( ! System . IO . File . Exists ( vSourceFilePathOrigin ) )
122
+ {
123
+ throw new System . IO . FileNotFoundException ( "Could not find file: " + vSourceFilePathOrigin ) ;
124
+ }
125
+
126
+ // Get 7Z app
127
+ var zPath = System . IO . Path . Combine ( System . IO . Path . GetDirectoryName ( System . Windows . Forms . Application . ExecutablePath ) , "Resources" , "7z.exe" ) ;
128
+
129
+ // If the directory doesn't exist, create it.
130
+ if ( ! Directory . Exists ( vFilePathDestination ) )
131
+ Directory . CreateDirectory ( vFilePathDestination ) ;
132
+
133
+ var result = "" ;
134
+ System . Diagnostics . Process process = new System . Diagnostics . Process ( ) ;
135
+
136
+ try
137
+ {
138
+ var temp = Guid . NewGuid ( ) ;
139
+ //Extract in temp to get list files and directories and delete
140
+ ProcessStartInfo pro = new ProcessStartInfo ( ) ;
141
+ pro . WindowStyle = ProcessWindowStyle . Hidden ;
142
+ pro . UseShellExecute = false ;
143
+ pro . FileName = zPath ;
144
+ pro . RedirectStandardOutput = true ;
145
+ pro . Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + "/" + temp + " -aoa" ;
146
+ process . StartInfo = pro ;
147
+ process . Start ( ) ;
148
+ process . WaitForExit ( ) ;
149
+ string [ ] dirPaths = Directory . GetDirectories ( vFilePathDestination + "/" + temp , "*" , SearchOption . TopDirectoryOnly ) ;
150
+ string [ ] filePaths = Directory . GetFiles ( vFilePathDestination + "/" + temp , "*" , SearchOption . TopDirectoryOnly ) ;
151
+
152
+ foreach ( var item in dirPaths )
153
+ {
154
+ result = result + item + Environment . NewLine ;
155
+ }
156
+ foreach ( var item in filePaths )
157
+ {
158
+ result = result + item + Environment . NewLine ;
159
+ }
160
+ result = result . Replace ( "/" + temp , "" ) ;
161
+ Directory . Delete ( vFilePathDestination + "/" + temp , true ) ;
162
+
163
+ //Extract
164
+ pro . Arguments = "x " + vSourceFilePathOrigin + " -o" + vFilePathDestination + " -aoa" ;
165
+ process . StartInfo = pro ;
166
+ process . Start ( ) ;
167
+ process . WaitForExit ( ) ;
168
+
169
+
170
+ result . StoreInUserVariable ( sender , v_applyToVariableName ) ;
171
+ }
172
+ catch ( System . Exception Ex )
173
+ {
174
+ process . Kill ( ) ;
175
+ v_applyToVariableName = Ex . Message ;
176
+ }
177
+
178
+
179
+
180
+
181
+
182
+
183
+
184
+ }
185
+ public override List < Control > Render ( frmCommandEditor editor )
186
+ {
187
+ base . Render ( editor ) ;
188
+
189
+ //create standard group controls
190
+ RenderedControls . AddRange ( CommandControls . CreateDefaultDropdownGroupFor ( "v_FileType" , this , editor ) ) ;
191
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_FilePathOrigin" , this , editor ) ) ;
192
+ RenderedControls . AddRange ( CommandControls . CreateDefaultDropdownGroupFor ( "v_FileSourceType" , this , editor ) ) ;
193
+ RenderedControls . AddRange ( CommandControls . CreateDefaultInputGroupFor ( "v_PathDestination" , this , editor ) ) ;
194
+ RenderedControls . Add ( CommandControls . CreateDefaultLabelFor ( "v_applyToVariableName" , this ) ) ;
195
+ var VariableNameControl = CommandControls . CreateStandardComboboxFor ( "v_applyToVariableName" , this ) . AddVariableNames ( editor ) ;
196
+ RenderedControls . AddRange ( CommandControls . CreateUIHelpersFor ( "v_applyToVariableName" , this , new Control [ ] { VariableNameControl } , editor ) ) ;
197
+ RenderedControls . Add ( VariableNameControl ) ;
198
+
199
+ return RenderedControls ;
200
+
201
+ }
202
+
203
+ public override string GetDisplayValue ( )
204
+ {
205
+ return base . GetDisplayValue ( ) + " [Extract From '" + v_FilePathOrigin + " to " + v_PathDestination + "' and apply result to '" + v_applyToVariableName + "'" ;
206
+ }
207
+ }
208
+ }
0 commit comments