java 压缩 zip 和tar包方法

tar包压缩方法

/**
* 压缩文件为TAR包。并删除之前文件
* @author liujunliang
* @creaetime Aug 2, 2011 3:21:46 PM
* @param filesPath 文件路径
* @param tarPath tar 路径
*/
public static void fileTar(String filesPath, String tarPath) {
List<String> list = null;
list = file2tar(filesPath, tarPath);

// 循环遍历删除备份日志文件
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
deleteFile(list.get(i)); // 备份后进行日志文件的删除
}
}
}

/**
* 删除单个文件
* @param fileName 要删除的文件的文件名
* @return boolean 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if ( file.exists() && file.isFile() ) {
if ( file.delete() ) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}

private static List<String> file2tar(String filesPath, String tarPath) {

List<String> list = new ArrayList<String>();
//文件个数
int count = 0;
//获取日志参数信息
LogParamModel logParam = LogQueneFactory.getInstance().logMap
.get(LogConstant.LOG_PARAMS);
File fileDirectory = new File(filesPath);
if (!fileDirectory.isDirectory()) {
return null;
}
int length = fileDirectory.listFiles().length;
File[] files = fileDirectory.listFiles();
TarEntry tarEn = null;
TarOutputStream tout = null;
FileOutputStream fout = null;
FileInputStream in = null;
try {
File tarFile = new File(tarPath);
if (!tarFile.exists()) {
tarFile.createNewFile();
}
fout = new FileOutputStream(tarFile);
tout = new TarOutputStream(fout);
// 循环压缩文本文件到tar格式文件
for (int i = 0; i < length; i++) {
if (!(files[i].getName().contains(LogConstant.TEMP_NAME))) {
String filename = fileDirectory.getPath() + File.separator
+ files[i].getName();
in = new FileInputStream(filename);
tarEn = new TarEntry(files[i]);
tarEn.setName(files[i].getName());
tout.putNextEntry(tarEn);
int num;
while ((num = in.read(B_ARRAY)) != -1) {
tout.write(B_ARRAY, 0, num);
}

tout.closeEntry();
in.close();
count++;
list.add(filename);
}
//判断文件个数
if(count == logParam.getFileCount()){
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(in !=null){
in.close();
}
tout.close();
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return list;
}

zip包压缩方式

/**
* 方法描述:把文件或者文件夹压缩为ZIP格式
* @author ljl
* @creaetime Jan 13, 2011 5:56:13 PM
* @param zipSrc 压缩后保存的路径
* @param fileSrc 要压缩的文件或文件夹的路径
* @param zipPath zip路径
* @return boolean 成功返回true 失败返回false
*/
public static boolean fileZip(String zipSrc, String zipPath, String fileSrc) {
boolean success = true; // 压缩成功标志
File srcpath = new File(fileSrc);
ZipOutputStream out = null;
FileOutputStream outputStream = null;
File zipSrcFile = null;
List<String> list = new ArrayList<String>();
try {
zipSrcFile = new File(zipPath);
// 判断是否有文件夹
if ( !zipSrcFile.isDirectory() ) {
boolean zipFlag = zipSrcFile.mkdirs();
if(!zipFlag){
LogFactory.getLogMessage().warn(LogConstant.RUN_LOG_TYPE,"mkzip is failure");
}
}
StringBuffer stringBuffer = new StringBuffer(
LogConstant.STRING_MAX_LENGTH);
stringBuffer.append(zipPath);
stringBuffer.append(zipSrc);
outputStream = new FileOutputStream(stringBuffer.toString());
out = new ZipOutputStream(outputStream); // 创建压缩输出流
out.setEncoding("utf-8");

compress(srcpath, out, "",list); // 开始压缩
}
catch (Exception ex) {
LogFactory.getLogMessage().error(LogConstant.RUN_LOG_TYPE,LogConstant.COMPRESSED_FILE_ERROR);
ex.printStackTrace();
success = false;
}
finally {
if ( out != null ) {
try {
out.close();

}
catch (IOException ex2){
ex2.printStackTrace();
}

}
try {
if(outputStream!=null){
outputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
// 循环遍历删除备份日志文件
for(int i =0;i<list.size();i++){
deleteFile(list.get(i)); // 备份后进行日志文件的删除
}

}
return success;
}

/**
* 方法描述:压缩文件的处理方法,fileZip掉用的方法
* @author ljl
* @creaetime Jan 13, 2011 5:55:52 PM
* @param srcpath 源路径
* @param out 输出
* @param base 根路径
* @throws Exception 异常
*/
private static void compress(File srcpath, ZipOutputStream out, String base,List<String> list)
throws Exception {
//文件个数
int count = 0;
LogParamModel logParam = LogQueneFactory.getInstance().logMap
.get(LogConstant.LOG_PARAMS);
// 如果是目录
if ( srcpath.isDirectory() ) {
File[] files = srcpath.listFiles(); // 列举出目录中所有的内容
out.putNextEntry(new ZipEntry(base
+ BaseTools.getInstance().getSp())); // 放入zip对应的路径目录
// 拼接打包路径的下划线
// StringBuffer buffer = new StringBuffer(
// LogConstant.STRING_MAX_LENGTH);
// buffer.append(base);
// buffer.append(BaseTools.getInstance().getSp());

if ( base.length() == LogConstant.LOG_LENGTH ) {
base = "";
}
// else {
// base = buffer.toString();
// }
// 循环压缩文本文件到ZIP格式文件
for (int i = 0; i < files.length; i++) {

if ( !files[i].isDirectory() ) {
// 判断是否是还需要往日志记录写入的临时文件
if (files[i].getName().contains(LogConstant.TEMP_NAME)) {
//LogFactory.getLogMessage().info((LogConstant.WRITE_FILE_ERROR));
}
else {
compress(files[i], out, files[i].getName(),list);// 压缩文本文件
list.add(srcpath + BaseTools.getInstance().getSp()
+ files[i].getName());
count++;
}
}
//判断文件个数
if(count == logParam.getFileCount()){
break;
}
}

}
else { // 如果是文件
if ( "".equals(base) ) {
out.putNextEntry(new ZipEntry(base
+ BaseTools.getInstance().getSp()));
base = srcpath.getName();
}
out.putNextEntry(new ZipEntry(base));
FileInputStream in = new FileInputStream(srcpath);
//读取长度
int writeSize = 2048;
byte[] data = new byte[writeSize];
int b;
while ((b = in.read(data)) != -1) {
out.write(data, 0, b);
}
in.close();
}
}

/**
* 删除单个文件
* @param fileName 要删除的文件的文件名
* @return boolean 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if ( file.exists() && file.isFile() ) {
if ( file.delete() ) {
return true;
}
else {
return false;
}
}
else {
return false;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值