java实现将多个文件用gzip压缩成tar.gz格式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CNZYYH/article/details/82900846

以下是一个工具类:


import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

public class TargzUtil {

	/**
	 *
	 * @Title: pack
	 * @Description: 将一组文件打成tar包
	 * @param sources
	 *            要打包的原文件数组
	 * @param target
	 *            打包后的文件
	 * @return File 返回打包后的文件
	 * @throws
	 */
	public static File pack(List<File> sources, File target) throws IOException {
		FileOutputStream out = null;
		try {
			out = new FileOutputStream(target);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		TarArchiveOutputStream os = new TarArchiveOutputStream(out);
		for (File file : sources) {
			InputStream inputStream = null;
			try {
				os.putArchiveEntry(new TarArchiveEntry(file,file.getName()));//打包的时候只是包含文件,不带路径
				//os.putArchiveEntry(new TarArchiveEntry(file));  //打包的时候带路径
				inputStream = new FileInputStream(file);
				IOUtils.copy(inputStream, os);
				os.closeArchiveEntry();

			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			if(inputStream!=null){
				inputStream.close();
			}
		}
		if (os != null) {
			try {
				os.flush();
				os.close();
				System.out.println("打包后文件为:" + target);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return target;
	}


	/**
	 * tar打包压缩
	 * @param source  需要压缩的文件
	 * @param FilePath  压缩后的文件全文件名(.tar)
	 * @return  返回压缩后的文件
	 */
	public static File compress(File source,String FilePath) {
		File target = new File(FilePath);
		FileInputStream in = null;
		GZIPOutputStream out = null;
		try {
			in = new FileInputStream(source);
			out = new GZIPOutputStream(new FileOutputStream(target));
			byte[] array = new byte[1024];
			int number;
			while ((number = in.read(array, 0, array.length)) != -1) {
				out.write(array, 0, number);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		} finally {
			if (in != null) {
				try {
					in.close();
					source.delete();//解压成功后,删除tar文件
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}
			}
			if (out != null) {
				try {
					out.close();
					System.out.println("打包后文件为:" + target);
				} catch (IOException e) {
					e.printStackTrace();
					return null;
				}
			}
		}
		return target;
	}

	public static void main(String[] args) {
		try {
			List<File> testFile = new ArrayList<>();
			String target = "D:/data/file/ceshi.tar";
			testFile.add(new File("D:/data/file/mock3.log"));
			testFile.add(new File("D:/data/file/mock4.log"));
			testFile.add(new File("D:/data/file/mock5.log"));
			//testFile.add(new File("app.properties"));
			compress(pack(testFile, new File(target)),"D:/data/file/ceshi.tar.gz");
		} catch (Exception e) {
			e.printStackTrace();
		}
 }

}
org.apache.commons.compress

需要引入包的pom:

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-compress</artifactId>
   <version>1.9</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/CNZYYH/article/details/82900846