use*_*246 3 powershell powershell-4.0
我正在寻找解决方案来解决使用 Powershell v4 创建 tar.gz 存档的问题。我找不到由 Microsoft 创建或验证以提供此类功能的扩展/数据包。是否存在这样的解决方案 [tar-ing 和 gzip-ing]?
mik*_*dge 10
另一种可能性是调用 Windows 现在支持的本机 tar 命令(使用 gzip 压缩):
tar -cvzf filename.tgz mydir
Run Code Online (Sandbox Code Playgroud)
https://techcommunity.microsoft.com/t5/Containers/Tar-and-Curl-Come-to-Windows/ba-p/382409
Microsoft在 .Net 2.0 的System.IO.Compression 中实现了 gzipstream C# 中有大量示例,powershell 中有许多其他示例。Powershell社区扩展还具有 Write-Zip 和 Write-Tar 功能。这是我很久以前做过的 gzip 函数。它确实应该更新以处理多个输入和更好的输出命名。它还一次仅处理一个文件。但如果您想了解它是如何完成的,它应该可以帮助您入门。
function New-GZipArchive
{
[CmdletBinding(SupportsShouldProcess=$true,
PositionalBinding=$true)]
[OutputType([Boolean])]
Param
(
# The input file(s)
[Parameter(Mandatory=$true,
ValueFromPipeline=$true,
Position=0)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
[ValidateScript({Test-Path $_})]
[String]
$fileIn,
# The path to the requested output file
[Parameter(Position=1)]
[ValidateNotNull()]
[ValidateNotNullOrEmpty()]
#validation is done in the script as the only real way to determine if it is a valid path is to try it
[String]
$fileOut,
# Param3 help description
[Switch]
$Clobber
)
Process
{
if ($pscmdlet.ShouldProcess("$fileIn", "Zip file to $fileOut"))
{
if($fileIn -eq $fileOut){
Write-Error "You can't zip a file into itself"
return
}
if(Test-Path $fileOut){
if($Clobber){
Remove-Item $fileOut -Force -Verbose
}else{
Write-Error "The output file already exists and the Clobber parameter was not specified. Please input a non-existent filename or specify the Clobber parameter"
return
}
}
try{
#create read stream
$fsIn = New-Object System.IO.FileStream($fileIn, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
#create out stream
$fsOut = New-Object System.IO.FileStream($fileOut, [System.IO.FileMode]::CreateNew, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)
#create gzip stream using out file stream
$gzStream = New-Object System.IO.Compression.GZipStream($fsout, [System.IO.Compression.CompressionMode]::Compress)
#create a shared buffer
$buffer = New-Object byte[](262144) #256KB
do{
#read into buffer
$read = $fsIn.Read($buffer,0,262144)
#write buffer back out
$gzStream.Write($buffer,0,$read)
}
while($read -ne 0)
}
catch{
#really should add better error handling
throw
}
finally{
#cleanup
if($fsIn){
$fsIn.Close()
$fsIn.Dispose()
}
if($gzStream){
$gzStream.Close()
$gzStream.Dispose()
}
if($fsOut){
$fsOut.Close()
$fsOut.Dispose()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
dir *.txt | %{New-GZipArchive $_.FullName $_.FullName.Replace('.txt','.gz') -verbose -clobber}
VERBOSE: Performing operation "Zip file to C:\temp\a.gz" on Target "C:\temp\a.txt".
VERBOSE: Performing operation "Remove file" on Target "C:\temp\a.gz".
VERBOSE: Performing operation "Zip file to C:\temp\b.gz" on Target "C:\temp\b.txt".
VERBOSE: Performing operation "Remove file" on Target "C:\temp\b.gz".
VERBOSE: Performing operation "Zip file to C:\temp\c.gz" on Target "C:\temp\c.txt".
VERBOSE: Performing operation "Remove file" on Target "C:\temp\c.gz".
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13762 次 |
| 最近记录: |