Powershell source extract .tar

I have a .tar.gz file to extract. I processed the gunzip bit with a GzipStream object from System.IO.Compression , but I could not find anything to work with tarball in this namespace. Is there a way to deal with .tar files natively in Powershell? Please note that it is only important that I can call any such construct / method / object / binary object construct from the Powershell script; in fact, it does not need to be written in powershell. (If it is important, I use 64-bit windows 10)

PS please do not say "use 7zip"; what is not native

+19
powershell tar
source share
7 answers

So, it's been eleven days since I asked about this, and the general consensus is: "No, there are no built-in tools in setting the vanilla window that can handle tar extraction" for you. "

This answer comes from Matthias R. Jensen and TessellatingHeckler , who both refused to answer external questions (I suspect that I don’t want to say no without deep knowledge of the entire Windows system architecture, which is true).

Of course, there are scripts and classes and programs that you can install, but nothing β€œnative”.

+3
source share

I am pretty sure that Windows 10 does not support tar file extraction by default.

1. Fast, easy solution for Windows 10

If you expect the script to have access to an active Internet connection, you can simply get 7Zip support using the built-in package manager. This solution:

  • Fully automated with a script (no user interaction required)
  • Does not require administrator rights
  • Installs the 7Zip support module only if it is not yet available.

Here:

 function Expand-Tar($tarFile, $dest) { if (-not (Get-Command Expand-7Zip -ErrorAction Ignore)) { Install-Package -Scope CurrentUser -Force 7Zip4PowerShell > $null } Expand-7Zip $tarFile $dest } 

To use this:

 Expand-Tar archive.tar dest 

He does his job, but makes constant changes to the client system. There is a better but slightly more complicated solution:

2. The best solution

The best solution is to link 7Zip4PowerShell with your script. This solution:

  • Fully automated with a script (no user interaction required)
  • No administrative privileges required
  • Does not install any software on the client system
  • No internet connection required
  • Must work on earlier versions of Windows

but. Download a copy of the 7Zip4PowerShell package

It is distributed under the LGPL-2.1 license, and therefore everything is in order to associate the package with your product. You must add a note to your documentation that it uses the package and provide a link, as you need to provide source code access for licensed GNU products.

 Save-Module -Name 7Zip4Powershell -Path . 

This will load it into the current directory. This is easiest to do in Windows 10, but there are instructions on how to add PowerShell Gallery to your system in earlier versions of Windows, from the link above.

b. Import the module when you need to extract the tar file

I made a simple function to demonstrate how you can do this based on the first solution:

 function Expand-Tar($tarFile, $dest) { $pathToModule = ".\7Zip4Powershell\1.9.0\7Zip4PowerShell.psd1" if (-not (Get-Command Expand-7Zip -ErrorAction Ignore)) { Import-Module $pathToModule } Expand-7Zip $tarFile $dest } 

You will need to set up $pathToModule where the linked module is actually stored when your script runs, but you need to understand this. I wrote this example so that you can simply paste both blocks of code above into the PowerShell window and get a working Expand-Tar cmdlet:

 Expand-Tar archive.tar dest 
+12
source share

Possible Solution:

I have Windows 10 and I wanted to run curl + tar in Powershell, but it lacked support. Fortunately, I had Ubuntu bash on Windows 10 that comes with preinstalled curl and tar and I ran it in this environment, and then after downloading and unpacking, I switched back to Powershell to continue with everything I did.

Or, as mentioned in a @Don Cruickshank comment below, you can install 7zip support from PowerShell directly using Install-Package 7Zip4Powershell . The Expand-7zip cmdlet extracts several archive formats, including tar.

These workarounds do not solve your specific problem, but for those that are stuck on Windows, they can be useful.

+7
source share

This is a snippet for a PowerShell script:

 try { Add-Type -AssemblyName "ICSharpCode.SharpZLib" $file = [IO.File]::OpenRead($gzArchiveName) $inStream=New-Object -TypeName ICSharpCode.SharpZipLib.GZip.GZipInputStream $file $tarIn = New-Object -TypeName ICSharpCode.SharpZipLib.Tar.TarInputStream $inStream $archive = [ICSharpCode.SharpZipLib.Tar.TarArchive]::CreateInputTarArchive($tarIn) $archive.ExtractContents($WORKDIR) } finally { $archive.Close } 

which is equivalent to the TarTool.exe utility code

+1
source share

This snippet works for me, I don’t think I have something that it uses. I still need to figure out how to extract tar for this to work out.

 Function DeGZip-File{ Param( $infile ) $outFile = $infile.Substring(0, $infile.LastIndexOfAny('.')) $input = New-Object System.IO.FileStream $inFile, ([IO.FileMode]::Open), ([IO.FileAccess]::Read), ([IO.FileShare]::Read) $output = New-Object System.IO.FileStream $outFile, ([IO.FileMode]::Create), ([IO.FileAccess]::Write), ([IO.FileShare]::None) $gzipStream = New-Object System.IO.Compression.GzipStream $input, ([IO.Compression.CompressionMode]::Decompress) $buffer = New-Object byte[](1024) while($true){ $read = $gzipstream.Read($buffer, 0, 1024) if ($read -le 0){break} $output.Write($buffer, 0, $read) } $gzipStream.Close() $output.Close() $input.Close() } $infile='D:\[Directory]\[File].tar.gz' DeGZip-File $infile 
0
source share

On Windows 10, starting in 2017, tar -xkf $archivePath -C $outDir tar so it can be run as usual exe from powershell e.g. tar -xkf $archivePath -C $outDir or better from pwsh .

0
source share

Why don't you use 7zip?

If there is a business rule that refutes such tools, trying to implement a workaround is a crazy order. In such an environment, you need to talk with interested parties and either:

  • get permission to use the necessary tools
  • move the process to another system where the use of the tool is allowed.
  • change the source system to send unarged files
  • explain that the requirements as such cannot be satisfied
-2
source share

All Articles