compressText static method
Compresses a string using gzip and encodes it as Base64.
This method first encodes the input string to UTF-8, then compresses it using gzip, and finally encodes the compressed bytes as a Base64 string.
Returns null if:
- The input
valueis empty - An error occurs during compression
Example:
final compressed = Base64Utils.compressText('Hello, World!');
// compressed is a Base64-encoded gzipped string
See also:
- decompressText to reverse this operation
Implementation
static String? compressText(String? value) {
if (value == null || value.isEmpty) {
return null;
}
try {
final List<int> encodedJson = utf8.encode(value);
final List<int> gzipJson = io.gzip.encode(encodedJson);
return base64.encode(gzipJson);
} on Exception {
return null;
}
}