GolangNote

Golang笔记

GoLang 解压缩zip 文件的例子

Permalink

Golang 使用标准模块archive/zip 解压缩zip 文件的例子

GoLang 解压缩zip 文件

Go: zip
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main

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

const (
    LOGFILEPATH = "./zip.log"
)

func main() {
    logfile, err := os.OpenFile(LOGFILEPATH, os.O_CREATE|os.O_RDWR, 0)
    if err != nil {
        fmt.Println(err.Error())
        return
    }
    defer logfile.Close()
    logger := log.New(logfile, "\r\n", log.Ldate|log.Ltime|log.Llongfile)
    if logger == nil {
        fmt.Println("logger init error")
    }
    r, err := zip.OpenReader("./file.zip")
    if err != nil {
        logger.Fatal(err)
    }
    defer r.Close()
    for _, f := range r.File {
        fmt.Println("FileName : ", f.Name)
        rc, err := f.Open()
        if err != nil {
            logger.Fatal(err)
        }
        _, err = io.CopyN(os.Stdout, rc, 68) //打印文件内容
        if err != nil {
            if err != io.EOF {
                logger.Fatal(err)
            }
        }
    }
}

Related articles

Write a Comment to "GoLang 解压缩zip 文件的例子"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.16.5 Processed in 0ms