简体   繁体   中英

How to pipe the results of the curl command into tar using Powershell?

I'm currently trying to run the following command in Windows Powershell:

curl https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.65/bin/apache-tomcat-8.5.65.tar.gz | tar xvz --strip-components=1 - -C ~/tomcat-8.6.65

When I run this command it tries to untar the default value \\\\.\\tape0 instead of the Tomcat tarball. I know that a workaround is to specify a filename with -f , however I don't know how that would be possible when I'm pulling the file from a URI. Does anyone have a solution to make this command work?

  • PowerShell only communicates via text (strings) with external programs, it doesn't support raw byte streams ; see this answer for more information.

  • In Windows PowerShell (but no longer in PowerShell (Core) 7+ ), curl is a built-in alias for the Invoke-WebRequest cmdlet; to call the external curl.exe program instead, use curl.exe , ie include the filename extension .

Since you do need raw byte-stream processing, use of the PowerShell pipeline isn't an option, but you can delegate to cmd.exe :

# Make sure that the target dir. exists and get its full path.
# (If the target dir. doesn't exist, tar's -C option will fail.)
$targetDir = (New-Item -Force -ErrorAction Stop -Type Directory $HOME/tomcat-8.6.65).FullName

# Invoke cmd.exe's binary pipeline, passing the target dir.
# As an aside: tar.exe on Windows does NOT know that ~ refers to the 
#              user's home dir.
cmd /c @"
curl https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.65/bin/apache-tomcat-8.5.65.tar.gz | tar xvz --strip-components=1 -f - -C "$targetDir"
"@

The above also corrects a syntax problem with your tar command: - should be -f - .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM