29 lines
664 B
Go
29 lines
664 B
Go
package zipper
|
|
|
|
import (
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
func PrependPath(filename, path string) string {
|
|
if len(path) == 0 {
|
|
return strings.ReplaceAll(strings.ReplaceAll(filename, "\\", "/"), "//", "/")
|
|
}
|
|
return strings.ReplaceAll(strings.ReplaceAll(path+"/"+filename, "\\", "/"), "//", "/")
|
|
}
|
|
|
|
// MakeSurePathExists creates all missing parent directories, given a specific file path.
|
|
func MakeSurePathExists(filepath string, perm os.FileMode) error {
|
|
i := len(filepath)
|
|
for i > 0 && !os.IsPathSeparator(filepath[i-1]) { // Scan backward over element.
|
|
i--
|
|
}
|
|
|
|
var err error
|
|
if i > 1 {
|
|
// Create parent.
|
|
err = os.MkdirAll(filepath[:i-1], perm)
|
|
}
|
|
return err
|
|
}
|