[英]How can I convert a string into a GZIP Base64 string?
I've been trying to figure out using GZIPOutputStream
's and the like but have had no success with understanding them.我一直在尝试使用
GZIPOutputStream
之类的东西,但在理解它们方面没有成功。 All I want to do is convert a string of characters - "A string of characters" into a GZIP Base64 format.我想要做的就是将一串字符——“一串字符”转换成 GZIP Base64 格式。 How can I do this?
我怎样才能做到这一点?
By GZIP Base64 format, I mean the string is first compressed using GZIP, then converted into Base64.通过 GZIP Base64 格式,我的意思是字符串首先使用 GZIP 压缩,然后转换为 Base64。
Use the Apache Commons Codec Base64OutputStream
.使用Apache Commons Codec
Base64OutputStream
。
Here's a sample class:这是一个示例类:
import java.util.zip.GZIPOutputStream;
import org.apache.commons.codec.binary.Base64OutputStream;
public class Test {
public static void main(String[] args) {
String text = "a string of characters";
try {
Base64OutputStream b64os = new Base64OutputStream(System.out);
GZIPOutputStream gzip = new GZIPOutputStream(b64os);
gzip.write(text.getBytes("UTF-8"));
gzip.close();
b64os.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
Which outputs:哪些输出:
H4sIAAAAAAAAAEtUKC4pysxLV8hPU0jOSCxKTC5JLSoGAOP+cfkWAAAA
Under Linux, you can confirm this works with:在 Linux 下,您可以确认这适用于:
echo 'H4sIAAAAAAAAAEtUKC4pysxLV8hPU0jOSCxKTC5JLSoGAOP+cfkWAAAA' | base64 -d | gunzip
(Please note that on OSX, you should use base64 -D
instead of base64 -d
in the above command) (请注意,在 OSX 上,您应该在上述命令中使用
base64 -D
而不是base64 -d
)
Which outputs:哪些输出:
a string of characters
我们可以使用Java GZIPOutputStream/GZIInputStream 和apache commons codec Base64 Encoder and Decoder: lifelongprogrammer.blogspot.com
Base64
- Since: 1.8Base64
- 自:1.8readAllBytes
- Since: 1.9readAllBytes
- 自:1.9GZIPInputStream
and GZIPOutputStream
- Since: 1.1GZIPInputStream
和GZIPOutputStream
- 自:1.1public static void main(String[] args) throws IOException {
byte[] original = "string".getBytes(StandardCharsets.UTF_8);
// encode
byte[] gzipToBase64 = encodeToBase64(encodeToGZIP(original));
System.out.println(new String(gzipToBase64));
//H4sIAAAAAAAAACsuKcrMSwcAqbK+ngYAAAA=
// decode
byte[] fromBase64Gzip = decodeFromGZIP(decodeFromBase64(gzipToBase64));
System.out.println(Arrays.equals(original, fromBase64Gzip)); //true
}
public static byte[] decodeFromBase64(byte[] arr) {
return Base64.getDecoder().decode(arr);
}
public static byte[] encodeToBase64(byte[] arr) {
return Base64.getEncoder().encode(arr);
}
public static byte[] decodeFromGZIP(byte[] arr) throws IOException {
ByteArrayInputStream bais = new ByteArrayInputStream(arr);
GZIPInputStream gzip = new GZIPInputStream(bais);
return gzip.readAllBytes();
}
public static byte[] encodeToGZIP(byte[] arr) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(baos);
gzip.write(arr);
gzip.finish();
return baos.toByteArray();
}
See also: Encoding as Base64 in Java另请参阅:在 Java 中编码为 Base64
This is a variation of this answer but without the deprecated stuff and using Vavr Try
:这是此答案的变体,但没有弃用的内容并使用 Vavr
Try
:
public static Try<String> compress(String input) {
return Try.of(() -> {
ByteArrayOutputStream rstBao = new ByteArrayOutputStream();
GZIPOutputStream zos = new GZIPOutputStream(rstBao);
zos.write(input.getBytes(StandardCharsets.UTF_8));
zos.close();
return Base64.encodeBase64String(rstBao.toByteArray());
});
public static Try<String> decompress(String input) {
return Try.of(() -> IOUtils.toString(new GZIPInputStream(
new ByteArrayInputStream(Base64.decodeBase64(input))),
StandardCharsets.UTF_8));
}
}
// unit test
@Test
public void testStringCompression() {
var data = "I finally rest. And watch the sun rise on a grateful universe. The hardest choices require the strongest wills.";
var cs = Utilities.compress(data);
assertThat(cs.isSuccess());
var us = Utilities.decompress(cs.get());
assertThat(us.isSuccess() && us.get().equals(data));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.