1

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?

Michael Altfield
  • 525
  • 6
  • 18
  • I don't think the zip file format supports symlinks. – Michael Hampton Oct 04 '20 at 18:07
  • my understanding is that symlinks and hardlinks are _filesystem_ things ... and i know of no archive format that supports anything more complex than directory structure. – Lee_Dailey Oct 04 '20 at 22:12
  • 1
    Really? `tar` preserves symlinks.. – Michael Altfield Oct 05 '20 at 04:44
  • Ok, well, if this is not possible with `.zip` what other native Windows archive formats support symlinks? – Michael Altfield Oct 05 '20 at 04:47
  • 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](https://serverfault.com/questions/265675/how-can-i-zip-compress-a-symlink). I don't know about the windows version though, since symlinks work different on NTFS. – Gerald Schneider Oct 05 '20 at 05:58
  • You could try to use the win32 build of the linux zip tool. – Gerald Schneider Oct 05 '20 at 05:59
  • 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. – Michael Altfield Sep 20 '21 at 21:18
  • @MichaelAltfield did you happen to find a solution eventually? – Sz. Aug 12 '22 at 19:00
  • 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 https://github.com/BusKill/buskill-app/blob/1a79dc07df39a2622ad32e042c21332a34cbf458/build/usb/usbDrive.sh#L206-L264 – Michael Altfield Aug 14 '22 at 10:28

0 Answers0