2012-05-06 4 views
2

프로그래밍 방식으로 .xml.gz 파일의 압축을 푸는 중입니다. .gz 파일의 압축을 푸는 방법을 알려주는 인터넷에서 사용할 수있는 많은 예제가 있기 때문에 꽤 간단합니다. 그러나, 그것을 시도 할 때마다 예외가 발생합니다 : java.io.IOException : 알 수없는 형식 (매직 번호 d4d4).Android .xml.gz 파일 압축 해제

Android 앱에서이 작업을 수행하려고합니다. Java에서 다르게 수행되는 방식과 다르게 수행되어야합니까?

이 예제 코드는 다음과 같습니다. here.

누구나 내가 뭘 잘못하고 있는지 알 수 있습니까? 또한 웹 서버에서 파일을 다운로드 한 후 파일의 압축을 푸는 중입니다. 다운로드가 제대로 작동 한 것 같습니다.

답변

5

압축 해제를 동시에 다운로드하는 멋진 방법을 찾았습니다. 그것은 산들 바람처럼 작동합니다.

protected String doInBackground(String... sUrl) { 
    try { 
     URL url = new URL(sUrl[0]); 
     URLConnection connection = url.openConnection(); 
     InputStream stream = connection.getInputStream(); 
     stream = new GZIPInputStream(stream); 
     InputSource is = new InputSource(stream); 
     InputStream input = new BufferedInputStream(is.getByteStream()); 
     OutputStream output = new FileOutputStream("Path to the file"); 
     byte data[] = new byte[2097152]; 
     long total = 0; 
     int count; 

     while ((count = input.read(data)) != -1) { 
      total += count; 
      output.write(data, 0, count); 
     } 

     output.flush(); 
     output.close(); 
     input.close(); 
    } catch (BufferOverflowException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

참조 : http://abhirampal.com/2012/05/17/android-download-decompress-gzip/

1

.xml.gz 파일의 처음 10 바이트는 무엇입니까? 압축 해제 기가 처음 두 바이트가 d4 d4라는 것을 알려주려는 것 같습니다. .gz 파일은 1f 8b로 시작해야합니다.