2009-11-16 1 views
5

gzipped 파일을 반환하는 서비스를 호출합니다. 응답에서 데이터를 InputStream (javax.activation.DataHandler.getInputStream(); 호의)으로 받았습니다.GZIPed 데이터의 InputStream에서 압축 해제 된 데이터의 InputStream을 얻으려면 어떻게해야합니까?

디스크에 아무 것도 쓰지 않고 압축 해제 된 데이터의 InputStream을 아카이브에있는 파일로 가져오고 싶습니다. 이 경우 압축 된 파일은 InputStream을 사용하는 javax.xml.bind.Unmarshaller을 사용하여 비 정렬하려고하는 xml 문서입니다.

현재 InputStream을 OutputStream (데이터 압축 풀기)에 씁니다. 그리고 나서 다시 InputStream에 써야합니다. 그것은 아직 작동하지 않기 때문에 나는 더 나은 (나는 희망한다) 접근법이 있는지를 알 것이라고 생각했다.

디스크에 초기 InputStream을 작성하고 gz 파일을 읽은 다음 그 파일을 읽고 거기에서 압축 된 파일을 가져 와서 거기에서부터 진행할 수 있지만 메모리에 모두 유지할 수 있습니다.

업데이트 1 : 여기 내 전류 (작동하지 - 얻을 "하지 GZIP 형식의"예외) 대신있는 ByteArrayOutputStream에 쓰는 나는 주위의 FileOutputStream에 처음 작성하는 경우

ByteArrayInputStream xmlInput = null; 
    try { 
     InputStream in = dh.getInputStream(); //dh is a javax.activation.DataHandler 
     BufferedInputStream bis = new BufferedInputStream(in); 
     ByteArrayOutputStream bo = new ByteArrayOutputStream(); 
     int bytes_read = 0; 
     byte[] dataBuf = new byte[4096]; 
     while ((bytes_read = bis.read(dataBuf)) != -1) { 
      bo.write(dataBuf, 0, bytes_read); 
     } 
     ByteArrayInputStream bin = new ByteArrayInputStream(bo.toByteArray()); 
     GZIPInputStream gzipInput = new GZIPInputStream(bin); 
     ByteArrayOutputStream out = new ByteArrayOutputStream(); 
     dataBuf = new byte[4096];; 
     bytes_read = 0; 
     while ((bytes_read = gzipInput.read(dataBuf)) > 0) { 
      out.write(dataBuf, 0, bytes_read); 
     } 
     xmlInput = new ByteArrayInputStream(out.toByteArray()); 

I 압축 파일 (xml 파일을 가져 오기 위해 수동으로 열 수 있음)을 가져오고 서비스 (eBay)가 gzip 파일이어야하므로 "GZIP 형식이 아닙니다"오류가 발생하는 이유를 모르겠습니다.

업데이트 2 : 나는 약간 다른 것을 시도했다 - 동일한 오류 ("GZIP 형식이 아님"). 와우, 방금 세미 콜론으로 괄호를 끝내려고했습니다. 어쨌든, 아직 작동하지 않는 두 번째 시도가 있습니다.

ByteArrayInputStream xmlInput = null; 
    try { 
     GZIPInputStream gzipInput = new GZIPInputStream(dh.getInputStream()); 
     ByteArrayOutputStream bo = new ByteArrayOutputStream(); 
     int bytes_read = 0; 
     byte[] dataBuf = new byte[4096]; 
     while ((bytes_read = gzipInput.read(dataBuf)) != -1) { 
      bo.write(dataBuf, 0, bytes_read); 
     } 
     xmlInput = new ByteArrayInputStream(bo.toByteArray()); 
+0

큰 문제인 것처럼 보였습니다. 이베이가 나에게 gzip을 제공한다고 말했지만 사실은 우편이었습니다. –

답변

2

다음 코드는 작동합니다. 예외를 올바르게 처리해야한다는 점에 유의하십시오.

OutputStream out = null; 
InputStream in = null; 
try { 
    out = /* some output stream */; 
    in = new java.util.GZIPInputStream(/*some stream*/); 
    byte[] buffer = new byte[4096]; 
    int c = 0; 
    while ((c = in.read(buffer, 0, 4096)) > 0) { 
     out.write(buffer, 0, c); 
    } 
} finally { 
    if (in != null) { 
     in.close(); 
    } 
    if (out != null) { 
     out.close(); 
    } 
} 
0

GZIPInputStream을 살펴보십시오. Here's an example; 클래스는 이것을 매우 투명하게 처리합니다. 사용하는 것이 거의 없습니다.

+1

그냥 호기심 : explologicalily 이유 1.4.2? 왜 최신 버전이 아닌가? http://java.sun.com/javase/6/docs/api/java/util/zip/GZIPInputStream.html – BalusC

+0

죄송합니다. "1.4.2에서 처음 사용 가능하다고 생각합니다." 저것을 편집하겠습니다. :-) –

+0

GZIPInputStream에서 "GZIP 형식이 아닙니다"예외가 발생해도 현재 작업하고있는 내용을보고 있습니다. 내가 파일을 디스크에 쓸 수 있으며 압축 된 파일 (문서에 따라 gzip)이므로이 내용을 이해할 수 없습니다. 나는 내가 현재하고있는 (작동하지 않는) 것을 질문에 추가 할 것이다. –

3

입력 스트림을 GZIPInputStream으로 꾸미십시오.

InputStream decompressed = new GZIPInputStream(compressed);