gitee.com/quant1x/gox@v1.21.2/api/embed.go (about) 1 package api 2 3 import ( 4 "embed" 5 "io" 6 "io/fs" 7 "os" 8 "time" 9 ) 10 11 // OpenEmbed 打开嵌入式文件 12 func OpenEmbed(resources embed.FS, filename string) (fs.File, error) { 13 reader, err := resources.Open(filename) 14 if err != nil { 15 return nil, err 16 } 17 return reader, nil 18 } 19 20 // Export 导出内嵌资源文件 21 func Export(resources embed.FS, source, target string) error { 22 src, err := OpenEmbed(resources, source) 23 if err != nil { 24 return err 25 } 26 output, err := os.Create(target) 27 if err != nil { 28 return err 29 } 30 _, err = io.Copy(output, src) 31 if err != nil { 32 return err 33 } 34 var mtime time.Time 35 fileinfo, err := src.Stat() 36 if err != nil || fileinfo.ModTime().IsZero() { 37 mtime = time.Now() 38 } else { 39 mtime = fileinfo.ModTime() 40 } 41 err = os.Chtimes(target, mtime, mtime) 42 return err 43 }