Last active
August 11, 2022 14:04
-
-
Save consatan/f93a9b27a82aaec3602776d1c5df5ec6 to your computer and use it in GitHub Desktop.
List 7z files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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