2012-10-23 53 views
1

我使用CBZip2OutputStream来创建一个压缩的bzip文件。有用。如何用CBZip2OutputStream压缩多个文件

但我想在一个bzip文件中压缩几个文件,但不使用tar归档。

如果我有file1,file2,file3,我希望它们在files.bz2中不在archive files.tar.bz2中。

有可能吗?

回答

0

我明白,所以我使用包带TarOutputStream类这样的:

public void makingTarArchive(File[] inFiles, String inPathName) throws IOException{ 

    StringBuilder stringBuilder = new StringBuilder(inPathName); 
    stringBuilder.append(".tar"); 

    String pathName = stringBuilder.toString() ; 

    // Output file stream 
    FileOutputStream dest = new FileOutputStream(pathName); 

    // Create a TarOutputStream 
    TarOutputStream out = new TarOutputStream(new BufferedOutputStream(dest)); 

    for(File f : inFiles){ 

     out.putNextEntry(new TarEntry(f, f.getName())); 
     BufferedInputStream origin = new BufferedInputStream(new FileInputStream(f)); 

     int count; 
     byte data[] = new byte[2048]; 
     while((count = origin.read(data)) != -1) { 

      out.write(data, 0, count); 
     } 

     out.flush(); 
     origin.close(); 
    } 

    out.close(); 

    dest.close(); 

    File file = new File(pathName) ; 

    createBZipFile(file); 

    boolean success = file.delete(); 

    if (!success) { 
     System.out.println("can't delete the .tar file"); 
    } 
} 
相关问题