2

How can I create a .zip compressed archive in Windows from the command-line that preserves symlinks?

I'm trying to create .zip file in PowerShell that includes a relative symlink from one file to another file in that archive, but I either encounter an error or the shortcut turns into a non-symlink file after extraction.

mkdir test
cd test
echo 'foo' > bar
cmd /C mklink bar_link bar
cd ..
Compress-Archive -DestinationPath test.zip -Path test

What are the commands I should use to create a .zip file from a directory that contains a relative symlink to a file in the directory to be archived?

9
  • I don't think the zip file format supports symlinks. Oct 4, 2020 at 18:07
  • 1
    Really? tar preserves symlinks.. Oct 5, 2020 at 4:44
  • 1
    I thought as well that zip doesn't support symlinks an was about to comment that yesterday, but before I looked it up and at least the Unix version of zip does support symlinks. I don't know about the windows version though, since symlinks work different on NTFS. Oct 5, 2020 at 5:58
  • 1
    I tested this. In linux, I created a zip archive with a symlink. It works fine. If you decompress it in linux, you get the symlink back again. But if you decompress this same .zip in Windows, then it dumps out the symlink as a file, not a symlink. So, it seems, Windows doesn't translate the symlink into a shortcut; it just outputs the symlink as the file to which it was linking-to. Sep 20, 2021 at 21:18
  • 1
    @Sz I'm sorry but I don't think this is possible. My use-case was for a compressed archive whoose contents was to be extracted onto a drive. So my solution was to add a provision.sh batch script to the .zip file that, when double-clicked in Windows, it creates the shortcut and deletes itself github.com/BusKill/buskill-app/blob/… Aug 14, 2022 at 10:28

1 Answer 1

1

Since Windows 10 (some 17*** build, end of 2017), the 'tar' command has been added natively. So now for preserving symlinks I would suggest something like:

tar -cvzf mybackup.tar.gz folder-to-backup

Then, after unpacking

tar -xvzf mybackup.tar.gz

the extracted folder folder-to-backup has all symlinks remaining.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .