Compress a Folder in PowerShell for use in Linux

Fresh Tar

I wasted quite a bit of time trying to find the correct way to zip a folder in PowerShell so that it could later be extracted in a Linux docker container.

TLDR; Turns out the standard unix tar command is now standard-issue in Windows—no need to use PowerShell functions at all.

 
tar -cvzf myfile.tgz mydir 

Microsoft added this in 2017 —nice!

These didn’t work for me:

  • PowerShell’s Compress-Archive generates Windows path-separators, which Alpine/BusyBox’s version of unzip can’t handle (though Debian’s unzip can). This will be resolved by Microsoft in Version 1.2.3.
  • The Powershell Community Extensions that includes Gzip and Tar does not currently install cleanly on the most recent PowerShell without this workaround
  • I didn’t try GZipStream because I didn’t see an equivalent to Tar.
 
But if you're stuck with zip file with windows paths, this one-liner will rename the extracted files, create the directories and move them to the right location ([credit](https://stackoverflow.com/questions/44490413/how-to-rename-file-name-contains-backslash-in-bash#answer-44490964)):
 
for file in *\\*; do target="${file//\\//}"; mkdir -p "${target%/*}"; mv -v "$file" "$target"; done