打开 _rels/.rels:权限被拒绝 golang

huangapple go评论113阅读模式
英文:

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

huangapple
  • 本文由 发表于 2016年3月17日 17:49:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/36056921.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定