"unzip zip file with folders golang" Code Answer's
You're definitely familiar with the best coding language Go that developers use to develop their projects and they get all their queries like "unzip zip file with folders golang" answered properly. Developers are finding an appropriate answer about unzip zip file with folders golang related to the Go coding language. By visiting this online portal developers get answers concerning Go codes question like unzip zip file with folders golang. Enter your desired code related query in the search bar and get every piece of information about Go code related question on unzip zip file with folders golang.
unzip zip file with folders golang
package main
import (
"archive/zip"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
files, err := Unzip("done.zip", "output-folder")
if err != nil {
log.Fatal(err)
}
fmt.Println("Unzipped:\n" + strings.Join(files, "\n"))
}
// Unzip will decompress a zip archive, moving all files and folders
// within the zip file (parameter 1) to an output directory (parameter 2).
func Unzip(src string, dest string) ([]string, error) {
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
// Store filename/path for returning and using later on
fpath := filepath.Join(dest, f.Name)
// Check for ZipSlip. More Info: http://bit.ly/2MsjAWE
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return filenames, fmt.Errorf("%s: illegal file path", fpath)
}
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
// Make Folder
os.MkdirAll(fpath, os.ModePerm)
continue
}
// Make File
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return filenames, err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
rc, err := f.Open()
if err != nil {
return filenames, err
}
_, err = io.Copy(outFile, rc)
// Close the file without defer to close before next iteration of loop
outFile.Close()
rc.Close()
if err != nil {
return filenames, err
}
}
return filenames, nil
}
Source: golangcode.com
All those coders who are working on the Go based application and are stuck on unzip zip file with folders golang can get a collection of related answers to their query. Programmers need to enter their query on unzip zip file with folders golang related to Go code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about unzip zip file with folders golang for the programmers working on Go code while coding their module. Coders are also allowed to rectify already present answers of unzip zip file with folders golang while working on the Go language code. Developers can add up suggestions if they deem fit any other answer relating to "unzip zip file with folders golang". Visit this developer's friendly online web community, CodeProZone, and get your queries like unzip zip file with folders golang resolved professionally and stay updated to the latest Go updates.