gpt4 book ai didi

java - 将压缩的 Base64 字符串转换为其原始文件

转载 作者:行者123 更新时间:2023-12-01 10:40:26 25 4
gpt4 key购买 nike

我有一个 Microsoft Word 文件的压缩 base64 字符串。如何在java中将这个压缩的base64字符串转换为其原始文件。

我已经尝试过这段代码,但没有成功。这是我正在尝试的代码

public static void main(String[] args) throws IOException, DataFormatException {

File file = new File(outputFileName);

byte[] zipData = Base64.decodeBase64(compressed_base64_string);
GZIPInputStream zi = new GZIPInputStream(new ByteArrayInputStream(zipData));
String result = IOUtils.toString(zi);
zi.close();


InputStream filedata = new ByteArrayInputStream(result.getBytes("UTF-8"));
byte[] buff = new byte[1024];

FileOutputStream fos = new FileOutputStream(file);
while (filedata.read(buff) > 0) {
fos.write(buff);
}
fos.close();
}

此代码生成一个 zip 文件,其中有一些 xml 文件。但 compressed_base64_string 是从 Microsoft Word 文档生成的。我无法从此代码中获取原始文档。请告诉我下一步该怎么做才能获取原始文件

最佳答案

以下代码对我有用

public static void main(String[] args) throws IOException, DataFormatException {
String outputFilePath = "document.docx";
File file = new File(outputFilePath);
FileOutputStream fos = new FileOutputStream(file);
byte[] zipData = Base64.decodeBase64(compressed_base64_string);
GZIPInputStream zi = new GZIPInputStream(new ByteArrayInputStream(zipData));
IOUtils.copy(zi, fos);

fos.close();
zi.close();

}

关于java - 将压缩的 Base64 字符串转换为其原始文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34452519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com