2015-04-15 34 views
1

我是PowerShell的新手,我正在嘗試編寫腳本來提取.tar.gz文件。PowerShell可以提取.tgz文件嗎?

它需要2個步驟來解壓文件。

# Create a .tar file 
7z.exe a -ttar files.tar *.txt 
7z.exe a -tgzip files.tar.gz files.tar 


# These 2 work 
& 'C:\Program Files\7-Zip\7z.exe' e .\files.tar.gz 
& 'C:\Program Files\7-Zip\7z.exe' x -aoa -ttar .\files.tar -o'c:\foobar' 

我想這兩個命令組合成一個命令,這樣我可以跳過寫files.tar文件保存到磁盤。

然而,當我嘗試結合的功能,我得到錯誤信息「不正確的函數」

有沒有辦法來組合這些2 7zip的命令到1?

& 'C:\Program Files\7-Zip\7z.exe' e .\files.tar.gz -so | & 'C:\Program Files\7-Zip\7z.exe' x -aoa -ttar -si -o'c:\foobar' 
+1

有使用7-​​ZIP DLL,它可能會在這裏有用的(我沒有用它在所有但一個PowerShell模塊)。這些命令中的每一個都能正確工作嗎? (如果您從變量中的第一個命令捕獲標準輸出並通過標準輸入將其傳輸到第二個命令?) –

+0

不要忘記,您可以在PowerShell中輕鬆使用.NET程序集。您可能可以使用像SharpCompress或tar-cs之類的東西。 –

回答

8

正如你可以看到7-Zip不是很擅長這個。自2009年以來,人們一直在使用asking for tarball原子操作。Here is a small program (490 KB)在Go中可以做到這一點,我爲compiled it

package main 
import (
    "archive/tar" 
    "compress/gzip" 
    "flag" 
    "fmt" 
    "io" 
    "os" 
    "strings" 
) 

func main() { 
    flag.Parse() // get the arguments from command line 
    sourcefile := flag.Arg(0) 
    if sourcefile == "" { 
    fmt.Println("Usage : go-untar sourcefile.tar.gz") 
    os.Exit(1) 
    } 
    file, err := os.Open(sourcefile) 
    if err != nil { 
    fmt.Println(err) 
    os.Exit(1) 
    } 
    defer file.Close() 
    var fileReader io.ReadCloser = file 
    // just in case we are reading a tar.gz file, 
    // add a filter to handle gzipped file 
    if strings.HasSuffix(sourcefile, ".gz") { 
    if fileReader, err = gzip.NewReader(file); err != nil { 
     fmt.Println(err) 
     os.Exit(1) 
    } 
    defer fileReader.Close() 
    } 
    tarBallReader := tar.NewReader(fileReader) 
    // Extracting tarred files 
    for { 
    header, err := tarBallReader.Next() 
    if err != nil { 
     if err == io.EOF { 
     break 
     } 
     fmt.Println(err) 
     os.Exit(1) 
    } 
    // get the individual filename and extract to the current directory 
    filename := header.Name 
    switch header.Typeflag { 
    case tar.TypeDir: 
     // handle directory 
     fmt.Println("Creating directory :", filename) 
     // or use 0755 if you prefer 
     err = os.MkdirAll(filename, os.FileMode(header.Mode)) 
     if err != nil { 
     fmt.Println(err) 
     os.Exit(1) 
     } 
    case tar.TypeReg: 
     // handle normal file 
     fmt.Println("Untarring :", filename) 
     writer, err := os.Create(filename) 
     if err != nil { 
     fmt.Println(err) 
     os.Exit(1) 
     } 
     io.Copy(writer, tarBallReader) 
     err = os.Chmod(filename, os.FileMode(header.Mode)) 
     if err != nil { 
     fmt.Println(err) 
     os.Exit(1) 
     } 
     writer.Close() 
    default: 
     fmt.Printf("Unable to untar type : %c in file %s", header.Typeflag, 
     filename) 
    } 
    } 
} 
0

如果您已經安裝了Python,你可以找到這段代碼放在手邊:

$path = 'C:\yourPythonFile.py' 
$str = "import tarfile", 
     "tar = tarfile.open(""C:/yourTarFile.tar"")", 
     "tar.extractall(""."")", 
     "tar.close()" 
[System.IO.File]::WriteAllLines($path, $str) 
cd C:\ 
python yourPythonFile.py