-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMicrosoft.PowerShell.Archive.psm1
More file actions
1322 lines (1158 loc) · 57.9 KB
/
Microsoft.PowerShell.Archive.psm1
File metadata and controls
1322 lines (1158 loc) · 57.9 KB
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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
data LocalizedData
{
# culture="en-US"
ConvertFrom-StringData @'
PathNotFoundError=The path '{0}' either does not exist or is not a valid file system path.
ExpandArchiveInValidDestinationPath=The path '{0}' is not a valid file system directory path.
InvalidZipFileExtensionError={0} is not a supported archive file format. {1} is the only supported archive file format.
ArchiveFileIsReadOnly=The attributes of the archive file {0} is set to 'ReadOnly' hence it cannot be updated. If you intend to update the existing archive file, remove the 'ReadOnly' attribute on the archive file else use -Force parameter to override and create a new archive file.
ZipFileExistError=The archive file {0} already exists. Use the -Update parameter to update the existing archive file or use the -Force parameter to overwrite the existing archive file.
DuplicatePathFoundError=The input to {0} parameter contains a duplicate path '{1}'. Provide a unique set of paths as input to {2} parameter.
ArchiveFileIsEmpty=The archive file {0} is empty.
CompressProgressBarText=The archive file '{0}' creation is in progress...
ExpandProgressBarText=The archive file '{0}' expansion is in progress...
AppendArchiveFileExtensionMessage=The archive file path '{0}' supplied to the DestinationPath parameter does not include .zip extension. Hence .zip is appended to the supplied DestinationPath path and the archive file would be created at '{1}'.
AddItemtoArchiveFile=Adding '{0}'.
BadArchiveEntry=Can not process invalid archive entry '{0}'.
CreateFileAtExpandedPath=Created '{0}'.
InvalidArchiveFilePathError=The archive file path '{0}' specified as input to the {1} parameter is resolving to multiple file system paths. Provide a unique path to the {2} parameter where the archive file has to be created.
InvalidExpandedDirPathError=The directory path '{0}' specified as input to the DestinationPath parameter is resolving to multiple file system paths. Provide a unique path to the Destination parameter where the archive file contents have to be expanded.
FileExistsError=Failed to create file '{0}' while expanding the archive file '{1}' contents as the file '{2}' already exists. Use the -Force parameter if you want to overwrite the existing directory '{3}' contents when expanding the archive file.
DeleteArchiveFile=The partially created archive file '{0}' is deleted as it is not usable.
InvalidDestinationPath=The destination path '{0}' does not contain a valid archive file name.
PreparingToCompressVerboseMessage=Preparing to compress...
PreparingToExpandVerboseMessage=Preparing to expand...
ItemDoesNotAppearToBeAValidZipArchive=File '{0}' does not appear to be a valid zip archive.
'@
}
Import-LocalizedData LocalizedData -filename ArchiveResources -ErrorAction Ignore
$zipFileExtension = ".zip"
<############################################################################################
# The Compress-Archive cmdlet can be used to zip/compress one or more files/directories.
############################################################################################>
function Compress-Archive
{
[CmdletBinding(
DefaultParameterSetName="Path",
SupportsShouldProcess=$true,
HelpUri="https://go.microsoft.com/fwlink/?LinkID=393252")]
[OutputType([System.IO.File])]
param
(
[parameter (mandatory=$true, Position=0, ParameterSetName="Path", ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[parameter (mandatory=$true, Position=0, ParameterSetName="PathWithForce", ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[parameter (mandatory=$true, Position=0, ParameterSetName="PathWithUpdate", ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string[]] $Path,
[parameter (mandatory=$true, ParameterSetName="LiteralPath", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$true)]
[parameter (mandatory=$true, ParameterSetName="LiteralPathWithForce", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$true)]
[parameter (mandatory=$true, ParameterSetName="LiteralPathWithUpdate", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("PSPath")]
[string[]] $LiteralPath,
[parameter (mandatory=$true,
Position=1,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false)]
[ValidateNotNullOrEmpty()]
[string] $DestinationPath,
[parameter (
mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false)]
[ValidateSet("Optimal","NoCompression","Fastest")]
[string]
$CompressionLevel = "Optimal",
[parameter(mandatory=$true, ParameterSetName="PathWithUpdate", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$false)]
[parameter(mandatory=$true, ParameterSetName="LiteralPathWithUpdate", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$false)]
[switch]
$Update = $false,
[parameter(mandatory=$true, ParameterSetName="PathWithForce", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$false)]
[parameter(mandatory=$true, ParameterSetName="LiteralPathWithForce", ValueFromPipeline=$false, ValueFromPipelineByPropertyName=$false)]
[switch]
$Force = $false,
[switch]
$PassThru = $false
)
BEGIN
{
# Ensure the destination path is in a non-PS-specific format
$DestinationPath = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($DestinationPath)
$inputPaths = @()
$destinationParentDir = [system.IO.Path]::GetDirectoryName($DestinationPath)
if($null -eq $destinationParentDir)
{
$errorMessage = ($LocalizedData.InvalidDestinationPath -f $DestinationPath)
ThrowTerminatingErrorHelper "InvalidArchiveFilePath" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $DestinationPath
}
if($destinationParentDir -eq [string]::Empty)
{
$destinationParentDir = '.'
}
$archiveFileName = [system.IO.Path]::GetFileName($DestinationPath)
$destinationParentDir = GetResolvedPathHelper $destinationParentDir $false $PSCmdlet
if($destinationParentDir.Count -gt 1)
{
$errorMessage = ($LocalizedData.InvalidArchiveFilePathError -f $DestinationPath, "DestinationPath", "DestinationPath")
ThrowTerminatingErrorHelper "InvalidArchiveFilePath" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $DestinationPath
}
IsValidFileSystemPath $destinationParentDir | Out-Null
$DestinationPath = Join-Path -Path $destinationParentDir -ChildPath $archiveFileName
# GetExtension API does not validate for the actual existence of the path.
$extension = [system.IO.Path]::GetExtension($DestinationPath)
# If user does not specify an extension, we append the .zip extension automatically.
If($extension -eq [string]::Empty)
{
$DestinationPathWithOutExtension = $DestinationPath
$DestinationPath = $DestinationPathWithOutExtension + $zipFileExtension
$appendArchiveFileExtensionMessage = ($LocalizedData.AppendArchiveFileExtensionMessage -f $DestinationPathWithOutExtension, $DestinationPath)
Write-Verbose $appendArchiveFileExtensionMessage
}
$archiveFileExist = Test-Path -LiteralPath $DestinationPath -PathType Leaf
if($archiveFileExist -and ($Update -eq $false -and $Force -eq $false))
{
$errorMessage = ($LocalizedData.ZipFileExistError -f $DestinationPath)
ThrowTerminatingErrorHelper "ArchiveFileExists" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $DestinationPath
}
# If archive file already exists and if -Update is specified, then we check to see
# if we have write access permission to update the existing archive file.
if($archiveFileExist -and $Update -eq $true)
{
$item = Get-Item -Path $DestinationPath
if($item.Attributes.ToString().Contains("ReadOnly"))
{
$errorMessage = ($LocalizedData.ArchiveFileIsReadOnly -f $DestinationPath)
ThrowTerminatingErrorHelper "ArchiveFileIsReadOnly" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidOperation) $DestinationPath
}
}
$isWhatIf = $psboundparameters.ContainsKey("WhatIf")
if(!$isWhatIf)
{
$preparingToCompressVerboseMessage = ($LocalizedData.PreparingToCompressVerboseMessage)
Write-Verbose $preparingToCompressVerboseMessage
$progressBarStatus = ($LocalizedData.CompressProgressBarText -f $DestinationPath)
ProgressBarHelper "Compress-Archive" $progressBarStatus 0 100 100 1
}
}
PROCESS
{
if($PsCmdlet.ParameterSetName -eq "Path" -or
$PsCmdlet.ParameterSetName -eq "PathWithForce" -or
$PsCmdlet.ParameterSetName -eq "PathWithUpdate")
{
$inputPaths += $Path
}
if($PsCmdlet.ParameterSetName -eq "LiteralPath" -or
$PsCmdlet.ParameterSetName -eq "LiteralPathWithForce" -or
$PsCmdlet.ParameterSetName -eq "LiteralPathWithUpdate")
{
$inputPaths += $LiteralPath
}
}
END
{
# If archive file already exists and if -Force is specified, we delete the
# existing archive file and create a brand new one.
if(($PsCmdlet.ParameterSetName -eq "PathWithForce" -or
$PsCmdlet.ParameterSetName -eq "LiteralPathWithForce") -and $archiveFileExist)
{
Remove-Item -Path $DestinationPath -Force -ErrorAction Stop
}
# Validate Source Path depending on parameter set being used.
# The specified source path contains one or more files or directories that needs
# to be compressed.
$isLiteralPathUsed = $false
if($PsCmdlet.ParameterSetName -eq "LiteralPath" -or
$PsCmdlet.ParameterSetName -eq "LiteralPathWithForce" -or
$PsCmdlet.ParameterSetName -eq "LiteralPathWithUpdate")
{
$isLiteralPathUsed = $true
}
ValidateDuplicateFileSystemPath $PsCmdlet.ParameterSetName $inputPaths
$resolvedPaths = GetResolvedPathHelper $inputPaths $isLiteralPathUsed $PSCmdlet
IsValidFileSystemPath $resolvedPaths | Out-Null
$sourcePath = $resolvedPaths;
# CSVHelper: This is a helper function used to append comma after each path specified by
# the $sourcePath array. The comma separated paths are displayed in the -WhatIf message.
$sourcePathInCsvFormat = CSVHelper $sourcePath
if($pscmdlet.ShouldProcess($sourcePathInCsvFormat))
{
try
{
# StopProcessing is not available in Script cmdlets. However the pipeline execution
# is terminated when ever 'CTRL + C' is entered by user to terminate the cmdlet execution.
# The finally block is executed whenever pipeline is terminated.
# $isArchiveFileProcessingComplete variable is used to track if 'CTRL + C' is entered by the
# user.
$isArchiveFileProcessingComplete = $false
$numberOfItemsArchived = CompressArchiveHelper $sourcePath $DestinationPath $CompressionLevel $Update
$isArchiveFileProcessingComplete = $true
}
finally
{
# The $isArchiveFileProcessingComplete would be set to $false if user has typed 'CTRL + C' to
# terminate the cmdlet execution or if an unhandled exception is thrown.
# $numberOfItemsArchived contains the count of number of files or directories add to the archive file.
# If the newly created archive file is empty then we delete it as it's not usable.
if(($isArchiveFileProcessingComplete -eq $false) -or
($numberOfItemsArchived -eq 0))
{
$DeleteArchiveFileMessage = ($LocalizedData.DeleteArchiveFile -f $DestinationPath)
Write-Verbose $DeleteArchiveFileMessage
# delete the partial archive file created.
if (Test-Path $DestinationPath) {
Remove-Item -LiteralPath $DestinationPath -Force -Recurse -ErrorAction SilentlyContinue
}
}
elseif ($PassThru)
{
Get-Item -LiteralPath $DestinationPath
}
}
}
}
}
<############################################################################################
# The Expand-Archive cmdlet can be used to expand/extract an zip file.
############################################################################################>
function Expand-Archive
{
[CmdletBinding(
DefaultParameterSetName="Path",
SupportsShouldProcess=$true,
HelpUri="https://go.microsoft.com/fwlink/?LinkID=393253")]
[OutputType([System.IO.FileSystemInfo])]
param
(
[parameter (
mandatory=$true,
Position=0,
ParameterSetName="Path",
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string] $Path,
[parameter (
mandatory=$true,
ParameterSetName="LiteralPath",
ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[Alias("PSPath")]
[string] $LiteralPath,
[parameter (mandatory=$false,
Position=1,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false)]
[ValidateNotNullOrEmpty()]
[string] $DestinationPath,
[parameter (mandatory=$false,
ValueFromPipeline=$false,
ValueFromPipelineByPropertyName=$false)]
[switch] $Force,
[switch]
$PassThru = $false
)
BEGIN
{
$isVerbose = $psboundparameters.ContainsKey("Verbose")
$isConfirm = $psboundparameters.ContainsKey("Confirm")
$isDestinationPathProvided = $true
if($DestinationPath -eq [string]::Empty)
{
$resolvedDestinationPath = (Get-Location).ProviderPath
$isDestinationPathProvided = $false
}
else
{
$destinationPathExists = Test-Path -Path $DestinationPath -PathType Container
if($destinationPathExists)
{
$resolvedDestinationPath = GetResolvedPathHelper $DestinationPath $false $PSCmdlet
if($resolvedDestinationPath.Count -gt 1)
{
$errorMessage = ($LocalizedData.InvalidExpandedDirPathError -f $DestinationPath)
ThrowTerminatingErrorHelper "InvalidDestinationPath" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $DestinationPath
}
# At this point we are sure that the provided path resolves to a valid single path.
# Calling Resolve-Path again to get the underlying provider name.
$suppliedDestinationPath = Resolve-Path -Path $DestinationPath
if($suppliedDestinationPath.Provider.Name-ne "FileSystem")
{
$errorMessage = ($LocalizedData.ExpandArchiveInValidDestinationPath -f $DestinationPath)
ThrowTerminatingErrorHelper "InvalidDirectoryPath" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $DestinationPath
}
}
else
{
$createdItem = New-Item -Path $DestinationPath -ItemType Directory -Confirm:$isConfirm -Verbose:$isVerbose -ErrorAction Stop
if($createdItem -ne $null -and $createdItem.PSProvider.Name -ne "FileSystem")
{
Remove-Item "$DestinationPath" -Force -Recurse -ErrorAction SilentlyContinue
$errorMessage = ($LocalizedData.ExpandArchiveInValidDestinationPath -f $DestinationPath)
ThrowTerminatingErrorHelper "InvalidDirectoryPath" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $DestinationPath
}
$resolvedDestinationPath = GetResolvedPathHelper $DestinationPath $true $PSCmdlet
}
}
$isWhatIf = $psboundparameters.ContainsKey("WhatIf")
if(!$isWhatIf)
{
$preparingToExpandVerboseMessage = ($LocalizedData.PreparingToExpandVerboseMessage)
Write-Verbose $preparingToExpandVerboseMessage
$progressBarStatus = ($LocalizedData.ExpandProgressBarText -f $DestinationPath)
ProgressBarHelper "Expand-Archive" $progressBarStatus 0 100 100 1
}
}
PROCESS
{
switch($PsCmdlet.ParameterSetName)
{
"Path"
{
$resolvedSourcePaths = GetResolvedPathHelper $Path $false $PSCmdlet
if($resolvedSourcePaths.Count -gt 1)
{
$errorMessage = ($LocalizedData.InvalidArchiveFilePathError -f $Path, $PsCmdlet.ParameterSetName, $PsCmdlet.ParameterSetName)
ThrowTerminatingErrorHelper "InvalidArchiveFilePath" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $Path
}
}
"LiteralPath"
{
$resolvedSourcePaths = GetResolvedPathHelper $LiteralPath $true $PSCmdlet
if($resolvedSourcePaths.Count -gt 1)
{
$errorMessage = ($LocalizedData.InvalidArchiveFilePathError -f $LiteralPath, $PsCmdlet.ParameterSetName, $PsCmdlet.ParameterSetName)
ThrowTerminatingErrorHelper "InvalidArchiveFilePath" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $LiteralPath
}
}
}
ValidateArchivePathHelper $resolvedSourcePaths
if($pscmdlet.ShouldProcess($resolvedSourcePaths))
{
$expandedItems = @()
try
{
# StopProcessing is not available in Script cmdlets. However the pipeline execution
# is terminated when ever 'CTRL + C' is entered by user to terminate the cmdlet execution.
# The finally block is executed whenever pipeline is terminated.
# $isArchiveFileProcessingComplete variable is used to track if 'CTRL + C' is entered by the
# user.
$isArchiveFileProcessingComplete = $false
# The User has not provided a destination path, hence we use '$pwd\ArchiveFileName' as the directory where the
# archive file contents would be expanded. If the path '$pwd\ArchiveFileName' already exists then we use the
# Windows default mechanism of appending a counter value at the end of the directory name where the contents
# would be expanded.
if(!$isDestinationPathProvided)
{
$archiveFile = New-Object System.IO.FileInfo $resolvedSourcePaths
$resolvedDestinationPath = Join-Path -Path $resolvedDestinationPath -ChildPath $archiveFile.BaseName
$destinationPathExists = Test-Path -LiteralPath $resolvedDestinationPath -PathType Container
if(!$destinationPathExists)
{
New-Item -Path $resolvedDestinationPath -ItemType Directory -Confirm:$isConfirm -Verbose:$isVerbose -ErrorAction Stop | Out-Null
}
}
ExpandArchiveHelper $resolvedSourcePaths $resolvedDestinationPath ([ref]$expandedItems) $Force $isVerbose $isConfirm
$isArchiveFileProcessingComplete = $true
}
finally
{
# The $isArchiveFileProcessingComplete would be set to $false if user has typed 'CTRL + C' to
# terminate the cmdlet execution or if an unhandled exception is thrown.
if($isArchiveFileProcessingComplete -eq $false)
{
if($expandedItems.Count -gt 0)
{
# delete the expanded file/directory as the archive
# file was not completely expanded.
$expandedItems | % { Remove-Item "$_" -Force -Recurse }
}
}
elseif ($PassThru -and $expandedItems.Count -gt 0)
{
# Return the expanded items, being careful to remove trailing directory separators from
# any folder paths for consistency
$trailingDirSeparators = '\' + [System.IO.Path]::DirectorySeparatorChar + '+$'
Get-Item -LiteralPath ($expandedItems -replace $trailingDirSeparators)
}
}
}
}
}
<############################################################################################
# GetResolvedPathHelper: This is a helper function used to resolve the user specified Path.
# The path can either be absolute or relative path.
############################################################################################>
function GetResolvedPathHelper
{
param
(
[string[]] $path,
[boolean] $isLiteralPath,
[System.Management.Automation.PSCmdlet]
$callerPSCmdlet
)
$resolvedPaths =@()
# null and empty check are are already done on Path parameter at the cmdlet layer.
foreach($currentPath in $path)
{
try
{
if($isLiteralPath)
{
$currentResolvedPaths = Resolve-Path -LiteralPath $currentPath -ErrorAction Stop
}
else
{
$currentResolvedPaths = Resolve-Path -Path $currentPath -ErrorAction Stop
}
}
catch
{
$errorMessage = ($LocalizedData.PathNotFoundError -f $currentPath)
$exception = New-Object System.InvalidOperationException $errorMessage, $_.Exception
$errorRecord = CreateErrorRecordHelper "ArchiveCmdletPathNotFound" $null ([System.Management.Automation.ErrorCategory]::InvalidArgument) $exception $currentPath
$callerPSCmdlet.ThrowTerminatingError($errorRecord)
}
foreach($currentResolvedPath in $currentResolvedPaths)
{
$resolvedPaths += $currentResolvedPath.ProviderPath
}
}
$resolvedPaths
}
function Add-CompressionAssemblies {
Add-Type -AssemblyName System.IO.Compression
if ($psedition -eq "Core")
{
Add-Type -AssemblyName System.IO.Compression.ZipFile
}
else
{
Add-Type -AssemblyName System.IO.Compression.FileSystem
}
}
function IsValidFileSystemPath
{
param
(
[string[]] $path
)
$result = $true;
# null and empty check are are already done on Path parameter at the cmdlet layer.
foreach($currentPath in $path)
{
if(!([System.IO.File]::Exists($currentPath) -or [System.IO.Directory]::Exists($currentPath)))
{
$errorMessage = ($LocalizedData.PathNotFoundError -f $currentPath)
ThrowTerminatingErrorHelper "PathNotFound" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $currentPath
}
}
return $result;
}
function ValidateDuplicateFileSystemPath
{
param
(
[string] $inputParameter,
[string[]] $path
)
$uniqueInputPaths = @()
# null and empty check are are already done on Path parameter at the cmdlet layer.
foreach($currentPath in $path)
{
$currentInputPath = $currentPath.ToUpper()
if($uniqueInputPaths.Contains($currentInputPath))
{
$errorMessage = ($LocalizedData.DuplicatePathFoundError -f $inputParameter, $currentPath, $inputParameter)
ThrowTerminatingErrorHelper "DuplicatePathFound" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $currentPath
}
else
{
$uniqueInputPaths += $currentInputPath
}
}
}
function CompressionLevelMapper
{
param
(
[string] $compressionLevel
)
$compressionLevelFormat = [System.IO.Compression.CompressionLevel]::Optimal
# CompressionLevel format is already validated at the cmdlet layer.
switch($compressionLevel.ToString())
{
"Fastest"
{
$compressionLevelFormat = [System.IO.Compression.CompressionLevel]::Fastest
}
"NoCompression"
{
$compressionLevelFormat = [System.IO.Compression.CompressionLevel]::NoCompression
}
}
return $compressionLevelFormat
}
function CompressArchiveHelper
{
param
(
[string[]] $sourcePath,
[string] $destinationPath,
[string] $compressionLevel,
[bool] $isUpdateMode
)
$numberOfItemsArchived = 0
$sourceFilePaths = @()
$sourceDirPaths = @()
foreach($currentPath in $sourcePath)
{
$result = Test-Path -LiteralPath $currentPath -Type Leaf
if($result -eq $true)
{
$sourceFilePaths += $currentPath
}
else
{
$sourceDirPaths += $currentPath
}
}
# The Source Path contains one or more directory (this directory can have files under it) and no files to be compressed.
if($sourceFilePaths.Count -eq 0 -and $sourceDirPaths.Count -gt 0)
{
$currentSegmentWeight = 100/[double]$sourceDirPaths.Count
$previousSegmentWeight = 0
foreach($currentSourceDirPath in $sourceDirPaths)
{
$count = CompressSingleDirHelper $currentSourceDirPath $destinationPath $compressionLevel $true $isUpdateMode $previousSegmentWeight $currentSegmentWeight
$numberOfItemsArchived += $count
$previousSegmentWeight += $currentSegmentWeight
}
}
# The Source Path contains only files to be compressed.
elseIf($sourceFilePaths.Count -gt 0 -and $sourceDirPaths.Count -eq 0)
{
# $previousSegmentWeight is equal to 0 as there are no prior segments.
# $currentSegmentWeight is set to 100 as all files have equal weightage.
$previousSegmentWeight = 0
$currentSegmentWeight = 100
$numberOfItemsArchived = CompressFilesHelper $sourceFilePaths $destinationPath $compressionLevel $isUpdateMode $previousSegmentWeight $currentSegmentWeight
}
# The Source Path contains one or more files and one or more directories (this directory can have files under it) to be compressed.
elseif($sourceFilePaths.Count -gt 0 -and $sourceDirPaths.Count -gt 0)
{
# each directory is considered as an individual segments & all the individual files are clubed in to a separate segment.
$currentSegmentWeight = 100/[double]($sourceDirPaths.Count +1)
$previousSegmentWeight = 0
foreach($currentSourceDirPath in $sourceDirPaths)
{
$count = CompressSingleDirHelper $currentSourceDirPath $destinationPath $compressionLevel $true $isUpdateMode $previousSegmentWeight $currentSegmentWeight
$numberOfItemsArchived += $count
$previousSegmentWeight += $currentSegmentWeight
}
$count = CompressFilesHelper $sourceFilePaths $destinationPath $compressionLevel $isUpdateMode $previousSegmentWeight $currentSegmentWeight
$numberOfItemsArchived += $count
}
return $numberOfItemsArchived
}
function CompressFilesHelper
{
param
(
[string[]] $sourceFilePaths,
[string] $destinationPath,
[string] $compressionLevel,
[bool] $isUpdateMode,
[double] $previousSegmentWeight,
[double] $currentSegmentWeight
)
$numberOfItemsArchived = ZipArchiveHelper $sourceFilePaths $destinationPath $compressionLevel $isUpdateMode $null $previousSegmentWeight $currentSegmentWeight
return $numberOfItemsArchived
}
function CompressSingleDirHelper
{
param
(
[string] $sourceDirPath,
[string] $destinationPath,
[string] $compressionLevel,
[bool] $useParentDirAsRoot,
[bool] $isUpdateMode,
[double] $previousSegmentWeight,
[double] $currentSegmentWeight
)
[System.Collections.Generic.List[System.String]]$subDirFiles = @()
if($useParentDirAsRoot)
{
$sourceDirInfo = New-Object -TypeName System.IO.DirectoryInfo -ArgumentList $sourceDirPath
$sourceDirFullName = $sourceDirInfo.Parent.FullName
# If the directory is present at the drive level the DirectoryInfo.Parent include directory separator. example: C:\
# On the other hand if the directory exists at a deper level then DirectoryInfo.Parent
# has just the path (without an ending directory separator). example C:\source
if($sourceDirFullName.Length -eq 3)
{
$modifiedSourceDirFullName = $sourceDirFullName
}
else
{
$modifiedSourceDirFullName = $sourceDirFullName + [System.IO.Path]::DirectorySeparatorChar
}
}
else
{
$sourceDirFullName = $sourceDirPath
$modifiedSourceDirFullName = $sourceDirFullName + [System.IO.Path]::DirectorySeparatorChar
}
$dirContents = Get-ChildItem -LiteralPath $sourceDirPath -Recurse
foreach($currentContent in $dirContents)
{
$isContainer = $currentContent -is [System.IO.DirectoryInfo]
if(!$isContainer)
{
$subDirFiles.Add($currentContent.FullName)
}
else
{
# The currentContent points to a directory.
# We need to check if the directory is an empty directory, if so such a
# directory has to be explicitly added to the archive file.
# if there are no files in the directory the GetFiles() API returns an empty array.
$files = $currentContent.GetFiles()
if($files.Count -eq 0)
{
$subDirFiles.Add($currentContent.FullName + [System.IO.Path]::DirectorySeparatorChar)
}
}
}
$numberOfItemsArchived = ZipArchiveHelper $subDirFiles.ToArray() $destinationPath $compressionLevel $isUpdateMode $modifiedSourceDirFullName $previousSegmentWeight $currentSegmentWeight
return $numberOfItemsArchived
}
function ZipArchiveHelper
{
param
(
[System.Collections.Generic.List[System.String]] $sourcePaths,
[string] $destinationPath,
[string] $compressionLevel,
[bool] $isUpdateMode,
[string] $modifiedSourceDirFullName,
[double] $previousSegmentWeight,
[double] $currentSegmentWeight
)
$numberOfItemsArchived = 0
$fileMode = [System.IO.FileMode]::Create
$result = Test-Path -LiteralPath $DestinationPath -Type Leaf
if($result -eq $true)
{
$fileMode = [System.IO.FileMode]::Open
}
Add-CompressionAssemblies
try
{
# At this point we are sure that the archive file has write access.
$archiveFileStreamArgs = @($destinationPath, $fileMode)
$archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs
$zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Update, $false)
$zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs
$currentEntryCount = 0
$progressBarStatus = ($LocalizedData.CompressProgressBarText -f $destinationPath)
$bufferSize = 4kb
$buffer = New-Object Byte[] $bufferSize
foreach($currentFilePath in $sourcePaths)
{
if($modifiedSourceDirFullName -ne $null -and $modifiedSourceDirFullName.Length -gt 0)
{
$index = $currentFilePath.IndexOf($modifiedSourceDirFullName, [System.StringComparison]::OrdinalIgnoreCase)
$currentFilePathSubString = $currentFilePath.Substring($index, $modifiedSourceDirFullName.Length)
$relativeFilePath = $currentFilePath.Replace($currentFilePathSubString, "").Trim()
}
else
{
$relativeFilePath = [System.IO.Path]::GetFileName($currentFilePath)
}
# Update mode is selected.
# Check to see if archive file already contains one or more zip files in it.
if($isUpdateMode -eq $true -and $zipArchive.Entries.Count -gt 0)
{
$entryToBeUpdated = $null
# Check if the file already exists in the archive file.
# If so replace it with new file from the input source.
# If the file does not exist in the archive file then default to
# create mode and create the entry in the archive file.
foreach($currentArchiveEntry in $zipArchive.Entries)
{
if(ArchivePathCompareHelper $currentArchiveEntry.FullName $relativeFilePath)
{
$entryToBeUpdated = $currentArchiveEntry
break
}
}
if($entryToBeUpdated -ne $null)
{
$addItemtoArchiveFileMessage = ($LocalizedData.AddItemtoArchiveFile -f $currentFilePath)
$entryToBeUpdated.Delete()
}
}
$compression = CompressionLevelMapper $compressionLevel
# If a directory needs to be added to an archive file,
# by convention the .Net API's expect the path of the directory
# to end with directory separator to detect the path as an directory.
if(!$relativeFilePath.EndsWith([System.IO.Path]::DirectorySeparatorChar, [StringComparison]::OrdinalIgnoreCase))
{
try
{
try
{
$currentFileStream = [System.IO.File]::Open($currentFilePath, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
}
catch
{
# Failed to access the file. Write a non terminating error to the pipeline
# and move on with the remaining files.
$exception = $_.Exception
if($null -ne $_.Exception -and
$null -ne $_.Exception.InnerException)
{
$exception = $_.Exception.InnerException
}
$errorRecord = CreateErrorRecordHelper "CompressArchiveUnauthorizedAccessError" $null ([System.Management.Automation.ErrorCategory]::PermissionDenied) $exception $currentFilePath
Write-Error -ErrorRecord $errorRecord
}
if($null -ne $currentFileStream)
{
$srcStream = New-Object System.IO.BinaryReader $currentFileStream
$entryPath = DirectorySeparatorNormalizeHelper $relativeFilePath
$currentArchiveEntry = $zipArchive.CreateEntry($entryPath, $compression)
# Updating the File Creation time so that the same timestamp would be retained after expanding the compressed file.
# At this point we are sure that Get-ChildItem would succeed.
$lastWriteTime = (Get-Item -LiteralPath $currentFilePath).LastWriteTime
if ($lastWriteTime.Year -lt 1980)
{
Write-Warning "'$currentFilePath' has LastWriteTime earlier than 1980. Compress-Archive will store any files with LastWriteTime values earlier than 1980 as 1/1/1980 00:00."
$lastWriteTime = [DateTime]::Parse('1980-01-01T00:00:00')
}
$currentArchiveEntry.LastWriteTime = $lastWriteTime
$destStream = New-Object System.IO.BinaryWriter $currentArchiveEntry.Open()
while($numberOfBytesRead = $srcStream.Read($buffer, 0, $bufferSize))
{
$destStream.Write($buffer, 0, $numberOfBytesRead)
$destStream.Flush()
}
$numberOfItemsArchived += 1
$addItemtoArchiveFileMessage = ($LocalizedData.AddItemtoArchiveFile -f $currentFilePath)
}
}
finally
{
If($null -ne $currentFileStream)
{
$currentFileStream.Dispose()
}
If($null -ne $srcStream)
{
$srcStream.Dispose()
}
If($null -ne $destStream)
{
$destStream.Dispose()
}
}
}
else
{
$entryPath = DirectorySeparatorNormalizeHelper $relativeFilePath
$currentArchiveEntry = $zipArchive.CreateEntry($entryPath, $compression)
$numberOfItemsArchived += 1
$addItemtoArchiveFileMessage = ($LocalizedData.AddItemtoArchiveFile -f $currentFilePath)
}
if($null -ne $addItemtoArchiveFileMessage)
{
Write-Verbose $addItemtoArchiveFileMessage
}
$currentEntryCount += 1
ProgressBarHelper "Compress-Archive" $progressBarStatus $previousSegmentWeight $currentSegmentWeight $sourcePaths.Count $currentEntryCount
}
}
finally
{
If($null -ne $zipArchive)
{
$zipArchive.Dispose()
}
If($null -ne $archiveFileStream)
{
$archiveFileStream.Dispose()
}
# Complete writing progress.
Write-Progress -Activity "Compress-Archive" -Completed
}
return $numberOfItemsArchived
}
<############################################################################################
# ValidateArchivePathHelper: This is a helper function used to validate the archive file
# path & its file format. The only supported archive file format is .zip
############################################################################################>
function ValidateArchivePathHelper
{
param
(
[string] $archiveFile
)
if(-not [System.IO.File]::Exists($archiveFile))
{
$errorMessage = ($LocalizedData.PathNotFoundError -f $archiveFile)
ThrowTerminatingErrorHelper "PathNotFound" $errorMessage ([System.Management.Automation.ErrorCategory]::InvalidArgument) $archiveFile
}
}
<############################################################################################
# ExpandArchiveHelper: This is a helper function used to expand the archive file contents
# to the specified directory.
############################################################################################>
function ExpandArchiveHelper
{
param
(
[string] $archiveFile,
[string] $expandedDir,
[ref] $expandedItems,
[boolean] $force,
[boolean] $isVerbose,
[boolean] $isConfirm
)
Add-CompressionAssemblies
try
{
# The existence of archive file has already been validated by ValidateArchivePathHelper
# before calling this helper function.
$archiveFileStreamArgs = @($archiveFile, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
$archiveFileStream = New-Object -TypeName System.IO.FileStream -ArgumentList $archiveFileStreamArgs
$zipArchiveArgs = @($archiveFileStream, [System.IO.Compression.ZipArchiveMode]::Read, $false)
try
{
$zipArchive = New-Object -TypeName System.IO.Compression.ZipArchive -ArgumentList $zipArchiveArgs
}
catch [System.IO.InvalidDataException]
{
# Failed to open the file for reading as a zip archive. Wrap the exception
# and re-throw it indicating it does not appear to be a valid zip file.
$exception = $_.Exception
if($null -ne $_.Exception -and
$null -ne $_.Exception.InnerException)
{
$exception = $_.Exception.InnerException
}
# Load the WindowsBase.dll assembly to get access to the System.IO.FileFormatException class
[System.Reflection.Assembly]::Load('WindowsBase,Version=4.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35')
$invalidFileFormatException = New-Object -TypeName System.IO.FileFormatException -ArgumentList @(
($LocalizedData.ItemDoesNotAppearToBeAValidZipArchive -f $archiveFile)
$exception
)
throw $invalidFileFormatException
}
if($zipArchive.Entries.Count -eq 0)
{
$archiveFileIsEmpty = ($LocalizedData.ArchiveFileIsEmpty -f $archiveFile)
Write-Verbose $archiveFileIsEmpty
return
}
$currentEntryCount = 0
$progressBarStatus = ($LocalizedData.ExpandProgressBarText -f $archiveFile)
# Ensures that the last character on the extraction path is the directory separator char.
# Without this, a bad zip file could try to traverse outside of the expected extraction path.
# At this point $expandedDir is a fully qualified path without any relative segments.
if (-not $expandedDir.EndsWith([System.IO.Path]::DirectorySeparatorChar))
{
$expandedDir += [System.IO.Path]::DirectorySeparatorChar
}
# The archive entries can either be empty directories or files.
foreach($currentArchiveEntry in $zipArchive.Entries)
{
# Windows filesystem provider will internally convert from `/` to `\`
$currentArchiveEntryPath = Join-Path -Path $expandedDir -ChildPath $currentArchiveEntry.FullName
# Remove possible relative segments from target
# This is similar to [System.IO.Path]::GetFullPath($currentArchiveEntryPath) but uses PS current dir instead of process-wide current dir
$currentArchiveEntryPath = $PSCmdlet.SessionState.Path.GetUnresolvedProviderPathFromPSPath($currentArchiveEntryPath)
# Check that expanded relative paths and absolute paths from the archive are Not going outside of target directory