forked from RainbowMage/OverlayPlugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PS-Zip.psm1
636 lines (553 loc) · 21.1 KB
/
PS-Zip.psm1
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
# PowerShellUtils - Scripts for manage with PowerShell Scripts
# Copyright c 2013-2013 Ikiru Yoshizaki <cherishacquire@outlook.com>
# All rights reserved.
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#Requires -Version 3.0
function New-ZipCompress{
[CmdletBinding(DefaultParameterSetName="safe")]
param(
[parameter(
mandatory,
position = 0,
valuefrompipeline,
valuefrompipelinebypropertyname)]
[string]
$source,
[parameter(
mandatory = 0,
position = 1,
valuefrompipeline,
valuefrompipelinebypropertyname)]
[string]
$destination,
[parameter(
mandatory = 0,
position = 2)]
[switch]
$quiet,
[parameter(
mandatory = 0,
position = 3,
ParameterSetName="safe")]
[switch]
$safe,
[parameter(
mandatory = 0,
position = 3,
ParameterSetName="force")]
[switch]
$force
)
begin
{
# only run with Verbose mode
if ($PSBoundParameters.Verbose.IsPresent)
{
# start Stopwatch
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$starttime = Get-Date
}
Write-Debug "import .NET Class for ZipFile"
try
{
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
}
catch
{
}
}
process
{
Write-Verbose "check source is file or Directory."
$file = Get-Item -Path $source
Write-Debug 'Another check source is "file/directory" or "contains PSISContainer'
if ($file.PSISContainer -and ($file.count -gt 1) -and ($source[-1] -eq "*"))
{
Write-Verbose "Detected as source using * without extension."
$oldsource = $source
$f = Get-Item -Path $source | select -First 1
$source = Split-Path -Path $f -Parent
$file = Get-Item -Path $source
Write-Verbose ("changed source {0} to parent folder {1}." -f $oldsource, $source)
}
# set zip extension
$zipExtension = ".zip"
Write-Debug ("set desktop as destination path destination {0} is null" -f $destination)
if ([string]::IsNullOrWhiteSpace($destination))
{
$desktop = [System.Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)
if ($file.PSISContainer -and ($file.count -eq 1))
{
Write-Verbose "Detected as Directory"
if ($file.FullName -eq $file.Root)
{
$filename = $file.PSDrive.Name
}
else
{
# remove \ or / on last letter of source
$fullpath = Join-Path (Split-Path -Path $file -Parent) (Split-Path -Path $file -Leaf)
$filename = [System.IO.Path]::GetFileName($fullpath)
}
Write-Verbose ("Desktop : {0}" -f $desktop)
Write-Verbose ("GetFileName : {0}" -f $filename)
Write-Verbose ("zipExtension : {0}" -f $zipExtension)
$destination = Join-Path $desktop ($filename + $zipExtension)
}
elseif ($file.PSISContainer -and ($file.count -gt 1) -and ($source[-1] -eq "*"))
{
Write-Verbose "Detected as source which use * without extension"
Write-Verbose "create zip from parent directory when last letter of source was wildcard *"
$filename = ([System.IO.Path]::GetFileNameWithoutExtension($file.FullName))
Write-Verbose ("Desktop : {0}" -f $desktop)
Write-Verbose ("GetFileName : {0}" -f $filename)
Write-Verbose ("zipExtension : {0}" -f $zipExtension)
$destination = Join-Path $desktop ($filename + $zipExtension)
}
else
{
Write-Verbose "Detected as File"
# use first file name as zip name
$filename = ([System.IO.Path]::GetFileNameWithoutExtension(($file | select -First 1 -ExpandProperty fullname)))
Write-Verbose ("Desktop : {0}" -f $desktop)
Write-Verbose ("GetFileName : {0}" -f ([System.IO.Path]::GetFileNameWithoutExtension(($file | select -First 1 -ExpandProperty fullname))))
Write-Verbose ("zipExtension : {0}" -f $zipExtension)
$destination = Join-Path $desktop ($filename + $zipExtension)
}
}
Write-Debug "check destination is input as .zip"
if (-not($destination.EndsWith($zipExtension)))
{
throw ("destination parameter value [{0}] not end with extension {1}" -f $destination, $zipExtension)
}
Write-Debug "check destination is already exist, CreateFromDirectory Method will fail with same name of destination file."
if (Test-Path $destination)
{
if ($safe)
{
Write-Debug "safe output zip file to new destination path, avoiding destination zip name conflict."
# show warning for same destination exist.
Write-Verbose ("Detected destination name {0} is already exist." -f $destination)
$olddestination = $destination
# get current destination information
$destinationRoot = [System.IO.Path]::GetDirectoryName($destination)
$destinationfile = [System.IO.Path]::GetFileNameWithoutExtension($destination)
$destinationExtension = [System.IO.Path]::GetExtension($destination)
# renew destination name with (2)...(x) until no more same name catch.
$count = 2
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
while (Test-Path $destination)
{
++$count
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
}
# show warning as destination name had been changed due to escape error.
Write-Warning ("Safe old deistination {0} change to new name {1}" -f $olddestination, $destination)
}
else
{
if($force)
{
Write-Warning ("force replacing old zip file {0}" -f $destination)
Remove-Item -Path $destination -Force
}
else
{
Remove-Item -Path $destination -Confirm
}
if (Test-Path $destination)
{
Write-Warning "Cancelled removing item. Quit cmdlet execution."
return
}
}
}
else
{
Write-Debug ("Destination not found. Check parent folder for destination {0} is exist." -f $destination)
$parentpath = Split-Path $destination -Parent
if (-not(Test-Path $parentpath))
{
Write-Warning ("Parent folder {0} not found. Creating path." -f $parentpath)
New-Item -Path $parentpath -ItemType Directory -Force
}
}
# compressionLevel
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
# show file property
Write-Verbose ("file.PSISContainer : {0}" -f $file.PSISContainer)
Write-Verbose ("file.count : {0}" -f $file.count)
Write-Debug "execute compression"
if ($file.PSISContainer -and ($file.count -eq 1))
{
try # create zip from directory
{
# include BaseDirectory
$includeBaseDirectory = $true
Write-Verbose "Detected as Directory"
Write-Verbose ("destination : {0}" -f $destination)
Write-Verbose ("file.fullname : {0}" -f $file.FullName)
Write-Verbose ("compressionLevel : {0}" -f $compressionLevel)
Write-Verbose ("includeBaseDirectory : {0}" -f $includeBaseDirectory)
if ($quiet)
{
Write-Verbose ("zipping up folder {0} to {1}" -f $file.FullName, $destination)
[System.IO.Compression.ZipFile]::CreateFromDirectory($file.fullname,$destination,$compressionLevel,$includeBaseDirectory) > $null
$?
}
else
{
Write-Verbose ("zipping up folder {0} to {1}" -f $file.FullName, $destination)
[System.IO.Compression.ZipFile]::CreateFromDirectory($file.fullname,$destination,$compressionLevel,$includeBaseDirectory)
Get-Item $destination
}
}
catch
{
Write-Error $_
$?
}
}
elseif ($file.PSISContainer -and ($file.count -gt 1) -and ($source[-1] -eq "*"))
{
try # create zip from directory when last letter of source was wildcard *
{
# include BaseDirectory
$includeBaseDirectory = $true
Write-Verbose "Detected as source which use * without extension"
Write-Verbose ("destination : {0}" -f $destination)
Write-Verbose ("file.fullname : {0}" -f $file.FullName)
Write-Verbose ("compressionLevel : {0}" -f $compressionLevel)
Write-Verbose ("includeBaseDirectory : {0}" -f $includeBaseDirectory)
if ($quiet)
{
Write-Verbose ("zipping up folder {0} to {1}" -f $file.FullName, $destination)
[System.IO.Compression.ZipFile]::CreateFromDirectory($file.FullName,$destination,$compressionLevel,$includeBaseDirectory) > $null
$?
}
else
{
Write-Verbose ("zipping up folder {0} to {1}" -f $file.FullName, $destination)
[System.IO.Compression.ZipFile]::CreateFromDirectory($file.FullName,$destination,$compressionLevel,$includeBaseDirectory)
Get-Item $destination
}
}
catch
{
Write-Error $_
$?
}
}
else
{
try # create zip from files
{
# create zip to add
$destzip = [System.IO.Compression.Zipfile]::Open($destination,"Update")
# get items
$files = Get-ChildItem -Path $source
foreach ($file in $files)
{
Write-Verbose "Detected as File"
Write-Verbose ("destzip : {0}" -f $destzip)
Write-Verbose ("file.fullname : {0}" -f $file.FullName)
Write-Verbose ("file.name : {0}" -f $file2)
Write-Verbose ("compressionLevel : {0}" -f $compressionLevel)
Write-Verbose ("zipping up files {0} to {1}" -f $file.FullName, $destzip)
[System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($destzip, $file.FullName, $file.Name, $compressionLevel) > $null
}
# show result
if ($quiet)
{
$?
}
else
{
Get-Item $destination
}
}
catch
{
Write-Error $_
$?
}
finally
{
Write-Debug ("Dispose Object {0} to remove file handler." -f $sourcezip)
$destzip.Dispose()
}
}
}
end
{
# only run with Verbose mode
if ($PSBoundParameters.Verbose.IsPresent)
{
# end Stopwatch
$endsw = $sw.Elapsed.TotalMilliseconds
$endtime = Get-Date
Write-Verbose ("Start time`t: {0:o}" -f $starttime)
Write-Verbose ("End time`t: {0:o}" -f $endtime)
Write-Verbose ("Duration`t: {0} ms" -f $endsw)
}
}
}
function New-ZipExtract{
[CmdletBinding(DefaultParameterSetName="safe")]
param(
[parameter(
mandatory,
position = 0,
valuefrompipeline,
valuefrompipelinebypropertyname)]
[string]
$source,
[parameter(
mandatory = 0,
position = 1,
valuefrompipeline,
valuefrompipelinebypropertyname)]
[string]
$destination,
[parameter(
mandatory = 0,
position = 2)]
[switch]
$quiet,
[parameter(
mandatory = 0,
position = 3,
ParameterSetName="safe")]
[switch]
$safe,
[parameter(
mandatory = 0,
position = 3,
ParameterSetName="force")]
[switch]
$force
)
begin
{
# only run with Verbose mode
if ($PSBoundParameters.Verbose.IsPresent)
{
# start Stopwatch
$sw = [System.Diagnostics.Stopwatch]::StartNew()
$starttime = Get-Date
}
Write-Debug "import .NET Class for ZipFile"
try
{
Add-Type -AssemblyName "System.IO.Compression.FileSystem"
}
catch
{
}
}
process
{
$zipExtension = ".zip"
Write-Debug "Check source is input as .zip"
if (-not($source.EndsWith($zipExtension)))
{
throw ("source parameter value [{0}] not end with extension {1}" -f $source, $zipExtension)
}
Write-Debug ("set desktop as destination path destination {0} is null" -f $destination)
if ([string]::IsNullOrWhiteSpace($destination))
{
$desktop = [System.Environment]::GetFolderPath([Environment+SpecialFolder]::Desktop)
$directoryname = [System.IO.Path]::GetFileNameWithoutExtension($source)
Write-Verbose ("Desktop : {0}" -f $desktop)
Write-Verbose ("GetFileName : {0}" -f $directoryname)
$destination = Join-Path $desktop $directoryname
}
Write-Debug "check destination is already exist, ExtractToDirectory Method will fail with same name of destination file."
if (Test-Path $destination)
{
if ($safe)
{
Write-Debug "safe output zip file to new destination path, avoiding destination zip name conflict."
# show warning for same destination exist.
Write-Verbose ("Detected destination name {0} is already exist. safe trying output to new destination zip name." -f $destination)
$olddestination = $destination
# get current destination information
$destinationRoot = [System.IO.Path]::GetDirectoryName($destination)
$destinationfile = [System.IO.Path]::GetFileNameWithoutExtension($destination)
$destinationExtension = [System.IO.Path]::GetExtension($destination)
# renew destination name with (2)...(x) until no more same name catch.
$count = 2
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
while (Test-Path $destination)
{
++$count
$destination = Join-Path $destinationRoot ($destinationfile + "(" + $count + ")" + $destinationExtension)
}
# show warning as destination name had been changed due to escape error.
Write-Warning ("Safe old deistination {0} change to new name {1}" -f $olddestination, $destination)
}
else
{
if($force)
{
Write-Warning ("force replacing old zip file {0}" -f $destination)
Remove-Item -Path $destination -Recurse -Force
}
else
{
Remove-Item -Path $destination -Recurse -Confirm
}
if (Test-Path $destination)
{
Write-Warning "Cancelled removing item. Quit cmdlet execution."
return
}
}
}
else
{
Write-Debug ("Destination not found. Check parent folder for destination {0} is exist." -f $destination)
$parentpath = Split-Path $destination -Parent
if (-not(Test-Path $parentpath))
{
Write-Warning ("Parent folder {0} not found. Creating path." -f $parentpath)
New-Item -Path $parentpath -ItemType Directory -Force
}
}
try
{
Write-Debug "create source zip and complression"
$sourcezip = [System.IO.Compression.Zipfile]::Open($source,"Update")
$compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
Write-Verbose ("sourcezip : {0}" -f $sourcezip)
Write-Verbose ("destination : {0}" -f $destination)
Write-Debug "Execute Main Process ExtractToDirectory."
if ($quiet)
{
[System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($sourcezip,$destination) > $null
$?
}
else
{
[System.IO.Compression.ZipFileExtensions]::ExtractToDirectory($sourcezip,$destination)
}
$sourcezip.Dispose()
}
catch
{
Write-Error $_
}
finally
{
Write-Debug ("Dispose Object {0} to remove file handler." -f $sourcezip)
$sourcezip.Dispose()
}
}
end
{
# only run with Verbose mode
if ($PSBoundParameters.Verbose.IsPresent)
{
# end Stopwatch
$endsw = $sw.Elapsed.TotalMilliseconds
$endtime = Get-Date
Write-Verbose ("Start time`t: {0:o}" -f $starttime)
Write-Verbose ("End time`t: {0:o}" -f $endtime)
Write-Verbose ("Duration`t: {0} ms" -f $endsw)
}
}
}
Export-ModuleMember `
-Function * `
-Cmdlet * `
-Variable *
<#
#### Compres Test
$ErrorActionPreference = "stop"
try
{
1
New-ZipCompress -source D:\hoge -destination d:\hoge.zip -verbose
2
New-ZipCompress -source D:\hoge -verbose
3
New-ZipCompress -source D:\hoge -force -verbose
4
New-ZipCompress -source D:\hoge -safe -verbose
5
New-ZipCompress -source D:\hoge\ -quiet -verbose
6
New-ZipCompress -source D:\hoge\* -destination d:\hoge.zip -verbose
7
New-ZipCompress -source D:\hoge\* -verbose
8
New-ZipCompress -source D:\hoge\* -force -verbose
9
New-ZipCompress -source D:\hoge\* -safe -verbose
10
New-ZipCompress -source D:\hoge\* -quiet -verbose
11
New-ZipCompress -source D:\hoge\*.ps1 -destination d:\hoge.zip -verbose
12
New-ZipCompress -source D:\hoge\*.ps1 -verbose
13
New-ZipCompress -source D:\hoge\*.ps1 -force -verbose
14
New-ZipCompress -source D:\hoge\*.ps1 -safe -verbose
15
New-ZipCompress -source D:\hoge\*.ps1 -quiet -verbose
16
New-ZipCompress -source R:\ -destination d:\hoge.zip -verbose
17
New-ZipCompress -source R:\ -verbose
18
New-ZipCompress -source R:\ -force -verbose
19
New-ZipCompress -source R:\ -safe -verbose
20
New-ZipCompress -source R:\ -quiet -verbose
}
catch
{
Write-Error $_
}
#### Extract Test
$ErrorActionPreference = "stop"
try
{
1
New-ZipExtract -source D:\hoge.zip -destination d:\hogehogehoge -verbose
2
New-ZipExtract -source D:\hoge.zip -verbose
3
New-ZipExtract -source D:\hoge.zip -force -verbose
4
New-ZipExtract -source D:\hoge.zip -safe -verbose
5
New-ZipExtract -source D:\hoge.zip -quiet -verbose
}
catch
{
Write-Error $_
}
#>