[Python] 纯文本查看 复制代码 import zlib
import base64
def compress_and_encode_base64(input_string):
# Convert the input string to bytes
input_bytes = input_string.encode('utf-8')
# Compress the byte array using zlib
compressed_data = zlib.compress(input_bytes)
# Encode the compressed data to Base64
base64_encoded_result = base64.b64encode(compressed_data).decode('utf-8')
return base64_encoded_result
# Example usage
input_string = "Hello, this is a test string to be compressed!"
base64_result = compress_and_encode_base64(input_string)
# Print the result
print(base64_result)
|