Skip to content

Commit 8beed9b

Browse files
committed
Support unzipping symlinks as symlinks on Windows
1 parent 5725d20 commit 8beed9b

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

os/src/ZipOps.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package os
33
import os.{shaded_org_apache_tools_zip => apache}
44

55
import java.net.URI
6-
import java.nio.file.{FileSystem, FileSystems, Files, LinkOption}
6+
import java.nio.file.{FileSystem, FileSystemException, FileSystems, Files, LinkOption}
77
import java.nio.file.attribute.{
88
BasicFileAttributes,
99
BasicFileAttributeView,
@@ -342,12 +342,23 @@ object unzip {
342342

343343
if (zipEntry.isDirectory) {
344344
os.makeDir.all(newFile, perms = perms)
345-
} else if (!isWin && isSymLink(mode)) {
345+
} else if (isSymLink(mode)) {
346346
val target = scala.io.Source.fromInputStream(zipInputStream).mkString
347347
val path = java.nio.file.Paths.get(target)
348348
val dest = if (path.isAbsolute) os.Path(path) else os.RelPath(path)
349349
os.makeDir.all(newFile / os.up)
350-
os.symlink(newFile, dest)
350+
try {
351+
os.symlink(newFile, dest)
352+
} catch {
353+
case _: FileSystemException => {
354+
System.err.println(
355+
s"Failed to create symbolic link ${zipEntry.getName} -> ${target}.\n" +
356+
(if (isWin) "On Windows this might be due to lack of sufficient privilege or file system support.\n" else "") +
357+
"This zip entry will be unzipped as a text file containing the target path."
358+
)
359+
os.write(newFile, target)
360+
}
361+
}
351362
} else {
352363
val outputStream = os.write.outputStream(newFile, createFolders = true)
353364
os.Internals.transfer(zipInputStream, outputStream, close = false)

0 commit comments

Comments
 (0)