github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/compress/gzip.go (about)

     1  package compress
     2  
     3  import (
     4  	"bytes"
     5  	"compress/gzip"
     6  	"encoding/binary"
     7  	"fmt"
     8  	"io"
     9  	"os"
    10  	"strings"
    11  )
    12  
    13  func GzipCompress(data []byte) ([]byte, error) {
    14  	var res bytes.Buffer
    15  	gz := gzip.NewWriter(&res)
    16  	defer func(gz *gzip.Writer) { _ = gz.Close() }(gz)
    17  	_, err := gz.Write(data)
    18  	_ = gz.Flush()
    19  	if err == nil {
    20  		return res.Bytes(), nil
    21  	} else {
    22  		return nil, err
    23  	}
    24  }
    25  
    26  func GzipDecompress(data []byte) ([]byte, error) {
    27  	var res bytes.Buffer
    28  	_ = binary.Write(&res, binary.LittleEndian, data)
    29  	fmt.Printf("res: %v\n", res.Bytes())
    30  	gz, _ := gzip.NewReader(&res)
    31  	defer func(gz *gzip.Reader) { _ = gz.Close() }(gz)
    32  	d, err := io.ReadAll(gz)
    33  	if strings.Contains(err.Error(), "unexpected EOF") {
    34  		// 解gzip时,读到最后必定是unexpected EOF,这里做特殊处理
    35  		err = nil
    36  	}
    37  	return d, err
    38  }
    39  
    40  // GzipCompressFile 压缩文件Src到Dst
    41  func GzipCompressFile(src string, dst string) error {
    42  	newfile, err := os.Create(dst)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	defer func(newfile *os.File) {
    47  		_ = newfile.Close()
    48  	}(newfile)
    49  
    50  	file, err := os.Open(src)
    51  	if err != nil {
    52  		return err
    53  	}
    54  
    55  	zw := gzip.NewWriter(newfile)
    56  
    57  	filestat, err := file.Stat()
    58  	if err != nil {
    59  		return nil
    60  	}
    61  
    62  	zw.Name = filestat.Name()
    63  	zw.ModTime = filestat.ModTime()
    64  	_, err = io.Copy(zw, file)
    65  	if err != nil {
    66  		return nil
    67  	}
    68  
    69  	_ = zw.Flush()
    70  	if err := zw.Close(); err != nil {
    71  		return nil
    72  	}
    73  	return nil
    74  }
    75  
    76  // GzipDeCompressFile 解压文件Src到Dst
    77  func GzipDeCompressFile(src string, dst string) error {
    78  	file, err := os.Open(src)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	defer func(file *os.File) {
    83  		_ = file.Close()
    84  	}(file)
    85  
    86  	newfile, err := os.Create(dst)
    87  	if err != nil {
    88  		return err
    89  	}
    90  	defer func(newfile *os.File) {
    91  		_ = newfile.Close()
    92  	}(newfile)
    93  
    94  	zr, err := gzip.NewReader(file)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	filestat, err := file.Stat()
   100  	if err != nil {
   101  		return err
   102  	}
   103  
   104  	zr.Name = filestat.Name()
   105  	zr.ModTime = filestat.ModTime()
   106  	_, err = io.Copy(newfile, zr)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	if err := zr.Close(); err != nil {
   112  		return err
   113  	}
   114  	return nil
   115  }