java打tar_1
程序员文章站
2022-05-27 16:46:12
...
文件打包tar
package com.qjzh.zhwg.modules.subsystem.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
/**
* @Title: GZIPUtil.java
* @Description: 压缩tar工具类
* @version V1.0
*/
public class GZIPUtil {
private static List<File> files = new ArrayList<File>();
public static final String path = "WebRoot";
public static final String[] paths = new String[] {
"/jincheng/help/wzdt.jsp",
};
public static final File target = new File("发布/V1.8.0.71.tar");
/**
* @Title: pack
* @Description: 将一组文件打成tar包
* @return File 返回打包后的文件
* @throws
*/
public static File pack() {
long startTime = System.currentTimeMillis();
FileOutputStream out = null;
try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
TarArchiveOutputStream os = new TarArchiveOutputStream(out);
os.setLongFileMode(2);
System.out.println("***************开始打" + target + "包****************");
int fileCount = 0;
for (File file : getSourcesFile()) {
try {
os.putArchiveEntry(new TarArchiveEntry(file));
IOUtils.copy(new FileInputStream(file), os);
os.closeArchiveEntry();
fileCount++;
} catch (FileNotFoundException e) {
System.out.print("****创建" + file.getName() + "******异常");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("***************打" + target + "包结束:用时 "
+ (endTime - startTime) + "毫秒,共"+fileCount+"个文件。");
return target;
}
public static List<File> getSourcesFile() {
for (int i = 0; i < paths.length; i++) {
try {
File file = new File(path + paths[i]);
if (!file.getName().contains(".svn")) {
getFilesByDirectory(file);
}
} catch (Exception e) {
System.out.println("-----创建文件" + paths[i] + "异常------");
e.printStackTrace();
}
}
return files;
}
public static void getFilesByDirectory(File file) {
if (file.isDirectory()) {
File[] fileArrays = file.listFiles();
for (File f : fileArrays) {
getFilesByDirectory(f);
}
} else {
if (!file.getName().contains(".svn")) {
files.add(file);
}
}
}
public static void main(String[] args) {
pack();
}
}