search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

golang 解開zip壓縮檔並讀取資料

golang 解開zip壓縮檔並讀取資料

 

golang的高效能讓它近來都被當作API使用,但其實他還有許多功能可以運用,這次來跟大家分享使用golang解壓縮和讀取檔案內資料的功能

 

package main

// import 相關套件

import (

    "archive/zip"

    "fmt"

    "io"

    "bufio"

)

  // 使用OpenReader解開zip壓縮檔

     zipFile, err := zip.OpenReader("sample.zip")

  // 如果解開zip壓縮檔錯誤顯示Error訊息

     if err != nil {

         fmt.Println(err)

     }

  // 使用完畢後,關閉zip

     defer zipFile.Close()

 

  // 逐一讀取zip檔案的內容

     for _, f := range zipFile.File {

    // 顯示檔案名稱   

         fmt.Println("FileName : ", f.Name)

    // 打開檔案

         fileContent, err := f.Open()

    // 如果打開檔案錯誤則顯示Error訊息

         if err != nil {

              fmt.Println(err)

         }

    // 使用NewReader讀取檔案

         file_line := bufio.NewReader(fileContent)

    // 逐行讀取內容

         for {

      // 逐行讀取內容並把內容放入 line_content

              line_content, _, readError := file_line.ReadLine()

      // 如果讀取到最後一行則停止執行

              if readError == io.EOF {

                  break

              }

      // 顯示該行內容

              fmt.Println("content : ", string(line_content))

         }

     }

}

 

以上就是golang解壓縮檔案並讀取資料的方法 

如果覺得對你有幫助的話請幫小弟按個讚吧~

 

golang相關文章:

使用Go建立http server

 



熱門推薦

本文由 newaurorapixnetnetblog 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦