github.com/wangyougui/gf/v2@v2.6.5/encoding/gcompress/gcompress_gzip.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gcompress
     8  
     9  import (
    10  	"bytes"
    11  	"compress/gzip"
    12  	"io"
    13  
    14  	"github.com/wangyougui/gf/v2/errors/gerror"
    15  	"github.com/wangyougui/gf/v2/os/gfile"
    16  )
    17  
    18  // Gzip compresses `data` using gzip algorithm.
    19  // The optional parameter `level` specifies the compression level from
    20  // 1 to 9 which means from none to the best compression.
    21  //
    22  // Note that it returns error if given `level` is invalid.
    23  func Gzip(data []byte, level ...int) ([]byte, error) {
    24  	var (
    25  		writer *gzip.Writer
    26  		buf    bytes.Buffer
    27  		err    error
    28  	)
    29  	if len(level) > 0 {
    30  		writer, err = gzip.NewWriterLevel(&buf, level[0])
    31  		if err != nil {
    32  			err = gerror.Wrapf(err, `gzip.NewWriterLevel failed for level "%d"`, level[0])
    33  			return nil, err
    34  		}
    35  	} else {
    36  		writer = gzip.NewWriter(&buf)
    37  	}
    38  	if _, err = writer.Write(data); err != nil {
    39  		err = gerror.Wrap(err, `writer.Write failed`)
    40  		return nil, err
    41  	}
    42  	if err = writer.Close(); err != nil {
    43  		err = gerror.Wrap(err, `writer.Close failed`)
    44  		return nil, err
    45  	}
    46  	return buf.Bytes(), nil
    47  }
    48  
    49  // GzipFile compresses the file `src` to `dst` using gzip algorithm.
    50  func GzipFile(srcFilePath, dstFilePath string, level ...int) (err error) {
    51  	dstFile, err := gfile.Create(dstFilePath)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	defer dstFile.Close()
    56  
    57  	return GzipPathWriter(srcFilePath, dstFile, level...)
    58  }
    59  
    60  // GzipPathWriter compresses `filePath` to `writer` using gzip compressing algorithm.
    61  //
    62  // Note that the parameter `path` can be either a directory or a file.
    63  func GzipPathWriter(filePath string, writer io.Writer, level ...int) error {
    64  	var (
    65  		gzipWriter *gzip.Writer
    66  		err        error
    67  	)
    68  	srcFile, err := gfile.Open(filePath)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	defer srcFile.Close()
    73  
    74  	if len(level) > 0 {
    75  		gzipWriter, err = gzip.NewWriterLevel(writer, level[0])
    76  		if err != nil {
    77  			return gerror.Wrap(err, `gzip.NewWriterLevel failed`)
    78  		}
    79  	} else {
    80  		gzipWriter = gzip.NewWriter(writer)
    81  	}
    82  	defer gzipWriter.Close()
    83  
    84  	if _, err = io.Copy(gzipWriter, srcFile); err != nil {
    85  		err = gerror.Wrap(err, `io.Copy failed`)
    86  		return err
    87  	}
    88  	return nil
    89  }
    90  
    91  // UnGzip decompresses `data` with gzip algorithm.
    92  func UnGzip(data []byte) ([]byte, error) {
    93  	var buf bytes.Buffer
    94  	reader, err := gzip.NewReader(bytes.NewReader(data))
    95  	if err != nil {
    96  		err = gerror.Wrap(err, `gzip.NewReader failed`)
    97  		return nil, err
    98  	}
    99  	if _, err = io.Copy(&buf, reader); err != nil {
   100  		err = gerror.Wrap(err, `io.Copy failed`)
   101  		return nil, err
   102  	}
   103  	if err = reader.Close(); err != nil {
   104  		err = gerror.Wrap(err, `reader.Close failed`)
   105  		return buf.Bytes(), err
   106  	}
   107  	return buf.Bytes(), nil
   108  }
   109  
   110  // UnGzipFile decompresses srcFilePath `src` to `dst` using gzip algorithm.
   111  func UnGzipFile(srcFilePath, dstFilePath string) error {
   112  	srcFile, err := gfile.Open(srcFilePath)
   113  	if err != nil {
   114  		return err
   115  	}
   116  	defer srcFile.Close()
   117  	dstFile, err := gfile.Create(dstFilePath)
   118  	if err != nil {
   119  		return err
   120  	}
   121  	defer dstFile.Close()
   122  
   123  	reader, err := gzip.NewReader(srcFile)
   124  	if err != nil {
   125  		err = gerror.Wrap(err, `gzip.NewReader failed`)
   126  		return err
   127  	}
   128  	defer reader.Close()
   129  
   130  	if _, err = io.Copy(dstFile, reader); err != nil {
   131  		err = gerror.Wrap(err, `io.Copy failed`)
   132  		return err
   133  	}
   134  	return nil
   135  }