Skip to content

Commit

Permalink
preserve symlinks (fixes mholt#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterpostmann committed Mar 7, 2018
1 parent 26cf5bb commit 8ea5dc0
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@ func unzipAll(r *zip.Reader, destination string) error {
}

func unzipFile(zf *zip.File, destination string) error {

path := filepath.Join(destination, zf.Name)

if strings.HasSuffix(zf.Name, "/") {
return mkdir(filepath.Join(destination, zf.Name))
return mkdir(path)
}

rc, err := zf.Open()
Expand All @@ -197,7 +200,19 @@ func unzipFile(zf *zip.File, destination string) error {
}
defer rc.Close()

return writeNewFile(filepath.Join(destination, zf.Name), rc, zf.FileInfo().Mode())
if zf.FileInfo().Mode()&os.ModeSymlink != 0 {
buffer := make([]byte, zf.FileInfo().Size())
size, err := rc.Read(buffer)
if err != nil {
return err
}

target := string(buffer[:size])

return writeNewSymbolicLink(path, target)
} else {
return writeNewFile(path, rc, zf.FileInfo().Mode())
}
}

// compressedFormats is a (non-exhaustive) set of lowercased
Expand Down

0 comments on commit 8ea5dc0

Please sign in to comment.