doumei9589 2016-07-19 09:41
浏览 338
已采纳

尝试从文件和目录创建tar.gz文件时出现“写得太长”错误

So I'm trying to crate a tar.gz file file from multiple directories and files. Something with the same usage as:

tar -cvzf sometarfile.tar.gz somedir/ someotherdir/ somefile.json somefile.xml

Assuming the directories have other directories inside of them. I have this as an input:

    paths := []string{
      "somedir/",
      "someotherdir/",
      "somefile.json",
      "somefile.xml",
    }

and using these:

    func TarFilesDirs(paths []string, tarFilePath string ) error {
       // set up the output file
       file, err := os.Create(tarFilePath)
       if err != nil {
           return err
       }

       defer file.Close()
       // set up the gzip writer
       gz := gzip.NewWriter(file)
       defer gz.Close()

       tw := tar.NewWriter(gz)
       defer tw.Close()

       // add each file/dir as needed into the current tar archive
       for _,i := range paths {
          if err := tarit(i, tw); err != nil {
               return err
          }
       }

       return nil
   }

func tarit(source string, tw *tar.Writer) error {
    info, err := os.Stat(source)
    if err != nil {
        return nil
    }

    var baseDir string
    if info.IsDir() {
        baseDir = filepath.Base(source)
    }

    return filepath.Walk(source,
        func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return err
            }

            header, err := tar.FileInfoHeader(info, info.Name())
            if err != nil {
                return err
            }

            if baseDir != "" {
                header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, source))
            }

            if err := tw.WriteHeader(header); err != nil {
                return err
            }

            if info.IsDir() {
                return nil
            }

            file, err := os.Open(path)
            if err != nil {
                return err
            }

            defer file.Close()

            _, err = io.Copy(tw, file)
            if err != nil {
                log.Println("failing here")
                return err
            }

            return err
        })
}

Problem: if a directory is large I'm getting:

archive/tar: write too long

error, when I remove it everything works.

Ran out of ideas and wasted many hours on this trying to find a solution...

Any ideas?

Thanks

  • 写回答

2条回答 默认 最新

  • doutongfu9484 2016-10-12 16:23
    关注

    I was having a similar issue until I looked more closely at the tar.FileInfoHeader doc:

    FileInfoHeader creates a partially-populated Header from fi. If fi describes a symlink, FileInfoHeader records link as the link target. If fi describes a directory, a slash is appended to the name. Because os.FileInfo's Name method returns only the base name of the file it describes, it may be necessary to modify the Name field of the returned header to provide the full path name of the file.

    Essentially, FileInfoHeader isn't guaranteed to fill out all the header fields before you write it with WriteHeader, and if you look at the implementation the Size field is only set on regular files. Your code snippet seems to only handle directories, this means if you come across any other non regular file, you write the header with a size of zero then attempt to copy a potentially non-zero sized special file on disk into the tar. Go returns ErrWriteTooLong to stop you from creating a broken tar.

    I came up with this and haven't had the issue since.

        if err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error {
            if err != nil {
                return check(err)
            }
    
            var link string
            if info.Mode()&os.ModeSymlink == os.ModeSymlink {
                if link, err = os.Readlink(path); err != nil {
                    return check(err)
                }
            }
    
            header, err := tar.FileInfoHeader(info, link)
            if err != nil {
                return check(err)
            }
    
            header.Name = filepath.Join(baseDir, strings.TrimPrefix(path, directory))
            if err = tw.WriteHeader(header); err != nil {
                return check(err)
            }
    
            if !info.Mode().IsRegular() { //nothing more to do for non-regular
                return nil
            }
    
            fh, err := os.Open(path)
            if err != nil {
                return check(err)
            }
            defer fh.Close()
    
            if _, err = io.CopyBuffer(tw, fh, buf); err != nil {
                return check(err)
            }
            return nil
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)