2012-02-10 8 views
0

서비스 A가 서비스 B에 압축 데이터를 제공하는 응용 프로그램이 있습니다. 서비스 B는 압축을 풀어야합니다.파일 내용 압축 해제

서비스 A에는 노출 메서드 getStream이 있으며 ByteArrayInputStream을 출력으로 제공하고 데이터 init은 압축 데이터입니다.

그러나 GzipInputStream에 전달하면 Not in Gzip 형식 예외가 발생합니다.

InputStream ins = method.getInputStream(); 
GZIPInputStream gis = new GZIPInputStream(ins); 

이것은 예외입니다. 파일이 서비스 A에 덤프되면 데이터가 압축됩니다. 따라서 getInputStream은 압축 된 데이터를 제공합니다.

처리 방법 ans는 GzipInputStream에 전달합니까? 이 압축되면

감사
Dheeraj 조시

답변

1

, 당신은 ZipInputstream를 사용해야합니다.

1

"우편 번호"형식에 따라 다릅니다. zip 이름 (zip, gzip, bzip2, lzip)을 가진 여러 형식이 있고 다른 형식은 다른 파서를 필요로합니다.
http://en.wikipedia.org/wiki/List_of_archive_formats
http://www.codeguru.com/java/tij/tij0115.shtml
http://docstore.mik.ua/orelly/java-ent/jnut/ch25_01.htm

당신이 다음이 코드를 시도 지퍼를 사용하는 경우 :

public void doUnzip(InputStream is, String destinationDirectory) throws IOException { 
    int BUFFER = 2048; 

    // make destination folder 
    File unzipDestinationDirectory = new File(destinationDirectory); 
    unzipDestinationDirectory.mkdir(); 

    ZipInputStream zis = new ZipInputStream(is); 

    // Process each entry 
    for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis 
      .getNextEntry()) { 

     File destFile = new File(unzipDestinationDirectory, entry.getName()); 

     // create the parent directory structure if needed 
     destFile.getParentFile().mkdirs(); 

     try { 
      // extract file if not a directory 
      if (!entry.isDirectory()) { 
       // establish buffer for writing file 
       byte data[] = new byte[BUFFER]; 

       // write the current file to disk 
       FileOutputStream fos = new FileOutputStream(destFile); 
       BufferedOutputStream dest = new BufferedOutputStream(fos, 
         BUFFER); 

       // read and write until last byte is encountered 
       for (int bytesRead; (bytesRead = zis.read(data, 0, BUFFER)) != -1;) { 
        dest.write(data, 0, bytesRead); 
       } 
       dest.flush(); 
       dest.close(); 
      } 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
    is.close(); 
} 

public static void main(String[] args) { 
    UnzipInputStream unzip = new UnzipInputStream(); 
    try { 
     InputStream fis = new FileInputStream(new File("test.zip")); 
     unzip.doUnzip(fis, "output"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

파일 내용이 GZipOutputStream –

+0

을 사용하여 압축 된이 파일이 손상되지 않았 확실합니까? 그런 다음 파일을 로컬에 저장하고 외부 응용 프로그램을 사용하여 압축을 풀 수 있는지 확인하십시오. 가능한 경우 코드에서 문제가됩니다. 그렇지 않으면 파일이 손상되었거나 다른 형식입니다. –