簡體   English   中英

如何使用CBZip2OutputStream壓縮多個文件

[英]How to compress multiple file with CBZip2OutputStream

我使用CBZip2OutputStream創建壓縮的bzip文件。 有用。

但是我想在一個bzip文件中壓縮多個文件,但不使用tar存檔。

如果我有file1,file2,file3,則我希望它們在files.bz2中而不在歸檔文件files.tar.bz2中。

有可能的 ?

BZip2只是單個文件的壓縮器,因此如果不先將多個文件放入存檔文件中,就無法將多個文件放入Bzip2文件中。

您可以將自己的文件開始和結束標記放到輸出流中,但是最好使用標准存檔格式。

Apache Commons具有TarArchiveOutputStream (和TarArchiveInputStream ),在這里很有用。

我了解,所以我使用帶有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");
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM