英文:
open _rels/.rels: permission denied golang
问题
我正在尝试解压一个.docx文件。已经提取了名为"[Content_Types].xml"的第一个文件。但是后来遇到了以下错误:
打开frontend/uploads/doc_data/_rels/.rels时出现权限被拒绝的错误。
我们如何为此设置权限?
我使用的解压函数如下:
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
fpath := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, f.Mode())
} else {
var fdir string
if lastIndex := strings.LastIndex(fpath,string(os.PathSeparator)); lastIndex > -1 {
fdir = fpath[:lastIndex]
}
err = os.MkdirAll(fdir, f.Mode())
if err != nil {
log.Fatal(err)
return err
}
f, err := os.OpenFile(
fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
}
return nil
}
英文:
I was trying to unzip a .docx file. First file of name "[Content_Types].xml" has been extracted. But then I encountered an error as follows:
open frontend/uploads/doc_data/_rels/.rels: permission denied
How can we set permission for this?
The unzip function I use is as follows:
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
fpath := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, f.Mode())
} else {
var fdir string
if lastIndex := strings.LastIndex(fpath,string(os.PathSeparator)); lastIndex > -1 {
fdir = fpath[:lastIndex]
}
err = os.MkdirAll(fdir, f.Mode())
if err != nil {
log.Fatal(err)
return err
}
f, err := os.OpenFile(
fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
}
return nil
}
答案1
得分: 3
尝试使用os包的Chmod函数将文件权限设置为允许你进行所需操作的权限。
err := os.Chmod("file.txt", 0777)
if err != nil {
fmt.Println(err)
}
更多信息请参考:https://socketloop.com/tutorials/golang-change-file-read-or-write-permission-example
英文:
Try using the Chmod function of the os package to set the file permissions to something that allows you to do what you want.
err := os.Chmod("file.txt", 0777)
if err != nil {
fmt.Println(err)
}
more info here : https://socketloop.com/tutorials/golang-change-file-read-or-write-permission-example
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论