Skip to content

Commit e4cc0ae

Browse files
Update script 🦅
1 parent 4987962 commit e4cc0ae

1 file changed

Lines changed: 81 additions & 41 deletions

File tree

Powershell/General Functions/Expand-ZipOrGzip.ps1

Lines changed: 81 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<#
22
.SYNOPSIS
3-
Expand-ZipOrGzip - Extracts ZIP or GZIP files, including TAR within GZIP.
3+
Expand-ZipOrGzip - Extracts ZIP or GZIP files, including TAR within GZIP, using 7-Zip for GZIP files if specified, with an option to hide toast notifications.
44
55
.DESCRIPTION
6-
This function extracts ZIP or GZIP compressed files. If a GZIP file contains a TAR file, it extracts the TAR file as well. Notifications are shown at various stages of the process.
6+
This function extracts ZIP or GZIP compressed files. If a GZIP file contains a TAR file, it extracts the TAR file as well. Notifications are shown at various stages of the process unless hidden by a switch. If the -7zip switch is used, GZIP files are extracted using 7-Zip.
77
88
.PARAMETER FilePath
99
The path of the ZIP or GZIP file to be extracted.
@@ -17,17 +17,15 @@
1717
.PARAMETER OverwriteExistingFiles
1818
Optionally, overwrites existing files in the destination directory without prompting.
1919
20-
.EXAMPLE
21-
Expand-ZipOrGzip -FilePath "C:\path\to\file.gz" -DestinationFolderPath "C:\extract\here" -OverwriteExistingFiles
22-
Extracts a GZIP file to the specified destination, overwriting any existing files.
20+
.PARAMETER Use7zip
21+
Optionally, use 7-Zip for extracting GZIP files.
2322
24-
.EXAMPLE
25-
Expand-ZipOrGzip -FilePath "C:\*.gz" -DestinationFolderPath "C:\extract\here" -OverwriteExistingFiles
26-
Extracts a GZIP file of wildcard to the specified destination, overwriting any existing files.
23+
.PARAMETER HideToastNotifications
24+
Optionally, hides toast notifications during the process.
2725
2826
.EXAMPLE
29-
Expand-ZipOrGzip -FilePath "C:\path\to\archive.zip" -DestinationFolderPath "C:\extract\zip"
30-
Extracts a ZIP file to the specified destination folder.
27+
Expand-ZipOrGzip -FilePath "C:\path\to\file.gz" -DestinationFolderPath "C:\extract\here" -7zip
28+
Extracts a GZIP file using 7-Zip to the specified destination.
3129
3230
.NOTES
3331
Author: Blake Drumm (blakedrumm@microsoft.com)
@@ -38,42 +36,65 @@
3836
#>
3937
param
4038
(
41-
[ValidateNotNullOrEmpty()]
4239
[string]$FilePath,
43-
[ValidateNotNullOrEmpty()]
4440
[string]$DestinationFolderPath,
4541
[switch]$HideProgressDialog,
46-
[switch]$OverwriteExistingFiles
42+
[switch]$OverwriteExistingFiles,
43+
[switch]$Use7zip,
44+
[switch]$HideToastNotifications
4745
)
4846

49-
# Function to show toast notification
50-
function Show-ToastNotification
51-
{
52-
param (
53-
[string]$Title,
54-
[string]$Text,
55-
[int]$Duration = 500,
56-
[string]$IconType = 'Info'
57-
)
58-
59-
Add-Type -AssemblyName System.Windows.Forms
60-
$notifyIcon = New-Object System.Windows.Forms.NotifyIcon
61-
62-
$notifyIcon.Icon = [System.Drawing.SystemIcons]::Information
63-
$notifyIcon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::$IconType
64-
$notifyIcon.BalloonTipTitle = $Title
65-
$notifyIcon.BalloonTipText = $Text
66-
$notifyIcon.Visible = $true
67-
$notifyIcon.ShowBalloonTip($Duration)
68-
}
69-
7047
function Expand-ZipOrGzip (
71-
[ValidateNotNullOrEmpty()][string]$FilePath,
72-
[ValidateNotNullOrEmpty()][string]$DestinationFolderPath,
48+
[ValidateNotNullOrEmpty()]
49+
[string]$FilePath,
50+
[ValidateNotNullOrEmpty()]
51+
[string]$DestinationFolderPath,
7352
[switch]$HideProgressDialog,
74-
[switch]$OverwriteExistingFiles
53+
[switch]$OverwriteExistingFiles,
54+
[switch]$Use7zip,
55+
[switch]$HideToastNotifications
7556
)
7657
{
58+
59+
# Global variable for NotifyIcon
60+
$Global:notifyIcon = $null
61+
62+
# Function to show toast notification
63+
function Show-ToastNotification
64+
{
65+
param (
66+
[string]$Title,
67+
[string]$Text,
68+
[int]$Duration = 5000,
69+
# Duration in milliseconds
70+
[string]$IconType = 'Info'
71+
)
72+
73+
if (-not $HideToastNotifications)
74+
{
75+
Add-Type -AssemblyName System.Windows.Forms
76+
77+
# Reuse the existing NotifyIcon or create a new one
78+
if ($null -eq $Global:notifyIcon)
79+
{
80+
$Global:notifyIcon = New-Object System.Windows.Forms.NotifyIcon
81+
$Global:notifyIcon.Icon = [System.Drawing.SystemIcons]::Information
82+
}
83+
84+
$Global:notifyIcon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::$IconType
85+
$Global:notifyIcon.BalloonTipTitle = $Title
86+
$Global:notifyIcon.BalloonTipText = $Text
87+
$Global:notifyIcon.Visible = $true
88+
$Global:notifyIcon.ShowBalloonTip($Duration)
89+
90+
# Schedule disposal of the NotifyIcon
91+
Start-Sleep -Milliseconds ($Duration + 100)
92+
$Global:notifyIcon.Visible = $false
93+
$Global:notifyIcon.Dispose()
94+
$Global:notifyIcon = $null
95+
}
96+
}
97+
7798
# Resolve file paths with wildcard characters
7899
$resolvedFilePaths = Resolve-Path $FilePath
79100
foreach ($resolvedFilePath in $resolvedFilePaths)
@@ -85,8 +106,20 @@ function Expand-ZipOrGzip (
85106
try
86107
{
87108
$fileExtension = [System.IO.Path]::GetExtension($resolvedFilePath).ToLower()
88-
89-
if ($fileExtension -eq ".gz")
109+
if ($fileExtension -eq ".gz" -and $Use7zip)
110+
{
111+
# Using 7zip for extraction
112+
$outputFolderPath = [System.IO.Path]::GetDirectoryName($DestinationFolderPath)
113+
$outputFileName = [System.IO.Path]::GetFileNameWithoutExtension($resolvedFilePath)
114+
$7zipPath = "C:\Program Files\7-Zip\7z.exe" # Adjust this path as needed
115+
116+
Show-ToastNotification -Title "7-Zip Extraction Started" -Text "Starting 7-Zip extraction of: `n$resolvedFilePath"
117+
118+
Start-Process -FilePath $7zipPath -ArgumentList "e `"$resolvedFilePath`" -o`"$outputFolderPath`" -y" -NoNewWindow -Wait
119+
120+
Show-ToastNotification -Title "7-Zip Extraction Complete" -Text "7-Zip extraction complete for: `n$resolvedFilePath"
121+
}
122+
elseif ($fileExtension -eq ".gz")
90123
{
91124
$outputFilePath = [System.IO.Path]::Combine($DestinationFolderPath, [System.IO.Path]::GetFileNameWithoutExtension($resolvedFilePath))
92125
$error.Clear()
@@ -203,10 +236,17 @@ function Expand-ZipOrGzip (
203236
}
204237
if ($FilePath -or $DestinationFolderPath)
205238
{
206-
Expand-ZipOrGzip -FilePath $FilePath -DestinationFolderPath $DestinationFolderPath -HideProgressDialog:$HideProgressDialog -OverwriteExistingFiles:$OverwriteExistingFiles
239+
Expand-ZipOrGzip -FilePath $FilePath -DestinationFolderPath $DestinationFolderPath -HideProgressDialog:$HideProgressDialog -OverwriteExistingFiles:$OverwriteExistingFiles -Use7zip:$Use7zip
207240
}
208241
else
209242
{
210-
# Example usage
243+
# --------------------------------------------------------------------------
244+
# Example 1:
245+
# Expand-ZipOrGzip -FilePath "G:\*.tar.gz" -DestinationFolderPath "C:\Output" -OverwriteExistingFiles -Use7zip
246+
# --------------------------------------------------------------------------
247+
# Example 2:
248+
# Expand-ZipOrGzip -FilePath "G:\File.zip" -DestinationFolderPath "C:\Output" -OverwriteExistingFiles
249+
# --------------------------------------------------------------------------
250+
# Modify the line below to change what happens when you run this script from PowerShell ISE
211251
Expand-ZipOrGzip
212252
}

0 commit comments

Comments
 (0)