2010-12-11 18 views
7

Bzip2 arşivi oluşturmam gerekiyor. 'Apache ant' den indirilen bir bzip2 kütüphanesi. Java: Bzip2 kütüphanesi

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

(bunu nasıl kullanılacağını herhangi bir örnek bulamadık, bu yüzden bu şekilde kullanmaya karar) Ama diskte bozuk bir arşiv oluşturur.

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

Sonra, örneğin, sen cat compressed.bz2 | bunzip2 > uncompressed.txt üzerinde birlikte bzipped dosyasının içeriğini ayıklamak: içerik yazmadan önce:

cevap

7

Sen ('B', 'Z' iki bayt) BZip2 başlığı eklemek zorunda a * nix sistemi.

+0

: https://svn.apache.org/ viewvc/karınca/çekirdek/gövde/src/main/org/apache/araçları/karınca/türleri/kaynaklar/BZip2Resource.java? view = biçimlendirme # L71 –

2

Ben bir örnek bulamadım ama sonunda ben işte biridir CBZip2OutputStream nasıl kullanılacağı anlaşılmaktadır: Karıncanın kodunun kendisi başına, Bu beklenen bir durumdur

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(); 

    }