java base64压缩包转二进制流
时间: 2023-12-01 21:42:17 浏览: 73
以下是Java中将base64压缩包转换为二进制流的示例代码:
```java
import java.io.*;
import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;
import org.apache.commons.codec.binary.Base64;
public class Base64ZipToBinary {
public static void main(String[] args) throws Exception {
// 读取base64字符串
String base64Str = "UEsDBBQACAAIAAAAAAAAAAAAAAAAAAAAAAAAALAAAAdGVzdC50eHRVVAkAA3N5c3RlbS54bWxQSwECFAAUAAgACAAA" +
"AAAAAAAAAAAAAAAAAAAAAAAAsAAAAdGVzdC50eHRVVAUAA3N5c3RlbS54bWxQSwUGAAAAAAEAAQBiAAAAZwAAAAAA";
byte[] bytes = Base64.decodeBase64(base64Str);
// 将base64字符串解压缩为二进制流
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ZipInputStream zis = new ZipInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = zis.read(buffer)) > 0) {
baos.write(buffer, 0, len);
}
byte[] binaryData = baos.toByteArray();
// 输出二进制流
FileOutputStream fos = new FileOutputStream("test.txt");
fos.write(binaryData);
fos.close();
}
}
```
该示例代码中,首先读取了一个base64字符串,然后将其解码为二进制数据。接着,使用ZipInputStream将解码后的数据解压缩为二进制流,并将其输出到文件中。
阅读全文