我需要创建Bzip2存档.从'Apache ant'下载的bzip2库.
I use class CBZip2OutputStream:
String s = .....
CBZip2OutputStream os = new CBZip2OutputStream(fos);
os.write(s.getBytes(Charset.forName("UTF-8")));
os.flush();
os.close();
Run Code Online (Sandbox Code Playgroud)
(我没有找到任何使用它的例子,所以我决定以这种方式使用它)
但它会在磁盘上创建损坏的存档.
在编写内容之前,您必须添加BZip2标头(两个字节:'B','Z'):
//Write 'BZ' before compressing the stream
fos.write("BZ".getBytes());
//Write to compressed stream as usual
CBZip2OutputStream os = new CBZip2OutputStream(fos);
... the rest ...
Run Code Online (Sandbox Code Playgroud)
然后,例如,您可以cat compressed.bz2 | bunzip2 > uncompressed.txt在*nix系统上提取bzip文件的内容.