本文介绍了通过Java制作tar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java将文件夹压缩到tar文件(以编程方式)。我认为必须有一个开源或图书馆来做。但是,我找不到这样的方法。

I want to use Java to compress a folder to a tar file (in programmatic way). I think there must be an open source or library to do it. However, I cannot find such method.

或者,我可以制作一个zip文件并将其扩展名重命名为.tar吗?

Alternatively, could I make a zip file and rename its extended name as .tar?

一个图书馆做呢?感谢!

Anyone could suggest a library to do it? Thanks!

推荐答案

您可以使用。

取自他们的网站:

一个例子, / p>

An example, also from their site:

   // Output file stream
   FileOutputStream dest = new FileOutputStream( "c:/test/test.tar" );

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

   // Files to tar
   File[] filesToTar=new File[2];
   filesToTar[0]=new File("c:/test/myfile1.txt");
   filesToTar[1]=new File("c:/test/myfile2.txt");

   for(File f:filesToTar){
      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();

这篇关于通过Java制作tar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 02:53