dongqin5604 2017-04-12 07:59
浏览 29
已采纳

如何在压缩文件的同时获取tar中文件的所需路径

I have been using this code to write to a tar file. I am calling it like err = retarIt(dirTopDebug, path), where dirTopDebug is the path to my tar file (/tmp/abc.tar), and path is the path of files I want to add (/tmp/xyz/...). When I am untarring the generated tar file, I find that inside abc.tar files are put in /tmp/xyz/.. format. But I want them inside tar like xyz/..., i.e. without the tmp folder.

How can I do that?

func TarGzWrite(_path string, tw *tar.Writer, fi os.FileInfo) {
    fr, _ := os.Open(_path)
    //handleError(err)
    defer fr.Close()

    h := new(tar.Header)
    h.Name = _path
    h.Size = fi.Size()
    h.Mode = int64(fi.Mode())
    h.ModTime = fi.ModTime()

    err := tw.WriteHeader(h)
    if err != nil {
        panic(err)
    }

    _, _ = io.Copy(tw, fr)
    //handleError( err )
}

func IterDirectory(dirPath string, tw *tar.Writer) {
    dir, _ := os.Open(dirPath)
    //handleError( err )
    defer dir.Close()
    fis, _ := dir.Readdir(0)
    //handleError( err )
    for _, fi := range fis {
        fmt.Println(dirPath)
        curPath := dirPath + "/" + fi.Name()
        if fi.IsDir() {
            //TarGzWrite( curPath, tw, fi )
            IterDirectory(curPath, tw)
        } else {
            fmt.Printf("adding... %s
", curPath)
            TarGzWrite(curPath, tw, fi)
        }
    }
}

func retarIt(outFilePath, inPath string) error {
    fw, err := os.Create(outFilePath)
    if err != nil {
            return err
    }
    defer fw.Close()
    gw := gzip.NewWriter(fw)
    defer gw.Close()

    // tar write
    tw := tar.NewWriter(gw)
    defer tw.Close()

    IterDirectory(inPath, tw)
    fmt.Println("tar.gz ok")
    return nil
}
  • 写回答

1条回答 默认 最新

  • doufeinai6081 2017-04-12 10:24
    关注

    Whatever name is specified in the tar header, is used. Use the strings.LastIndex (or strings.Index) function of the strings package to separate the part till /tmp.

    So if the code in TarGzWrite function above is modified as follows it works the way you want (note: you may want to replace strings.LastIndex below with strings.Index).

    //TarGzWrite function same as above....
    h := new(tar.Header)
    //New code after this..
    lastIndex := strings.LastIndex(_path, "/tmp")
    fmt.Println("String is ", _path, "Last index is", lastIndex)
    var name string
    if lastIndex > 0 {
        name = _path[lastIndex+len("/tmp")+1:]
        fmt.Println("Got name:", name)
    } else {
        //This would not be needed, but was there just for testing my code
        name = _path
    }
    // h.Name = _path
    h.Name = name
    h.Size = fi.Size()
    h.Mode = int64(fi.Mode())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵