Golang使用archive/zip包實現(xiàn)ZIP壓縮與解壓
實現(xiàn)壓縮功能
1、首先需要創(chuàng)建一個 zip 文件。
zip 文件也是一個文件,首先需要創(chuàng)建一個基礎的 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)建一個新的 *Writer 對象,用于將待壓縮的文件寫入到 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)建一個新的 *Writer 對象 zipWriter := zip.NewWriter(zipFile) }
3、向 zip 文件中添加第一個文件
創(chuàng)建完 zip writer 后,向 zip 文件中添加一個文件
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)建一個新的 *Writer 對象 zipWriter := zip.NewWriter(zipFile) //第三步,向 zip 文件中添加第一個文件 // //向 zip 文件中添加一個文件,返回一個待壓縮的文件內(nèi)容應寫入的 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() //需要壓縮更多文件到壓縮包的話,只需要重復第三步即可,只需要更換下文件名 if _, err := io.Copy(w, f); err != nil { panic(err) } }
4、關閉 zip writer,將所有數(shù)據(jù)寫入指向基礎 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)建一個新的 *Writer 對象 zipWriter := zip.NewWriter(zipFile) //第三步,向 zip 文件中添加第一個文件 // //向 zip 文件中添加一個文件,返回一個待壓縮的文件內(nèi)容應寫入的 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() //需要壓縮更多文件到壓縮包的話,只需要重復第三步即可,只需要更換下文件名 if _, err := io.Copy(w, f); err != nil { panic(err) } // 第四步,關閉 zip writer,將所有數(shù)據(jù)寫入指向基礎 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)建對應文件夾 if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil { panic(err) } // 解壓到的目標文件 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) } // 寫入到解壓到的目標文件 if _, err := io.Copy(dstFile, file); err != nil { panic(err) } dstFile.Close() file.Close() } }
到此這篇關于Golang使用archive/zip包實現(xiàn)ZIP壓縮與解壓的文章就介紹到這了,更多相關Go ZIP壓縮與解壓內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!