2010-12-11 3 views
7

Bzip2 보관 파일을 만들어야합니다. A는 'Apache ant'에서 bzip2 라이브러리를 다운로드했습니다. Java : Bzip2 라이브러리

I use class CBZip2OutputStream: 
String s = ..... 
CBZip2OutputStream os = new CBZip2OutputStream(fos); 
       os.write(s.getBytes(Charset.forName("UTF-8"))); 
       os.flush(); 
       os.close(); 

(나는 그것을 사용하는 방법을 어떤 예를 찾을 수 없었기 때문에이 방법을 사용하기로 결정) 그러나 디스크의 손상된 압축 파일을 생성합니다.

//Write 'BZ' before compressing the stream 
fos.write("BZ".getBytes()); 
//Write to compressed stream as usual 
CBZip2OutputStream os = new CBZip2OutputStream(fos); 
... the rest ... 

다음, 예를 들어, 당신이 cat compressed.bz2 | bunzip2 > uncompressed.txt에와 bzip으로 압축 된 파일의 내용을 추출 할 수 있습니다 : 콘텐츠를 쓰기 전에 :

답변

7

당신은 ('B', 'Z'두 바이트)의 bzip2 헤더를 추가해야 a * nix 시스템.

+0

: https://svn.apache.org/ viewvc/ant/core/trunk/src/main/org/apache/tools/ant/types/resources/BZip2Resource.java? view = 마크 업 # l71 –

2

내가 예를 발견하지 않았습니다 그러나 결국 내가 지금 여기에 하나입니다 CBZip2OutputStream을 사용하는 방법을 이해 : 개미의 코드 자체 당이 예상되는

public void createBZipFile() throws IOException{ 

     // file to zip 
     File file = new File("plane.jpg"); 

     // fichier compresse 
     File fileZiped= new File("plane.bz2"); 

     // Outputstream for fileZiped 
     FileOutputStream fileOutputStream = new FileOutputStream(fileZiped); 
     fileOutputStream.write("BZ".getBytes()); 

     // we getting the data in a byte array 
     byte[] fileData = getArrayByteFromFile(file); 

     CBZip2OutputStream bzip = null; 

     try{ 
      bzip = new CBZip2OutputStream(fileOutputStream); 

      bzip.write(fileData, 0, fileData.length); 
      bzip.flush() ; 
      bzip.close(); 

     }catch (IOException ex) { 

      ex.printStackTrace(); 
     } 



     fos.close(); 

    }