Java 将文件打包成 tar 文件

来源:互联网 发布:centos6.5图形网络配置 编辑:程序博客网 时间:2024/05/22 06:04

Java 将文件打包成 tar 文件

<dependency>  <groupId>org.xeustechnologies</groupId>  <artifactId>jtar</artifactId>  <version>1.1</version></dependency>


package com;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.xeustechnologies.jtar.TarEntry;import org.xeustechnologies.jtar.TarOutputStream;public class Compress {public void toCompress() {FileOutputStream dest;TarOutputStream out;try {dest = new FileOutputStream("c:/test/test.tar");out = new TarOutputStream(new BufferedOutputStream(dest));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();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void main(String args[]){new Compress().toCompress();}}






0 0