Skip to content

Commit f82c287

Browse files
committed
Added ConvertFrom-Gzip cmdlet
1 parent dec8ee3 commit f82c287

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

filesystem/gzip.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function ConvertFrom-Gzip {
2+
Param (
3+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
4+
[ValidateScript( { (Get-Item $_).Name.EndsWith(".gz") })]
5+
[System.IO.FileInfo]
6+
$InputObject,
7+
8+
[Parameter(Mandatory = $false)]
9+
[switch]
10+
$RemoveInputFile
11+
)
12+
Process {
13+
# Create a new file and open a filestream for it
14+
$NewFilename = $InputObject.FullName.Remove($InputObject.FullName.Length - $InputObject.Extension.Length)
15+
$DecompressedFileStream = [System.IO.File]::Create($NewFilename)
16+
17+
# Open the compressed file and copy the file to the decompressed stream
18+
$CompressedFileStream = $InputObject.OpenRead()
19+
$GZipStream = [System.IO.Compression.GZipStream]::new($CompressedFileStream, [System.IO.Compression.CompressionMode]::Decompress)
20+
$GZipStream.CopyTo($DecompressedFileStream)
21+
22+
# Cleanup
23+
$DecompressedFileStream.Dispose()
24+
$GZipStream.Dispose()
25+
$CompressedFileStream.Dispose()
26+
$DecompressedFileStream, $GZipStream, $CompressedFileStream = $null
27+
28+
# Remove the initial file if requested.
29+
if ($PSBoundParameters.ContainsKey('RemoveInputFile')) {
30+
$InputObject.Delete()
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)