Skip to content

Instantly share code, notes, and snippets.

@consatan
Last active August 11, 2022 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save consatan/f93a9b27a82aaec3602776d1c5df5ec6 to your computer and use it in GitHub Desktop.
Save consatan/f93a9b27a82aaec3602776d1c5df5ec6 to your computer and use it in GitHub Desktop.
List 7z files
package main
import (
"regexp"
"os"
"os/exec"
)
var pattern = regexp.MustCompile(`(?m)^Path = (.+)$`)
// shell version: 7z l path.7z -ba -slt | grep -oE 'Path = .*' | awk -F' = ' '{print $2}'
func list7zFiles(path string) ([]string, error) {
if _, err := os.Stat(path); err != nil {
return nil, err
}
// -slt switch since v15.xx
cmd := exec.Command("7z", "l", path, "-ba", "-slt")
output, err := cmd.Output()
if err != nil {
return nil, err
}
matches := pattern.FindAllSubmatch(output, -1)
files := make([]string, len(matches))
for i, match := range matches {
files[i] = string(match[1])
}
return files, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment