通过Java制作tar文件 [英] Make tar file by Java

查看:395
本文介绍了通过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!

推荐答案

您可以使用 jtar - Java Tar library

取自他们的网站:


JTar是一个简单的Java Tar库,它提供了一种使用IO流创建和读取tar文件的简单方法。 API非常简单,类似于java.util.zip包。

JTar is a simple Java Tar library, that provides an easy way to create and read tar files using IO streams. The API is very simple to use and similar to the java.util.zip package.

一个例子, / 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文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆