Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to zip / archive hidden files using Compress-Archive?

When zipping folders using Compress-Archive it skips all hidden files.

The Documentation page tells us that this cmdlet uses Microsoft .NET Framework API System.IO.Compression.ZipArchive in the background.

Is there some way to force it to archive hidden files? I cannot find this issue documented anywhere. I tried -Force for the heck of it, didn't help.

My current workaround is to use Set-FileAttribute to remove the hidden attribute before zipping.

like image 995
GuidedHacking Avatar asked Nov 30 '18 04:11

GuidedHacking


2 Answers

I replaced Compress-Archive with \[System.IO.Compression.ZipFile]::CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName) and found (on macOS at least) that the ZIP-file included a directory that starts with . (which is one way to hide a directory/file on macOS). This is using PowerShell 7.2.

like image 81
Michiel van Oosterhout Avatar answered Sep 19 '22 06:09

Michiel van Oosterhout


I've just had the same issue and this is how I got aroung it

# WARNING: This only works on a windows based powershell core terminal instance.
# In a linux based powershell core instance the .dot files are not included in 
# the final archive when using the -Filter example

# This will include .files like .gitignore
Get-ChildItem -Path . -Filter *.* | Compress-Archive -DestinationPath dist.zip

# This will include .dot files like .gitignore and any that are hidden like .git
Get-ChildItem -Path . -Force | Compress-Archive -DestinationPath dist.zip
like image 37
Pr1nz Avatar answered Sep 20 '22 06:09

Pr1nz