Why can't I extract a Gzip file using PowerShell's tar.exe?
Asked by
72 migration log
I'm trying to extract a Gzip file (.gz) using PowerShell's tar command but keep running into issues. I successfully extracted the contents with 7-zip, but I'd prefer to use a built-in tool since my script will be shared and not everyone has 7-zip installed. When I run the command `tar -xf c:tmptest.gz -C c:tmp`, I get the error: `tar.exe: Error opening archive: Unrecognized archive format`. Interestingly, I can extract the file with Windows File Explorer just fine. My technical background isn't strong, but I know enough to get by. I performed all tests directly in PowerShell. By the way, here's the file structure I'm working with:
* Example.gz
* ConfigData (no file extension)
Answers
Have you thought of trying UNIX-style forward slashes in your path? Sometimes it works better, though I didn't have any luck with it when I tested. Worth a shot!
Reply to this answer
Just to clarify, the tar youโre using in PowerShell is actually a BSD port of tar that Microsoft included, separate from PowerShell itself. When using File Explorer, it uses native OS calls to extract files. If you want to extract Gzip files, it might be worth exploring using .NET APIs within PowerShell instead.
Reply to this answer
Just a note of caution: The `Expand-Archive` cmdlet can sometimes give trouble with files that have non-ASCII characters. I ran into that myself before opting to stick with 7z for reliability.
Reply to this answer
You might want to try using the `-xzf` option instead, which should work for Gzip files. When I tried `tar -xzf c:tmptest.gz -C c:tmp`, I got an error as well, saying 'Unrecognized archive format.' Looks like this option is just not being accepted in the current setup. It's frustrating because the native tools work perfectly fine.
Reply to this answer
If `tar` isnโt working, have you considered using `Expand-Archive`? Although itโs part of the Microsoft.PowerShell.Archive module, it usually only supports .zip files. Unfortunately, it looks like .gz files arenโt supported. Just a heads up, I tried it and got an error saying '.gz is not a supported archive file format.' So you might be out of luck there.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions