讀取寫入tar/zip檔案(go語言)

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

1.讀取寫入tar檔案

package mainimport (    "archive/tar"    "fmt"    "os"    "bufio"    "io")func readFile(path string) string{    var content string = ""    file,err := os.Open(path)    if err != nil{        fmt.Printf("%v",err)        return ""    }    defer file.Close()    inputReader := bufio.NewReader(file)    for{        inputString,err := inputReader.ReadString('\n')        content += inputString        if err == io.EOF{            break        }    }    return content}func tarPackage(tarFileName string,tarFileList []string,rootPath string){    fw,err := os.Create(rootPath+"/"+tarFileName)    if err != nil{        fmt.Printf("%v",err)        return    }    defer fw.Close()    tw := tar.NewWriter(fw)    defer tw.Close()    for _,fileItem := range tarFileList{        body := readFile(rootPath+"/"+fileItem)        hdr := &tar.Header{            Name:fileItem,            Mode:0600,            Size:int64(len(body)),        }        if err := tw.WriteHeader(hdr);err != nil{            fmt.Printf("%v",err)            return        }        if _,err := tw.Write([]byte(body));err != nil{            fmt.Printf("%v",err)            return        }    }    fmt.Println("done!")}func tarReader(tarFileName string){    fw,err := os.Open(tarFileName)    if err != nil{        fmt.Printf("%v",err)        return    }    defer fw.Close()    tr := tar.NewReader(fw)    for{        hdr,err := tr.Next()        if err == io.EOF{            break        }        if err != nil{            fmt.Printf("%v",err)            continue        }        fmt.Println("filename:"+hdr.Name)    }}func main(){    tarFileName := "test.tar"    //tarFileList := []string{"1","2","3"}    rootPath := "/home/pijing/goworkspace/gocode"    //tarPackage(tarFileName,tarFileList,rootPath)    tarReader(rootPath+"/"+tarFileName)}

2.讀取寫入zip檔案

package mainimport (    "io"    "archive/zip"    "fmt"    "os"    "bufio")func readFile(path string) string{    var content string = ""    file,err := os.Open(path)    if err != nil{        fmt.Printf("%v",err)        return ""    }    defer file.Close()    inputReader := bufio.NewReader(file)    for{        inputString,err := inputReader.ReadString('\n')        content += inputString        if err == io.EOF{            break        }    }    return content}func zipPackage(zipFileName string,zipFileList []string,rootPath string){    fw,err := os.Create(rootPath+"/"+zipFileName)    if err != nil{        fmt.Printf("%v",err)        return    }    defer fw.Close()    zw := zip.NewWriter(fw)    defer zw.Close()    for _,fileItem := range zipFileList{        body := readFile(rootPath+"/"+fileItem)        f,err := zw.Create(fileItem)        if err != nil{            fmt.Printf("%v",err)            continue        }        _,err1 := f.Write([]byte(body))        if err1 != nil{            fmt.Printf("%v",err1)            continue        }    }    fmt.Println("done!")}func zipReader(zipFileName string){    r,err := zip.OpenReader(zipFileName)    if err != nil{        fmt.Printf("%v",err)        return    }    defer r.Close()    for _,f := range r.File{        fmt.Println("file:"+f.Name)    }}func main(){    zipFileName := "test.zip"    // zipFileList := []string{"1","2","3"}    rootPath := "/home/pijing/goworkspace/gocode"    // zipPackage(zipFileName,zipFileList,rootPath)    zipReader(rootPath+"/"+zipFileName)}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.