This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 390
The symbolic links are not preserved when unpacking a zip (other formats not tested) #21
Labels
Comments
Same here. @Skywalker13 did you find solution for this? |
Yes but I use package main
import (
"archive/zip"
"io"
"os"
"path/filepath"
)
// Unzip a file to a destination and preserve the symlinks
// Based on http://stackoverflow.com/a/24792688/842097 with symlink additions
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll(dest, 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else if f.FileInfo().Mode()&os.ModeSymlink != 0 {
buffer := make([]byte, f.FileInfo().Size())
size, err := rc.Read(buffer)
if err != nil {
return err
}
target := string(buffer[:size])
err = os.Symlink(target, path)
if err != nil {
return err
}
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err = f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
} |
Hmmm, want to make a pull request? :D |
@Skywalker13 really appreciate actual example - saves me a lot of time! If you will have an hour of free time or so, as @mholt said - PR would be awesome! Thanks again! |
oaleynik
added a commit
to oaleynik/nwjs-autoupdater
that referenced
this issue
Jun 14, 2017
mholt/archiver package can't currently handle symbolic links This adds custom zip extraction based on mholt/archiver#21 (comment) Closes #8
oaleynik
added a commit
to oaleynik/nwjs-autoupdater
that referenced
this issue
Jun 14, 2017
mholt/archiver package can't currently handle symbolic links This adds custom zip extraction based on mholt/archiver#21 (comment) Closes #8
peterpostmann
added a commit
to peterpostmann/archiver
that referenced
this issue
Mar 7, 2018
peterpostmann
added a commit
to peterpostmann/archiver
that referenced
this issue
Mar 7, 2018
peterpostmann
added a commit
to peterpostmann/archiver
that referenced
this issue
Mar 7, 2018
Closed
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
See subject
The text was updated successfully, but these errors were encountered: