繁体   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