importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.text.SimpleDateFormat;importjava.util.Calendar;importjava.util.Date;importjava.util.zip.GZIPOutputStream;importorg.apache.tools.tar.TarEntry;importorg.apache.tools.tar.TarOutputStream;public classTest {//不能对每层都包含文件和目录的多层次目录结构打包
public static voidCompressedFiles_Gzip(String folderPath, String targzipFilePath)
{
File srcPath=newFile(folderPath);int length=srcPath.listFiles().length;byte[] buf = new byte[1024]; //设定读入缓冲区尺寸
File[] files =srcPath.listFiles();try{//建立压缩文件输出流
FileOutputStream fout=newFileOutputStream(targzipFilePath);//建立tar压缩输出流
TarOutputStream tout=newTarOutputStream(fout);for(int i=0;i
{
String filename=srcPath.getPath()+File.separator+files[i].getName();//打开需压缩文件作为文件输入流
FileInputStream fin=new FileInputStream(filename); //filename是文件全路径
TarEntry tarEn=new TarEntry(files[i]); //此处必须使用new TarEntry(File file);//tarEn.setName(files[i].getName());//此处需重置名称,默认是带全路径的,否则打包后会带全路径
tout.putNextEntry(tarEn);intnum;while ((num=fin.read(buf)) != -1)
{
tout.write(buf,0,num);
}
tout.closeEntry();
fin.close();
}
tout.close();
fout.close();//建立压缩文件输出流
FileOutputStream gzFile=new FileOutputStream(targzipFilePath+".gz");//建立gzip压缩输出流
GZIPOutputStream gzout=newGZIPOutputStream(gzFile);//打开需压缩文件作为文件输入流
FileInputStream tarin=new FileInputStream(targzipFilePath); //targzipFilePath是文件全路径
intlen;while ((len=tarin.read(buf)) != -1)
{
gzout.write(buf,0,len);
}
gzout.close();
gzFile.close();
tarin.close();
}catch(FileNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
}//循环遍历目录结构中的文件并添加至tar的输出流
public static voidaddFiles(TarOutputStream tout,String folderPath)
{
File srcPath=newFile(folderPath);int length=srcPath.listFiles().length;byte[] buf = new byte[1024]; //设定读入缓冲区尺寸
File[] files =srcPath.listFiles();try{for(int i=0;i
{if(files[i].isFile())
{
System.out.println("file:"+files[i].getName());
String filename=srcPath.getPath()+File.separator+files[i].getName();//打开需压缩文件作为文件输入流
FileInputStream fin=new FileInputStream(filename); //filename是文件全路径
TarEntry tarEn=new TarEntry(files[i]); //此处必须使用new TarEntry(File file);//System.out.println("--------"+files[i].getParentFile().getName());
tarEn.setName(files[i].getParentFile().getName()+"\\"+files[i].getName()); //此处需重置名称,默认是带全路径的,否则打包后会带全路径
tout.putNextEntry(tarEn);intnum;while ((num=fin.read(buf)) != -1)
{
tout.write(buf,0,num);
}
tout.closeEntry();
fin.close();
}else{
System.out.println(files[i].getPath());
addFiles(tout,files[i].getPath());
}
}
}catch(FileNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
}//生成tar并压缩成tar.gz
public static voidWriteToTarGzip(String folderPath, String targzipFilePath)
{byte[] buf = new byte[1024]; //设定读入缓冲区尺寸
try{//建立压缩文件输出流
FileOutputStream fout=newFileOutputStream(targzipFilePath);//建立tar压缩输出流
TarOutputStream tout=newTarOutputStream(fout);
addFiles(tout,folderPath);
tout.close();
fout.close();//建立压缩文件输出流
FileOutputStream gzFile=new FileOutputStream(targzipFilePath+".gz");//建立gzip压缩输出流
GZIPOutputStream gzout=newGZIPOutputStream(gzFile);//打开需压缩文件作为文件输入流
FileInputStream tarin=new FileInputStream(targzipFilePath); //targzipFilePath是文件全路径
intlen;while ((len=tarin.read(buf)) != -1)
{
gzout.write(buf,0,len);
}
gzout.close();
gzFile.close();
tarin.close();
}catch(FileNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
File tarfile=newFile(targzipFilePath);
tarfile.delete();
}//循环遍历目录结构中的文件并添加至tar的输出流(支持多个文件夹)
public static voidaddFilesAll(TarOutputStream tout,File[] sources)
{
File srcPath=null;//File[] sources = new File[] {new File("D:\\test\\20160603"), new File("D:\\test\\20160302")};
try{for(int k=0;k
srcPath=sources[k];int length=srcPath.listFiles().length;byte[] buf = new byte[1024]; //设定读入缓冲区尺寸
File[] files =srcPath.listFiles();for(int i=0;i
{if(files[i].isFile())
{//System.out.println("file:"+files[i].getName());
String filename=srcPath.getPath()+File.separator+files[i].getName();//打开需压缩文件作为文件输入流
FileInputStream fin=new FileInputStream(filename); //filename是文件全路径
TarEntry tarEn=new TarEntry(files[i]); //此处必须使用new TarEntry(File file);//System.out.println("--------"+files[i].getParentFile().getName());
tarEn.setName(files[i].getParentFile().getName()+"\\"+files[i].getName()); //此处需重置名称,默认是带全路径的,否则打包后会带全路径
tout.putNextEntry(tarEn);intnum;while ((num=fin.read(buf)) != -1)
{
tout.write(buf,0,num);
}
tout.closeEntry();
fin.close();
}else{
System.out.println(files[i].getPath());
addFiles(tout,files[i].getPath());
}
}
}
}catch(FileNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
}//生成tar并压缩成tar.gz
public static voidWriteToTarGzipAll(String targzipFilePath,File[] sources)
{byte[] buf = new byte[1024]; //设定读入缓冲区尺寸
try{//建立压缩文件输出流
FileOutputStream fout=newFileOutputStream(targzipFilePath);//建立tar压缩输出流
TarOutputStream tout=newTarOutputStream(fout);
addFilesAll(tout,sources);
tout.close();
fout.close();//建立压缩文件输出流
FileOutputStream gzFile=new FileOutputStream(targzipFilePath+".gz");//建立gzip压缩输出流
GZIPOutputStream gzout=newGZIPOutputStream(gzFile);//打开需压缩文件作为文件输入流
FileInputStream tarin=new FileInputStream(targzipFilePath); //targzipFilePath是文件全路径
intlen;while ((len=tarin.read(buf)) != -1)
{
gzout.write(buf,0,len);
}
gzout.close();
gzFile.close();
tarin.close();
}catch(FileNotFoundException e)
{
System.out.println(e);
}catch(IOException e)
{
System.out.println(e);
}
File tarfile=newFile(targzipFilePath);
tarfile.delete();
}//判断文件夹是否存在
public static booleanisFile(String path){boolean flag=true;
File file= new File(path); //判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists()) {
flag=false;
}returnflag;
}public String runFileTar(String indir,String outdir,String sdate,String edate,intjg){//String indir="D:\\test\\";//String outdir="D:\\test\\";//String sdate="20160301";//String edate="20160905";//int jg=2;//间隔月份
String returnFlag="------执行成功----------";
Calendar c= Calendar.getInstance();//获得一个日历的实例
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");try{
Date sd=sdf.parse(sdate);
Date ed=sdf.parse(edate);if (ed.getTime() < sd.getTime()) { //比较日期大小
returnFlag="------开始时间大于结束时间----------";
}else{
File[] sources=null;//遍历总时间
while(ed.getTime() >sd.getTime()){
c.setTime(sd);//设置日历时间
c.add(Calendar.MONTH,jg);//在日历的月份上增加jg个月
Date mthdate=c.getTime();
c.setTime(mthdate);int day2=c.get(Calendar.DATE);
c.set(Calendar.DATE,day2-1);
mthdate=c.getTime();if(mthdate.getTime()>ed.getTime()){
mthdate=ed;
}
Date mthfile=mthdate;
Date mthdate2=mthdate;int fileCount=0;while(sd.getTime() <= mthdate2.getTime()){ //遍历jg个月内的文件夹//判断文件夹是否存在
if(isFile(indir+sdf.format(mthdate2))){
fileCount=fileCount+1;
}//日期减一天
c.setTime(mthdate2);int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
mthdate2=c.getTime();
}if(fileCount>0){
sources= newFile[fileCount];while(sd.getTime() <= mthdate.getTime()){ //遍历jg个月内的文件夹//判断文件夹是否存在
if(isFile(indir+sdf.format(mthdate.getTime()))){
File file=new File(indir+sdf.format(mthdate));
fileCount=fileCount-1;
sources[fileCount]=file;
}//日期减一天
c.setTime(mthdate);int day=c.get(Calendar.DATE);
c.set(Calendar.DATE,day-1);
mthdate=c.getTime();
}
WriteToTarGzipAll(outdir+sdf.format(sd)+"-"+sdf.format(mthfile)+".tar",sources);
}
mthdate=mthdate2;
c.setTime(sd);//设置日历时间
c.add(Calendar.MONTH,jg);//在日历的月份上增加jg个月
sd=c.getTime();
}
}
}catch(Exception e) {
e.printStackTrace();
returnFlag="------运行出错----------";
}finally{
System.out.println("finished");
}returnreturnFlag;
}public static voidmain(String[] args)
{//方法一:对于目录中只含文件的文件夹打包并压缩//CompressedFiles_Gzip("D:\\test\\20160302","D:\\test\\20160603.tar");//方法二:对目录中既含有文件又含有递归目录的文件夹打包//WriteToTarGzip("D:\\test\\20160603","D:\\test\\20160603.tar");//File[] str = new File[2];//str[0]=new File("D:\\test\\20160603");//str[1] = new File("D:\\test\\20160302");//支持多个文件夹//File[] sources = new File[] {new File("D:\\test\\20160603"), new File("D:\\test\\20160302"), new File("D:\\test\\2016080633")};//WriteToTarGzipAll("D:\\test\\20160603.tar",sources);
String indir="D:\\test\\";
String outdir="D:\\test\\";
String sdate="20160220";
String edate="20170905";int jg=20; //间隔月份
Test test=newTest();
String returnFlag=test.runFileTar(indir, outdir, sdate, edate, jg);}
}