-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualEngine.ZipArchive.ps1
700 lines (666 loc) · 33.2 KB
/
VirtualEngine.ZipArchive.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#region Public Functions
function Expand-ZipArchive {
<#
.SYNOPSIS
Extracts a Zip Archive.
.DESCRIPTION
Extracts the entire contents of a Zip Archive.
.PARAMETER Path
File path to the source Zip Archive (.zip) file to be extracted.
.PARAMETER LiteralPath
Literal file path to the source Zip Archive (.zip) to be extracted.
.PARAMETER DestinationPath
Destination directroy path to extract the Zip Archive (.zip) file contents to.
.PARAMETER Force
By default, the Expand-ZipArchive cmdlet will not overwrite an existing file
in the destination output directory. To overwrite existing files you must specify
the -Force parameter.
.EXAMPLE
Expand-ZipArchive -Path ~\Desktop\Example.zip -DestinationPath ~\Documents\Example\
This command extracts the contents of the 'Example.zip' file on the user's desktop into
the 'Example' directory in the user's Documents directory. Any existing files in the
'Example' directory will not be overwritten.
.EXAMPLE
Expand-ZipArchive -Path ~\Desktop\Example.zip -DestinationPath ~\Documents\Example\ -Force
This command extracts the contents of the 'Example.zip' file on the user's desktop into
the 'Example' directory in the user's Documents directory. Any existing files in the
'Example' directory will be overwritten without warning.
.OUTPUTS
A System.IO.FileInfo object for each extracted file.
#>
[CmdletBinding(DefaultParameterSetName='Path', HelpUri = 'https://github.com/VirtualEngine/Compression')]
[OutputType([System.IO.FileInfo])]
param (
# Source path to the Zip Archive.
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position =0 , ParameterSetName = 'Path')]
[ValidateNotNullOrEmpty()] [Alias('PSPath','FullName')] [System.String] $Path = (Get-Location -PSProvider FileSystem),
# Source path to the Zip Archive.
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'LiteralPath')]
[ValidateNotNullOrEmpty()] [System.String[]] $LiteralPath,
# Destination file path to extarct the Zip Archive item to.
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)]
[ValidateNotNullOrEmpty()] [System.String] $DestinationPath,
# Overwrite existing files
[Switch] $Force
)
begin {
## Validate destination path
if (-not(Test-Path -Path $DestinationPath -IsValid)) {
throw ('Invalid Zip Archive destination path ''{0}''.' -f $DestinationPath);
}
Write-Verbose ('Resolving destination path ''{0}''.' -f $DestinationPath);
$DestinationPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($DestinationPath);
if (-not (Test-Path -Path $DestinationPath -PathType Container)) {
Write-Verbose ('Creating destination path directory ''{0}''.' -f $DestinationPath);
[Ref] $null = New-Item -Path $DestinationPath -ItemType Directory;
}
if ($PSCmdlet.ParameterSetName -eq 'Path') {
Write-Verbose ('Resolving source path(s) ''{0}''.' -f $Path);
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path);
}
else {
## Set the path to the literal path specified
$Path = $LiteralPath;
}
## If all tests passed, load the required .NET assemblies
Write-Debug 'Loading ''System.IO.Compression'' .NET binaries.';
Add-Type -AssemblyName 'System.IO.Compression';
Add-Type -AssemblyName 'System.IO.Compression.FileSystem';
} # end begin
process {
foreach ($pathEntry in $Path) {
try {
Write-Verbose ('Expanding Zip Archive ''{0}''.' -f $pathEntry);
$zipArchive = [System.IO.Compression.ZipFile]::OpenRead($pathEntry);
Expand-ZipArchiveItem -InputObject ([Ref] $zipArchive.Entries) -DestinationPath $DestinationPath -Force:$Force;
} # end try
catch {
Write-Error $_.Exception;
}
finally {
## Close the file handle
if ($zipArchive -ne $null) { $zipArchive.Dispose(); }
}
} # end foreach
} # end process
end {
## Close the file handle (just in case!)
if ($zipArchive -ne $null) { $zipArchive.Dispose(); }
}
} # end function Expand-ZipArchive
function Expand-ZipArchiveItem {
<#
.SYNOPSIS
Extracts file(s) from a Zip Archive.
.DESCRIPTION
The Expand-ZipArchiveItem cmdlet extracts an individual file from a Zip Archive.
.PARAMETER Path
Internal ZipArchiveItem path inside the source Zip Archive (.zip) file to be extracted.
.PARAMETER DestinationPath
Destination directroy path to extract the Zip Archive (.zip) file contents to.
.PARAMETER Force
By default, the Expand-ZipArchive cmdlet will not overwrite an existing file
in the destination output directory. To overwrite existing files you must specify
the -Force parameter.
.EXAMPLE
$ZipContents = Get-ZipArchiveItem -Path ~\Desktop\Example.zip
$ZipContents[0] | Expand-ZipArchiveItem -DestinationPath ~\Documents\Example\
This command extracts the first item from the 'Example.zip' file located on the user's
desktop directory. The file will be extracted to the user's Documents\Example directory.
Any existing file in the destination direcotory will not be overwritten.
.EXAMPLE
Expand-ZipArchiveItem -Path "Example.txt" -DestinationPath ~\Documents\Example\
This command extracts the 'Example.txt' file from the Zip Archive to the user's \Documents\
Example directory. Any existing file in the destination direcotory will not be overwritten.
.EXAMPLE
Expand-ZipArchiveItem -Path "SubFolder\Example2.txt" -DestinationPath ~\Documents\Example\ -Force
This command extracts the 'Example.txt' file from the Zip Archive 'SubFolder' directory, to
the user's \Documents\Example directory. Any existing file in the destination directory will
be overwritten without warning.
.INPUTS
You can pipe System.IO.Compression.ZipArchiveEntry objects to the Expand-ZipArchiveItem
cmdlet that are produced by the Get-ZipArchiveItem cmdlet.
.OUTPUTS
A System.IO.FileInfo object for each extracted file.
#>
[CmdletBinding(DefaultParameterSetName='Path', HelpUri = 'https://github.com/VirtualEngine/Compression')]
[OutputType([System.IO.FileInfo])]
param (
# Source path to the Zip Archive.
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'Path')]
[ValidateNotNullOrEmpty()] [System.IO.Compression.ZipArchiveEntry[]] [Ref] $InputObject,
# Destination file path to extarct the Zip Archive item to.
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)]
[ValidateNotNullOrEmpty()] [System.String] $DestinationPath,
# Overwrite existing files
[Switch] $Force
)
begin {
## Load the required .NET assemblies, just in case
Write-Debug 'Loading ''System.IO.Compression'' .NET binaries.';
Add-Type -AssemblyName 'System.IO.Compression';
Add-Type -AssemblyName 'System.IO.Compression.FileSystem';
}
process {
try {
foreach ($zipArchiveEntry in $InputObject) {
if ($zipArchiveEntry.FullName.Contains('/')) {
## We need to create the directory path as the ExtractToFile extension
## method won't do this and will throw an exception
$pathSplit = $zipArchiveEntry.FullName.Split('/');
$relativeDirectoryPath = New-Object System.Text.StringBuilder;
## Generate the relative directory name
for ($pathSplitPart = 0; $pathSplitPart -lt ($pathSplit.Count -1); $pathSplitPart++) {
[Ref] $null = $relativeDirectoryPath.AppendFormat('{0}\', $pathSplit[$pathSplitPart]);
}
## Create the destination directory path, joining the relative directory name
$directoryPath = Join-Path $DestinationPath $relativeDirectoryPath.ToString().Trim('\');
[Ref] $null = _NewDirectory -Path $directoryPath;
$fullDestinationFilePath = Join-Path $directoryPath $zipArchiveEntry.Name;
} # end if
else {
## Just a file in the root so just use the $DestinationPath
$fullDestinationFilePath = Join-Path $DestinationPath $zipArchiveEntry.Name;
} # end else
if ([string]::IsNullOrEmpty($zipArchiveEntry.Name)) {
## This is a folder and we need to create the directory path as the
## ExtractToFile extension method won't do this and will throw an exception
$pathSplit = $zipArchiveEntry.FullName.Split('/');
$relativeDirectoryPath = New-Object System.Text.StringBuilder;
## Generate the relative directory name
for ($pathSplitPart = 0; $pathSplitPart -lt ($pathSplit.Count -1); $pathSplitPart++) {
[Ref] $null = $relativeDirectoryPath.AppendFormat('{0}\', $pathSplit[$pathSplitPart]);
}
## Create the destination directory path, joining the relative directory name
$directoryPath = Join-Path $DestinationPath $relativeDirectoryPath.ToString().Trim('\');
[Ref] $null = _NewDirectory -Path $directoryPath;
$fullDestinationFilePath = Join-Path $directoryPath $zipArchiveEntry.Name;
}
elseif (!$Force -and (Test-Path -Path $fullDestinationFilePath -PathType Leaf)) {
## Are we overwriting existing files (-Force)?
Write-Warning ('Target file ''{0}'' already exists.' -f $fullDestinationFilePath);
}
else {
## Just overwrite any existing file
Write-Verbose ('Extracting Zip Archive Entry ''{0}''.' -f $fullDestinationFilePath);
[System.IO.Compression.ZipFileExtensions]::ExtractToFile($zipArchiveEntry, $fullDestinationFilePath, $true);
## Return a FileInfo object to the pipline
Write-Output (Get-Item -Path $fullDestinationFilePath);
} # end if
} # end foreach zipArchiveEntry
} # end try
catch {
Write-Error $_.Exception;
}
} # end process
} # end function Expand-ZipArchiveItem
function Get-ZipArchiveItem {
<#
.SYNOPSIS
Gets the contents of a Zip Archive.
.DESCRIPTION
The Get-ZipArchiveItem cmdlet gets the file contents of a Zip Archive. The results
of the this cmdlet can be used with the Expand-ZipArchiveItem cmdlet to extract one
or files.
.PARAMETER Path
File path to the source Zip Archive (.zip) file to be enumerated.
.PARAMETER LiteralPath
Literal file path to the source Zip Archive (.zip) to be enumerated.
.EXAMPLE
Get-ZipArchiveItem -Path ~\Desktop\Example.zip
This commands returns the contents of the 'Example.zip' .zip file on the user's
desktop.
.OUTPUTS
A System.IO.Compression.ZipArchiveEntry object per Zip Archive item.
#>
[CmdletBinding(DefaultParameterSetName = 'Path', HelpUri = 'https://github.com/VirtualEngine/Compression')]
[OutputType([System.IO.Compression.ZipArchiveEntry])]
param (
# Source path/files to add to the .ZIP file
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'Path')]
[ValidateNotNullOrEmpty()] [Alias('PSPath','FullName')] [System.String] $Path = (Get-Location -PSProvider FileSystem),
# Source path/files to add to the .ZIP file
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'LiteralPath')]
[ValidateNotNullOrEmpty()] [System.String[]] $LiteralPath
)
begin {
if ($PSCmdlet.ParameterSetName -eq 'Path') {
Write-Verbose ('Resolving path(s) ''{0}''.' -f $Path);
$Path = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path);
$Path = Resolve-Path -Path $Path;
}
else {
## Set the path to the literal path specified
$Path = $LiteralPath;
}
## If all tests passed, load the required .NET assemblies
Write-Debug 'Loading ''System.IO.Compression'' .NET binaries.';
Add-Type -AssemblyName 'System.IO.Compression';
Add-Type -AssemblyName 'System.IO.Compression.FileSystem';
} # end begin
process {
foreach ($pathEntry in $Path) {
Write-Verbose ('Processing Zip Archive ''{0}''.' -f $pathEntry);
try {
$fileStream = New-Object System.IO.FileStream($pathEntry, [System.IO.FileMode]::Open);
$zipArchive = New-Object System.IO.Compression.ZipArchive($fileStream, [System.IO.Compression.ZipArchiveMode]::Read);
$zipArchive.Entries;
}
catch {
Write-Error $_.Exception;
}
finally {
## Clean up
if ($zipArchive -ne $null) { $zipArchive.Dispose(); }
if ($fileStream -ne $null) { $fileStream.Close(); }
}
} # end foreach
} # end process
} # end function Get-ZipArchiveItem
function Add-ZipArchiveItem {
<#
.SYNOPSIS
Adds file(s) to an existing Zip Archive.
.DESCRIPTION
The Add-ZipArchiveItem cmdlets adds one or more files to an existing Zip Archive.
.PARAMETER Path
File path to the source file to be added to the Zip Archive.
.PARAMETER LiteralPath
Absolute file path to the source file to be added to the Zip Archive.
.PARAMETER DestinationPath
Destination Zip Archive (.zip) file to add the files to.
.PARAMETER CompressionLevel
The compression algorithm to use. You must specify either 'Optimal', 'Fastest' or
'NoCompression'. By default, optimal compression is used.
.PARAMETER Force
By default, the Add-ZipArchiveItem cmdlet will not overwrite an existing file
in the Zip Archive. To overwrite existing files within the Zip Archive, you must
specify the -Force parameter.
.OUTPUTS
System.IO.FileInfo
#>
[CmdletBinding(DefaultParameterSetName = 'Path', HelpUri = 'https://github.com/VirtualEngine/Compression')]
[OutputType([System.IO.FileInfo])]
param (
# Source path/files to add to the .ZIP file
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'Path')]
[ValidateNotNullOrEmpty()] [Alias('PSPath','FullName')] [System.String[]] $Path = (Get-Location -PSProvider FileSystem),
# Source path/files to add to the .ZIP file
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'LiteralPath')]
[ValidateNotNullOrEmpty()] [System.String[]] $LiteralPath,
# Existing Zip Archive file path
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)]
[ValidateNotNullOrEmpty()] [System.String] $DestinationPath,
# Compression level
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateSet('Optimal', 'Fastest', 'NoCompression')] [System.String] $CompressionLevel = 'Optimal',
# Overwrite existing Zip Archive entries if present
[Parameter(ValueFromPipelineByPropertyName=$true)] [Switch] $Force
)
begin {
## Validate destination path
if (-not (Test-Path -Path $DestinationPath -IsValid)) {
throw ('Invalid Zip Archive destination path ''{0}''.' -f $DestinationPath);
}
Write-Verbose ('Resolving destination path ''{0}''.' -f $DestinationPath);
$DestinationPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($DestinationPath);
$DestinationPath = Resolve-Path -Path $DestinationPath;
$resolvedPaths = @();
if ($PSCmdlet.ParameterSetName -eq 'Path') {
foreach ($pathItem in $Path) {
Write-Verbose ('Resolving source path(s) ''{0}''.' -f $pathItem);
$pathItem = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($pathItem);
$resolvedPaths += Resolve-Path -Path $pathItem;
}
}
else {
## Set the path to the literal path specified
$Path = $LiteralPath;
}
## If all tests passed, load the required .NET assemblies
Write-Debug 'Loading ''System.IO.Compression'' .NET binaries.';
Add-Type -AssemblyName 'System.IO.Compression';
Add-Type -AssemblyName 'System.IO.Compression.FileSystem';
Write-Verbose ('Opening existing Zip Archive ''{0}''.' -f $DestinationPath);
[System.IO.FileStream] $fileStream = New-Object System.IO.FileStream($DestinationPath, [System.IO.FileMode]::OpenOrCreate);
[System.IO.Compression.ZipArchive] $zipArchive = New-Object System.IO.Compression.ZipArchive($fileStream, [System.IO.Compression.ZipArchiveMode]::Update);
} # end begin
process {
foreach ($path in $resolvedPaths) {
_ProcessZipArchivePath -Path $path -ZipArchive ([Ref] $zipArchive) -Force:$Force;
} # end foreach
} # end process
end {
_CloseZipArchive;
}
} #end function Add-ZipArchiveItem
function New-ZipArchive {
<#
.SYNOPSIS
Creates a new Zip Archive.
.DESCRIPTION
The New-ZipArchive cmdlet creates a new Zip Archive from files or
directory paths passed vai the $Path parameter.
.PARAMETER Path
File path to the source file to be added to the Zip Archive.
.PARAMETER LiteralPath
Absolute file path to the source file to be added to the Zip Archive.
.PARAMETER DestinationPath
Destination Zip Archive (.zip) file to add the files to.
.PARAMETER CompressionLevel
The compression algorithm to use. You must specify either 'Optimal', 'Fastest' or
'NoCompression'. By default, optimal compression is used.
.PARAMETER Force
By default, the Add-ZipArchiveItem cmdlet will not overwrite an existing file
in the Zip Archive. To overwrite existing files within the Zip Archive, you must
specify the -Force switch parameter.
.PARAMETER NoClobber
By default, the New-ZipArchive cmdlet will overwrite an exiting Zip Archive file,
if present. To avoid overwriting the Zip Archive file, you must specify the
-NoClobber switch parameter.
.EXAMPLE
New-ZipArchive -Path .\ExampleFolder -DestinationPath ~\Desktop\Example.zip
This command compresses the files and sub-folders of the .\ExampleFolder into a
Zip Archive called 'Example.zip' that is placed on the user's desktop.
.OUTPUTS
System.IO.FileInfo
#>
[CmdletBinding(DefaultParameterSetName='Path', HelpUri = 'https://github.com/VirtualEngine/Compression')]
[OutputType([System.IO.FileInfo])]
param (
# Source path/files to add to the .ZIP file
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'Path')]
[ValidateNotNullOrEmpty()] [Alias('PSPath','FullName')] [System.String[]] $Path = (Get-Location -PSProvider FileSystem),
# Source path/files to add to the .ZIP file
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0, ParameterSetName = 'LiteralPath')]
[ValidateNotNullOrEmpty()] [System.String[]] $LiteralPath,
# Zip file output name
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 1)]
[ValidateNotNullOrEmpty()] [System.String] $DestinationPath,
# Compression level
[Parameter(ValueFromPipelineByPropertyName = $true)]
[ValidateSet('Optimal', 'Fastest', 'NoCompression')] [System.String] $CompressionLevel = 'Optimal',
# Overwrite existing Zip Archive entries if present
[Parameter(ValueFromPipelineByPropertyName = $true)] [Switch] $Force,
# Do not create a new Zip Archive file if present
[Parameter(ValueFromPipelineByPropertyName = $true)] [Switch] $NoClobber
)
begin {
## Validate destination path
if (-not (Test-Path -Path $DestinationPath -IsValid)) {
throw ('Invalid Zip Archive destination path ''{0}''.' -f $DestinationPath);
}
Write-Verbose ('Resolving destination path ''{0}''.' -f $DestinationPath);
$DestinationPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($DestinationPath);
$resolvedPaths = @();
if ($PSCmdlet.ParameterSetName -eq 'Path') {
foreach ($pathItem in $Path) {
Write-Verbose ('Resolving source path(s) ''{0}''.' -f $pathItem);
$pathItem = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($pathItem);
$resolvedPaths += Resolve-Path -Path $pathItem;
}
}
else {
## Set the path to the literal path specified
$Path = $LiteralPath;
}
## If all tests passed, load the required .NET assemblies
Write-Debug 'Loading ''System.IO.Compression'' .NET binaries.';
Add-Type -AssemblyName 'System.IO.Compression';
Add-Type -AssemblyName 'System.IO.Compression.FileSystem';
if ($NoClobber) {
Write-Verbose ('Opening an existing or creating a new Zip Archive ''{0}''.' -f $DestinationPath);
[System.IO.FileStream] $fileStream = New-Object System.IO.FileStream($DestinationPath, [System.IO.FileMode]::OpenOrCreate);
}
else {
## (Re)create a new Zip Archive
Write-Verbose ('Creating new Zip Archive ''{0}''.' -f $DestinationPath);
[System.IO.FileStream] $fileStream = New-Object System.IO.FileStream($DestinationPath, [System.IO.FileMode]::Create);
}
[System.IO.Compression.ZipArchive] $zipArchive = New-Object System.IO.Compression.ZipArchive($fileStream, [System.IO.Compression.ZipArchiveMode]::Update);
} # end begin
process {
foreach ($path in $resolvedPaths) {
_ProcessZipArchivePath -Path $path -ZipArchive ([Ref] $zipArchive) -Force:$Force;
}
} # end process
end {
_CloseZipArchive;
## Return a System.IO.FileInfo to the pipeline
Get-Item -Path $DestinationPath;
} # end end
}
#endregion Public Functions
#region Private Functions
function _NewDirectory {
<#
.SYNOPSIS
Creates a filesystem directory.
.DESCRIPTION
The New-Directory cmdlet will create the target directory
if it doesn't already exist. If the target path already exists,
the cmdlet does nothing.
.EXAMPLE
New-Directory -Path ~\Desktop\Example
This example will create a folder in the user's desktop folder
if it does not already exist.
.INPUTS
You can pipe multiple strings or multiple System.IO.DirectoryInfo
objects to this cmdlet.
.OUTPUTS
System.IO.DirectoryInfo
.NOTES
This is an internal function and should not be called directly.
#>
[CmdletBinding(DefaultParameterSetName = 'ByString', SupportsShouldProcess = $true, ConfirmImpact = 'Medium')]
[OutputType([System.IO.DirectoryInfo])]
param (
# Target filesystem directory to create
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true,
Position = 0, ParameterSetName = 'ByDirectoryInfo')]
[ValidateNotNullOrEmpty()] [System.IO.DirectoryInfo[]] $InputObject,
# Target filesystem directory to create
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true,
Position = 0, ParameterSetName = 'ByString')] [Alias('PSPath')]
[ValidateNotNullOrEmpty()] [System.String[]] $Path
)
process {
Write-Debug ('Using parameter set ''{0}''.' -f $PSCmdlet.ParameterSetName);
switch ($PSCmdlet.ParameterSetName) {
'ByString' {
foreach ($Directory in $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($Path)) {
Write-Debug ('Testing target directory ''{0}''.' -f $Directory);
if (-not (Test-Path -Path $Directory -PathType Container)) {
if ($PSCmdlet.ShouldProcess($Directory, 'Create directory')) {
Write-Verbose ('Creating target directory ''{0}''.' -f $Directory);
New-Item -Path $Directory -ItemType Directory;
}
}
else {
Write-Debug ('Target directory ''{0}'' already exists.' -f $Directory);
Get-Item -Path $Directory;
}
} # end foreach
} # end ByString
'ByDirectoryInfo' {
foreach ($DirectoryInfo in $InputObject) {
Write-Debug ('Testing target directory ''{0}''.' -f $DirectoryInfo.FullName);
if (!($DirectoryInfo.Exists)) {
if ($PSCmdlet.ShouldProcess($DirectoryInfo.FullName, 'Create directory')) {
Write-Verbose ('Creating target directory ''{0}''.' -f $DirectoryInfo.FullName);
New-Item -Path $DirectoryInfo.FullName -ItemType Directory;
}
}
else {
Write-Debug ('Target directory ''{0}'' already exists.' -f $DirectoryInfo.FullName);
$DirectoryInfo;
}
} # end foreach
} #end ByDirectoryInfo
}
} # end process
} # end function _NewDirectory
function _CloseZipArchive {
<#
.SYNOPSIS
Tidies up and closes open Zip Archives and file handles
.NOTES
This is an internal function and should not be called directly.
#>
[CmdletBinding()]
param ()
process {
## Clean up
Write-Verbose ('Saving Zip Archive ''{0}''.' -f $DestinationPath);
if ($zipArchive -ne $null) {
$zipArchive.Dispose();
}
if ($fileStream -ne $null) {
$fileStream.Close();
}
} # end process
} # end function _CloseZipArchive
function _ProcessZipArchivePath {
<#
.SYNOPSIS
Adds the specified paths to a Zip Archive object Reference.
.NOTES
This is an internal function and should not be called directly.
#>
[CmdletBinding()]
param (
[Parameter()] [ValidateNotNullOrEmpty()] [System.String[]] $Path,
[Parameter()] [ValidateNotNull()] [System.IO.Compression.ZipArchive] [Ref] $ZipArchive,
[Switch] $Force
)
process {
foreach ($pathEntry in $Path) {
if (Test-Path -Path $pathEntry -PathType Container) {
## The base directory is used for internal References to directories within the Zip Archive
$BasePath = New-Object System.IO.DirectoryInfo($pathEntry);
[Ref] $null = _AddZipArchiveItem -Path $pathEntry -ZipArchive ([Ref] $zipArchive) -Force:$Force;
} # end if
else {
$fileInfo = New-Object System.IO.FileInfo($pathEntry);
if ((-not $Force) -and (_TestZipArchiveEntry -ZipArchive ([Ref] $zipArchive) -Name $fileInfo.Name)) {
Write-Warning ('Zip Archive entry ''{0}'' already exists.' -f $fileInfo.Name);
}
else {
Write-Verbose ('Adding Zip Archive entry ''{0}''.' -f $fileInfo.Name);
[Ref] $null = _TestZipArchiveEntry -ZipArchive ([Ref] $zipArchive) -Name $fileInfo.Name -Delete;
[Ref] $null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zipArchive, $fileInfo.FullName, $fileInfo.Name);
}
} # end else
} # end foreach
} # end process
} # end function _ProcessZipArchivePath
function _TestZipArchiveEntry {
<#
.SYNOPSIS
Tests whether a Zip Archive file contains the specified file.
.NOTES
This is an internal function and should not be called directly.
#>
[CmdletBinding()]
[OutputType([System.Boolean])]
param (
# Reference to the Zip Archive object
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNull()] [System.IO.Compression.ZipArchive] [Ref] $ZipArchive,
# Zip archive entry name, i.e. Subfolder\Filename.txt
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()] [System.String] $Name,
# Remove zip archive entry if present
[Switch] $Delete
)
process {
$ZipArchiveEntry = $ZipArchive.GetEntry($Name);
if ($zipArchiveEntry -eq $null) {
return $false;
}
else {
## Delete the entry if instructed
if ($Delete) {
Write-Debug ('Deleting existing Zip Archive entry ''{0}''.' -f $Name);
$ZipArchiveEntry.Delete();
}
return $true;
} # end else
} # end process
} # end function _TestZipArchiveEntry
function _RemoveZipArchiveEntry {
<#
.SYNOPSIS
Deletes a Zip Archive entry if it exists.
.NOTES
This is an internal function and should not be called directly.
#>
[CmdletBinding()]
[OutputType([System.Boolean])]
param (
# Reference to the Zip Archive object
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNull()] [System.IO.Compression.ZipArchive] [Ref] $ZipArchive,
# Zip archive entry name, i.e. Subfolder\Filename.txt
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()] [System.String] $Name
)
process {
_TestZipArchiveEntry -ZipArchive ([Ref] $ZipArchive) -Name $Name -Delete;
}
} # end _RemoveZipArchiveEntry
function _AddZipArchiveItem {
<#
.SYNOPSIS
Adds an item to an existing System.IO.Compression.ZipArchive.
.NOTES
This is an internal function and should not be called directly.
#>
[CmdletBinding()]
[OutputType([System.IO.Compression.ZipArchiveEntry])]
param (
# Directory path to add to the Zip Archive
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()] [System.String] $Path,
# Reference to the ZipArchive object
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNull()] [System.IO.Compression.ZipArchive] [Ref] $ZipArchive,
# Base directory path
[Parameter(ValueFromPipelineByPropertyName = $true)]
[AllowNull()] [System.String] $BasePath = '',
# Overwrite existing Zip Archive entries if present
[Switch] $Force
)
process {
Write-Debug ('Resolving directory path ''{0}''.' -f $Path);
foreach ($childItem in (Get-ChildItem -Path $Path)) {
if (Test-Path -Path $childItem.FullName -PathType Container) {
## Recurse subfolder, expanding the base directory, i.e. SubFolder1\SubFolder2
if ([string]::IsNullOrEmpty($BasePath)) {
$newBasePath = New-Object System.IO.DirectoryInfo($childItem).Name;
}
else {
$newBasePath = '{0}\{1}' -f $BasePath, (New-Object System.IO.DirectoryInfo($childItem)).Name;
}
} # end if
else {
## Add the file using the current base directory
if ([string]::IsNullOrEmpty($BasePath)) {
$childItemPath = $childItem;
}
else {
$childItemPath = '{0}\{1}' -f $BasePath, $childItem;
}
if (!$Force -and (_TestZipArchiveEntry -ZipArchive ([Ref] $zipArchive) -Name $childItemPath)) {
Write-Warning ('Zip Archive entry ''{0}'' already exists.' -f $childItemPath);
}
else {
Write-Verbose ('Adding Zip Archive entry ''{0}''.' -f $childItemPath);
[Ref] $null = _TestZipArchiveEntry -ZipArchive ([Ref] $zipArchive) -Name $childItemPath -Delete;
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zipArchive, $childItem.FullName, $childItemPath);
}
} # end else
} # end foreach
} # end process
} # end function _AddZipArchiveItem
#endregion Private Functions