// unzipFile extracts the given source file to the destination folder
unzipFile extracts the given source file to the destination folder
// unzipFile extracts the given source file to the destination folder
func unzipFile(src, dest string) (result model.Result) {
	reader, err := zip.OpenReader(src)
	if err != nil {
		result.WasSuccessful = false
		result.Message = err.Error()
		return result
	}
	defer reader.Close()
	for _, f := range reader.File {
		rc, err := f.Open()
		if err != nil {
			result.WasSuccessful = false
			result.Message = err.Error()
			return result
		}
		defer rc.Close()
		path := filepath.Join(dest, f.Name)
		if f.FileInfo().IsDir() {
			os.MkdirAll(path, os.ModePerm)
		} else {
			var fileDir string
			if lastIndex := strings.LastIndex(path, string(os.PathSeparator)); lastIndex > -1 {
				fileDir = path[:lastIndex]
			}
			err = os.MkdirAll(fileDir, os.ModePerm)
			if err != nil {
				result.WasSuccessful = false
				result.Message = err.Error()
				return result
			}
			f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
			if err != nil {
				result.WasSuccessful = false
				result.Message = err.Error()
				return result
			}
			defer f.Close()
			_, err = io.Copy(f, rc)
			if err != nil {
				result.WasSuccessful = false
				result.Message = err.Error()
				return result
			}
		}
	}
	result.WasSuccessful = true
	result.Message = "unpacked"
	return result
}
func unzipFile ( src , dest string ) ( result model . Result ) { reader , err := zip . OpenReader ( src ) if err != nil { result . WasSuccessful = false result . Message = err . Error ( ) return result } defer reader . Close ( ) for _ , f := range reader . File { rc , err := f . Open ( ) if err != nil { result . WasSuccessful = false result . Message = err . Error ( ) return result } defer rc . Close ( ) path := filepath . Join ( dest , f . Name ) if f . FileInfo ( ) . IsDir ( ) { os . MkdirAll ( path , os . ModePerm ) } else { var fileDir string if lastIndex := strings . LastIndex ( path , string ( os . PathSeparator ) ) ; lastIndex > - 1 { fileDir = path [ : lastIndex ] } err = os . MkdirAll ( fileDir , os . ModePerm ) if err != nil { result . WasSuccessful = false result . Message = err . Error ( ) return result } f , err := os . OpenFile ( path , os . O_WRONLY | os . O_CREATE | os . O_TRUNC , f . Mode ( ) ) if err != nil { result . WasSuccessful = false result . Message = err . Error ( ) return result } defer f . Close ( ) _ , err = io . Copy ( f , rc ) if err != nil { result . WasSuccessful = false result . Message = err . Error ( ) return result } } } result . WasSuccessful = true result . Message = " " return result }
unzipFile extracts the given source file to the destination folder
unzipFile extracts the given source file to the destination folder
// Create a reader for the specified file // Range over the content of the archive // Check if the current file points to a file or a folder // Create the folder // Create the file