我是Powershell的新手,我正在尝试编写脚本以提取.tar.gz文件。
解压缩文件需要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'
最佳答案
如您所见,7-Zip在此方面不是很好。人们一直在asking for
从2009年开始进行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)
}
}
}
关于powershell - Powershell可以提取.tgz文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662657/