github.com/enetx/g@v1.0.80/string_compdecomp.go (about)

     1  package g
     2  
     3  import (
     4  	"bytes"
     5  	"compress/flate"
     6  	"compress/gzip"
     7  	"compress/zlib"
     8  	"io"
     9  
    10  	"github.com/andybalholm/brotli"
    11  	"github.com/klauspost/compress/zstd"
    12  )
    13  
    14  type (
    15  	// A struct that wraps a String for compression.
    16  	comp struct{ str String }
    17  
    18  	// A struct that wraps a String for decompression.
    19  	decomp struct{ str String }
    20  )
    21  
    22  // Comp returns a comp struct wrapping the given String.
    23  func (s String) Comp() comp { return comp{s} }
    24  
    25  // Decomp returns a decomp struct wrapping the given String.
    26  func (s String) Decomp() decomp { return decomp{s} }
    27  
    28  // Zstd compresses the wrapped String using the zstd compression algorithm and
    29  // returns the compressed data as a String.
    30  func (c comp) Zstd() String {
    31  	buffer := new(bytes.Buffer)
    32  	writer, _ := zstd.NewWriter(buffer)
    33  
    34  	_, _ = writer.Write(c.str.ToBytes())
    35  	_ = writer.Flush()
    36  	_ = writer.Close()
    37  
    38  	return String(buffer.String())
    39  }
    40  
    41  // Zstd decompresses the wrapped String using the zstd compression algorithm and
    42  // returns the decompressed data as a Result[String].
    43  func (d decomp) Zstd() Result[String] {
    44  	reader, err := zstd.NewReader(d.str.Reader())
    45  	if err != nil {
    46  		reader.Close()
    47  		return Err[String](err)
    48  	}
    49  
    50  	defer reader.Close()
    51  
    52  	buffer := new(bytes.Buffer)
    53  	if _, err := io.Copy(buffer, reader); err != nil {
    54  		return Err[String](err)
    55  	}
    56  
    57  	return Ok(String(buffer.String()))
    58  }
    59  
    60  // Brotli compresses the wrapped String using the Brotli compression algorithm and
    61  // returns the compressed data as a String.
    62  func (c comp) Brotli() String {
    63  	buffer := new(bytes.Buffer)
    64  	writer := brotli.NewWriter(buffer)
    65  
    66  	_, _ = writer.Write(c.str.ToBytes())
    67  	_ = writer.Flush()
    68  	_ = writer.Close()
    69  
    70  	return String(buffer.String())
    71  }
    72  
    73  // Brotli decompresses the wrapped String using the Brotli compression algorithm and
    74  // returns the decompressed data as a Result[String].
    75  func (d decomp) Brotli() Result[String] {
    76  	reader := brotli.NewReader(d.str.Reader())
    77  
    78  	buffer := new(bytes.Buffer)
    79  	if _, err := io.Copy(buffer, reader); err != nil {
    80  		return Err[String](err)
    81  	}
    82  
    83  	return Ok(String(buffer.String()))
    84  }
    85  
    86  // Zlib compresses the wrapped String using the zlib compression algorithm and
    87  // returns the compressed data as a String.
    88  func (c comp) Zlib() String {
    89  	// gzcompress() php
    90  	buffer := new(bytes.Buffer)
    91  	writer := zlib.NewWriter(buffer)
    92  
    93  	_, _ = writer.Write(c.str.ToBytes())
    94  	_ = writer.Flush()
    95  	_ = writer.Close()
    96  
    97  	return String(buffer.String())
    98  }
    99  
   100  // Zlib decompresses the wrapped String using the zlib compression algorithm and
   101  // returns the decompressed data as a Result[String].
   102  func (d decomp) Zlib() Result[String] {
   103  	// gzuncompress() php
   104  	reader, err := zlib.NewReader(d.str.Reader())
   105  	if err != nil {
   106  		return Err[String](err)
   107  	}
   108  
   109  	defer reader.Close()
   110  
   111  	buffer := new(bytes.Buffer)
   112  	if _, err := io.Copy(buffer, reader); err != nil {
   113  		return Err[String](err)
   114  	}
   115  
   116  	return Ok(String(buffer.String()))
   117  }
   118  
   119  // Gzip compresses the wrapped String using the gzip compression format and
   120  // returns the compressed data as a String.
   121  func (c comp) Gzip() String {
   122  	// gzencode() php
   123  	buffer := new(bytes.Buffer)
   124  	writer := gzip.NewWriter(buffer)
   125  
   126  	_, _ = writer.Write(c.str.ToBytes())
   127  	_ = writer.Flush()
   128  	_ = writer.Close()
   129  
   130  	return String(buffer.String())
   131  }
   132  
   133  // Gzip decompresses the wrapped String using the gzip compression format and
   134  // returns the decompressed data as a Result[String].
   135  func (d decomp) Gzip() Result[String] {
   136  	// gzdecode() php
   137  	reader, err := gzip.NewReader(d.str.Reader())
   138  	if err != nil {
   139  		return Err[String](err)
   140  	}
   141  
   142  	defer reader.Close()
   143  
   144  	buffer := new(bytes.Buffer)
   145  	if _, err := io.Copy(buffer, reader); err != nil {
   146  		return Err[String](err)
   147  	}
   148  
   149  	return Ok(String(buffer.String()))
   150  }
   151  
   152  // Flate compresses the wrapped String using the flate (zlib) compression algorithm
   153  // and returns the compressed data as a String.
   154  // It accepts an optional compression level. If no level is provided, it defaults to 7.
   155  func (c comp) Flate(level ...int) String {
   156  	// gzdeflate() php
   157  	buffer := new(bytes.Buffer)
   158  
   159  	l := 7
   160  	if len(level) != 0 {
   161  		l = level[0]
   162  	}
   163  
   164  	writer, _ := flate.NewWriter(buffer, l)
   165  
   166  	_, _ = writer.Write(c.str.ToBytes())
   167  	_ = writer.Flush()
   168  	_ = writer.Close()
   169  
   170  	return String(buffer.String())
   171  }
   172  
   173  // Flate decompresses the wrapped String using the flate (zlib) compression algorithm
   174  // and returns the decompressed data as a Result[String].
   175  func (d decomp) Flate() Result[String] {
   176  	// gzinflate() php
   177  	reader := flate.NewReader(d.str.Reader())
   178  	defer reader.Close()
   179  
   180  	buffer := new(bytes.Buffer)
   181  	if _, err := io.Copy(buffer, reader); err != nil {
   182  		return Err[String](err)
   183  	}
   184  
   185  	return Ok(String(buffer.String()))
   186  }