真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

golang創(chuàng)建壓縮文件

import (
    "os"
    "archive/zip"
    "io"
    "fmt"
    "strings"
)

// ZipFiles compresses one or many files into a single zip archive file.
//壓縮多個文件到一個文件里面
// Param 1: 輸出的zip文件的名字
// Param 2: 需要添加到zip文件里面的文件
//Param 3: 由于file是絕對路徑,打包后可能不是想要的目錄,oldform就是filename中需要被替換的掉的路徑
//Param 4: 要替換成的路徑
func ZipFiles(filename string, files []string, oldform, newform string) error {

    newZipFile, err := os.Create(filename)
    if err != nil {
        return err
    }
    defer newZipFile.Close()

    zipWriter := zip.NewWriter(newZipFile)
    defer zipWriter.Close()

    // 把files添加到zip中
    for _, file := range files {

        zipfile, err := os.Open(file)
        if err != nil {
            return err
        }
        defer zipfile.Close()

        // 獲取file的基礎(chǔ)信息
        info, err := zipfile.Stat()
        if err != nil {
            return err
        }

        header, err := zip.FileInfoHeader(info)
        if err != nil {
            return err
        }

        //使用上面的FileInforHeader() 就可以把文件保存的路徑替換成我們自己想要的了,如下面
        header.Name = strings.Replace(file, oldform, newform, -1)

        // 優(yōu)化壓縮
        // 更多參考see http://golang.org/pkg/archive/zip/#pkg-constants
        header.Method = zip.Deflate

        writer, err := zipWriter.CreateHeader(header)
        if err != nil {
            return err
        }
        if _, err = io.Copy(writer, zipfile); err != nil {
            return err
        }
    }
    return nil

使用方法 一

精河網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)公司等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)建站從2013年成立到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站。

func main() {
    fileList := []string{
        "/Users/Jack/Downloads/QQreceiver/72bian_log/anal.txt",
        "/Users/Jack/Downloads/QQreceiver/72bian_log/config.json",
        "/Users/Jack/Downloads/QQreceiver/72bian_log/xzwx_client/md5.txt",
    }
    //保留原來文件的結(jié)構(gòu)
    err := ZipFiles("/Users/Jack/Downloads/QQreceiver/test.zip", fileList, "","")
    if err != nil {
        fmt.Println(err)
    }
}

// 執(zhí)行之后

//在對應(yīng)目錄下生成了我們定義的zip文件
?  QQreceiver#  ls test.zip
test.zip
?  QQreceiver#  pwd
/Users/Jack/Downloads/QQreceiver
//解壓test.zip
?  QQreceiver# ls
Users    test.zip
?  QQreceiver# ls -R Users
Jack

Users/Jack:
Downloads

Users/Jack/Downloads:
QQreceiver

Users/Jack/Downloads/QQreceiver:
72bian_log

Users/Jack/Downloads/QQreceiver/72bian_log:
anal.txt    config.json xzwx_client

Users/Jack/Downloads/QQreceiver/72bian_log/xzwx_client:
md5.txt
//可以看到test打包的是文件的絕對路徑,如果這個test.zip是我們要更新的程序的目錄,這樣肯定是不能用的

使用方法 二:
替換成我們想要的結(jié)構(gòu)

func main() {
    fileList := []string{
        "/Users/Jack/Downloads/QQreceiver/72bian_log/anal.txt",
        "/Users/Jack/Downloads/QQreceiver/72bian_log/config.json",
        "/Users/Jack/Downloads/QQreceiver/72bian_log/xzwx_client/md5.txt",
    }
    //比如線上的程序的目錄為game ,"/Users/Jack/Downloads/QQreceiver/72bian_log/"下面的文件為我們線game下的程序文件內(nèi)容
    err := ZipFiles("/Users/Jack/Downloads/QQreceiver/test.zip", fileList, "/Users/Jack/Downloads/QQreceiver/72bian_log","game/")
    if err != nil {
        fmt.Println(err)
    }
}

執(zhí)行之后

//在對應(yīng)目錄下生同樣生成了我們定義的zip文件
?  QQreceiver#  ls test.zip
test.zip
?  QQreceiver#  pwd
/Users/Jack/Downloads/QQreceiver
//解壓test.zip
?  QQreceiver# ls
game     test.zip
?  QQreceiver# ls -R game
anal.txt    config.json xzwx_client

game/xzwx_client:
md5.txt
//上面正好是我們需要的目錄

網(wǎng)頁題目:golang創(chuàng)建壓縮文件
文章地址:http://weahome.cn/article/igsojd.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部