Golang使用archive/zip包實(shí)現(xiàn)ZIP壓縮與解壓
實(shí)現(xiàn)壓縮功能
1、首先需要?jiǎng)?chuàng)建一個(gè) zip 文件。
zip 文件也是一個(gè)文件,首先需要?jiǎng)?chuàng)建一個(gè)基礎(chǔ)的 zip 文件,使用 os.Create() 函數(shù)創(chuàng)建即可。
package main import "os" func main() { //第一步,創(chuàng)建 zip 文件 zipFile, err := os.Create("archive.zip") if err != nil { panic(err) } defer zipFile.Close() }
2、使用 zip.NewWriter() 函數(shù)創(chuàng)建一個(gè)新的 *Writer 對(duì)象,用于將待壓縮的文件寫入到 zip 文件。
package main import ( "archive/zip" "os" ) func main() { //第一步,創(chuàng)建 zip 文件 zipFile, err := os.Create("archive.zip") if err != nil { panic(err) } defer zipFile.Close() //第二步,創(chuàng)建一個(gè)新的 *Writer 對(duì)象 zipWriter := zip.NewWriter(zipFile) }
3、向 zip 文件中添加第一個(gè)文件
創(chuàng)建完 zip writer 后,向 zip 文件中添加一個(gè)文件
package main import ( "archive/zip" "io" "os" ) func main() { //第一步,創(chuàng)建 zip 文件 zipFile, err := os.Create("archive.zip") if err != nil { panic(err) } defer zipFile.Close() //第二步,創(chuàng)建一個(gè)新的 *Writer 對(duì)象 zipWriter := zip.NewWriter(zipFile) //第三步,向 zip 文件中添加第一個(gè)文件 // //向 zip 文件中添加一個(gè)文件,返回一個(gè)待壓縮的文件內(nèi)容應(yīng)寫入的 Writer w, err := zipWriter.Create("json.go") if err != nil { panic(err) } // 打開待壓縮的文件 f, err := os.Open("bec/json.go") if err != nil { panic(err) } defer f.Close() //需要壓縮更多文件到壓縮包的話,只需要重復(fù)第三步即可,只需要更換下文件名 if _, err := io.Copy(w, f); err != nil { panic(err) } }
4、關(guān)閉 zip writer,將所有數(shù)據(jù)寫入指向基礎(chǔ) zip 文件的數(shù)據(jù)流
package main import ( "archive/zip" "io" "os" ) func main() { //第一步,創(chuàng)建 zip 文件 zipFile, err := os.Create("archive.zip") if err != nil { panic(err) } defer zipFile.Close() //第二步,創(chuàng)建一個(gè)新的 *Writer 對(duì)象 zipWriter := zip.NewWriter(zipFile) //第三步,向 zip 文件中添加第一個(gè)文件 // //向 zip 文件中添加一個(gè)文件,返回一個(gè)待壓縮的文件內(nèi)容應(yīng)寫入的 Writer w, err := zipWriter.Create("json.go") if err != nil { panic(err) } // 打開待壓縮的文件 f, err := os.Open("bec/json.go") if err != nil { panic(err) } defer f.Close() //需要壓縮更多文件到壓縮包的話,只需要重復(fù)第三步即可,只需要更換下文件名 if _, err := io.Copy(w, f); err != nil { panic(err) } // 第四步,關(guān)閉 zip writer,將所有數(shù)據(jù)寫入指向基礎(chǔ) zip 文件的數(shù)據(jù)流 zipWriter.Close() }
解壓 ZIP 包
1、 使用 zip.OpenReader 打開 zip 包
package main import ( "archive/zip" ) func main() { // 第一步,打開 zip 文件 zipFile, err := zip.OpenReader("archive.zip") if err != nil { panic(err) } defer zipFile.Close() }
2、 遍歷 zip 中的文件,將壓縮包的中的文件讀取到本地文件
package main import ( "archive/zip" "io" "os" "path/filepath" ) func main() { // 第一步,打開 zip 文件 zipFile, err := zip.OpenReader("archive.zip") if err != nil { panic(err) } defer zipFile.Close() // 第二步,遍歷 zip 中的文件 for _, f := range zipFile.File { filePath := f.Name if f.FileInfo().IsDir() { _ = os.MkdirAll(filePath, os.ModePerm) continue } // 創(chuàng)建對(duì)應(yīng)文件夾 if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil { panic(err) } // 解壓到的目標(biāo)文件 dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) if err != nil { panic(err) } file, err := f.Open() if err != nil { panic(err) } // 寫入到解壓到的目標(biāo)文件 if _, err := io.Copy(dstFile, file); err != nil { panic(err) } dstFile.Close() file.Close() } }
到此這篇關(guān)于Golang使用archive/zip包實(shí)現(xiàn)ZIP壓縮與解壓的文章就介紹到這了,更多相關(guān)Go ZIP壓縮與解壓內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
GoLang實(shí)現(xiàn)Viper庫(kù)的封裝流程詳解
Viper是一個(gè)用于Go語言應(yīng)用程序的配置管理庫(kù),它提供了一種簡(jiǎn)單而靈活的方式來處理應(yīng)用程序的配置,支持多種格式的配置文件,這篇文章主要介紹了GoLang封裝Viper庫(kù)的流程,感興趣的同學(xué)可以參考下文2023-05-05Go?iota關(guān)鍵字與枚舉類型實(shí)現(xiàn)原理
這篇文章主要介紹了Go?iota關(guān)鍵字與枚舉類型實(shí)現(xiàn)原理,iota是go語言的常量計(jì)數(shù)器,只能在常量的表達(dá)式中使用,更多相關(guān)內(nèi)容需要的小伙伴可以參考一下2022-07-07