首先,如果我的术语有点业余,我很抱歉,试着忍受我;)
我正在尝试将HTTP响应的gzipped主体转换为纯文本。我已经采用了这个响应的字节数组并将其转换为ByteArrayInputStream。然后我将其转换为GZIPInputStream。我现在想要读取GZIPInputStream并将最终解压缩的HTTP响应体存储为明文字符串。
此代码将最终解压缩的内容存储在OutputStream中,但我想将内容存储为String:
public static int sChunk = 8192;
ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
GZIPInputStream gzis = new GZIPInputStream(bais);
byte[] buffer = new byte[sChunk];
int length;
while ((length = gzis.read(buffer, 0, sChunk)) != -1) {
out.write(buffer, 0, length);
}
答案 0 :(得分:46)
要解码InputStream中的字节,您可以使用InputStreamReader。然后,BufferedReader将允许您逐行读取您的流。
您的代码如下:
ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
GZIPInputStream gzis = new GZIPInputStream(bais);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader in = new BufferedReader(reader);
String readed;
while ((readed = in.readLine()) != null) {
System.out.println(readed);
}
答案 1 :(得分:32)
您应该以InputStream
而不是byte[]
获得响应。然后,您可以使用GZIPInputStream
对其进行解压缩,并使用InputStreamReader
将其作为字符数据读取,最后使用StringWriter
将其作为字符数据写入String
。
String body = null;
String charset = "UTF-8"; // You should determine it based on response header.
try (
InputStream gzippedResponse = response.getInputStream();
InputStream ungzippedResponse = new GZIPInputStream(gzippedResponse);
Reader reader = new InputStreamReader(ungzippedResponse, charset);
Writer writer = new StringWriter();
) {
char[] buffer = new char[10240];
for (int length = 0; (length = reader.read(buffer)) > 0;) {
writer.write(buffer, 0, length);
}
body = writer.toString();
}
// ...
如果您的最终目的是将响应解析为HTML,那么我强烈建议您使用HTML解析器,例如Jsoup。然后就像这样简单:
String html = Jsoup.connect("http://google.com").get().html();
答案 2 :(得分:6)
使用try-with-resources习惯用法(在从块退出时自动关闭在try(...)中打开的所有资源)以使代码更清晰。
使用Apache IOUtils使用默认的CharSet将inputStream转换为String。
import org.apache.commons.io.IOUtils;
public static String gzipFileToString(File file) throws IOException {
try(GZIPInputStream gzipIn = new GZIPInputStream(new FileInputStream(file))) {
return IOUtils.toString(gzipIn);
}
}
答案 3 :(得分:2)
使用Apache Commons将GzipInputStream转换为byteArray。
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import org.apache.commons.io.IOUtils;
public static byte[] decompressContent(byte[] pByteArray) throws IOException {
GZIPInputStream gzipIn = null;
try {
gzipIn = new GZIPInputStream(new ByteArrayInputStream(pByteArray));
return IOUtils.toByteArray(gzipIn);
} finally {
if (gzipIn != null) {
gzipIn.close();
}
}
要将字节数组未压缩的内容转换为String,请执行以下操作:
String uncompressedContent = new String(decompressContent(inputStream));
答案 4 :(得分:1)
您可以使用StringWriter写入字符串
答案 5 :(得分:1)
GZip wiki 是文件格式和用于文件压缩和解压缩的软件应用程序。 gzip是单文件/流无损数据压缩实用程序,其中生成的压缩文件通常具有后缀 .gz
字符串
(Plain)
➢字节➤GZip数据(Compress)
➦字节➥字符串(Decompress)
String zipData = "Hi Stackoverflow and GitHub";
// String to Bytes
byte[] byteStream = zipData.getBytes();
System.out.println("String Data:"+ new String(byteStream, "UTF-8"));
// Bytes to Compressed-Bytes then to String.
byte[] gzipCompress = gzipCompress(byteStream);
String gzipCompressString = new String(gzipCompress, "UTF-8");
System.out.println("GZIP Compressed Data:"+ gzipCompressString);
// Bytes to DeCompressed-Bytes then to String.
byte[] gzipDecompress = gzipDecompress(gzipCompress);
String gzipDecompressString = new String(gzipDecompress, "UTF-8");
System.out.println("GZIP Decompressed Data:"+ gzipDecompressString);
GZip字节
(Compress)
➥文件(*.gz)
➥字符串(Decompress)
GZip文件扩展名 .gz ,Internet媒体类型为application/gzip
。
File textFile = new File("C:/Yash/GZIP/archive.gz.txt");
File zipFile = new File("C:/Yash/GZIP/archive.gz");
org.apache.commons.io.FileUtils.writeByteArrayToFile(textFile, byteStream);
org.apache.commons.io.FileUtils.writeByteArrayToFile(zipFile, gzipCompress);
FileInputStream inStream = new FileInputStream(zipFile);
byte[] fileGZIPBytes = IOUtils.toByteArray(inStream);
byte[] gzipFileDecompress = gzipDecompress(fileGZIPBytes);
System.out.println("GZIPFILE Decompressed Data:"+ new String(gzipFileDecompress, "UTF-8"));
以下功能用于压缩和解压缩。
public static byte[] gzipCompress(byte[] uncompressedData) {
byte[] result = new byte[]{};
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream(uncompressedData.length);
GZIPOutputStream gzipOS = new GZIPOutputStream(bos)
) {
gzipOS.write(uncompressedData);
gzipOS.close(); // You need to close it before using ByteArrayOutputStream
result = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static byte[] gzipDecompress(byte[] compressedData) {
byte[] result = new byte[]{};
try (
ByteArrayInputStream bis = new ByteArrayInputStream(compressedData);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPInputStream gzipIS = new GZIPInputStream(bis)
) {
//String gZipString= IOUtils.toString(gzipIS);
byte[] buffer = new byte[1024];
int len;
while ((len = gzipIS.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
result = bos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
答案 6 :(得分:0)
import java.io.*;
import java.util.zip.*;
public class Ex1 {
public static void main(String[] args) throws Exception{
String str ;
H h1 = new H();
h1.setHcfId("PH12345658");
h1.setHcfName("PANA HEALTH ACRE FACILITY");
str = h1.toString();
System.out.println(str);
if (str == null || str.length() == 0) {
return ;
}
ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
out.close();
String s = out.toString() ;
System.out.println( s );
byte[] ba = out.toByteArray();
System.out.println( "---------------BREAK-------------" );
ByteArrayInputStream in = new ByteArrayInputStream(ba);
GZIPInputStream gzis = new GZIPInputStream(in);
InputStreamReader reader = new InputStreamReader(gzis);
BufferedReader pr = new BufferedReader(reader);
String readed;
while ((readed = pr.readLine()) != null) {
System.out.println(readed);
}
//Close all the streams
}
}
答案 7 :(得分:0)
你也可以
try (GZIPInputStream gzipIn = new GZIPInputStream(new ByteArrayInputStream(pByteArray)))
{
....
}
AutoClosable是一件好事 https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html