Gzip Compress
In HTTP, GNUzip(Gzip) compression coding is a way to optimize the performance of Web applications, and Hertz also provides an implementation of Gzip.
Install
Example
Gzip
Gzip Stream
If the user has a need for gzip compression combined with chunked streaming writing, they can use this middleware. The behavior of this middleware is to chunk each chunk, compress it with gzip, and then send it to the client. Each chunk is a separate compressed data, so each chunk received by the client can be independently decompressed and used.
Note: Using this middleware will hijack the response writer and may have an impact on other interfaces. Therefore, it is only necessary to use this middleware on interfaces with streaming gzip requirements.
Config
The following configuration is also applicable to
Gzip Stream
.
Gzip
provides four compression options: BestCompression
, BestSpeed
, DefaultCompression
, NoCompression
for user-defined compression modes
Options | Description |
---|---|
BestCompression | Provides the best file compression ratio |
BestSpeed | Provides the best compression speed |
DefaultCompression | Default compression rate |
NoCompression | No compression |
Function Signature:
Sample Code:
WithExcludedExtensions
gzip
provides WithExcludeExtensions
to help users set file extensions that do not require gzip
compression, the default values are .png
, .gif
, .jpeg
, .jpg
Function Signature:
Sample Code:
WithExcludedPaths
gzip
provides WithExcludedPaths
to help users set the paths of files they do not want to compress with gzip
Function Signature:
Sample Code:
WithExcludedPathRegexes
gzip
provides WithExcludedPathRegexes
to help users set custom regular expressions to filter out files that do not need to be compressed by gzip
Function Signature
Sample Code:
Refer to the gzip for more usage examples