如何使用powershell创建tar gz文件

时间:2015-02-05 21:57:29

标签: powershell powershell-v4.0

我正在寻找使用Powershell v4创建tar.gz存档解决问题的解决方案。我无法找到Microsoft创建或验证的扩展/数据包来提供此类功能。这样的解决方案是否存在[tar-ing和gzip-ing]?

2 个答案:

答案 0 :(得分:2)

另一种可能性是调用Windows现在支持的本机tar命令(使用gzip压缩):

tar -cvzf filename.tgz mydir

https://blogs.technet.microsoft.com/virtualization/2017/12/19/tar-and-curl-come-to-windows/

答案 1 :(得分:1)

Microsoft在.Net 2.0中实现了gzipstream in System.IO.Compression C#中有很多示例,而PowerShell中有很多其他示例。 Powershell Community Extensions还具有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()
                }
            }
        }
    }
}

用法:

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".