根据输入时间段备份压缩日志文件

根据输入时间段备份压缩日志文件为tar包,

ant-1.7.1.jar

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.zip.GZIPOutputStream;

import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarOutputStream;


public class Test {

    
    
     //不能对每层都包含文件和目录的多层次目录结构打包
    public static void CompressedFiles_Gzip(String folderPath, String   targzipFilePath)   
     {   
         File srcPath =new File(folderPath);   
         int length=srcPath.listFiles().length;   
         byte[] buf = new byte[1024]; //设定读入缓冲区尺寸   
          File[] files   =   srcPath.listFiles();   
         try   
          {  
             //建立压缩文件输出流   
              FileOutputStream fout=new FileOutputStream(targzipFilePath);   
             //建立tar压缩输出流   
              TarOutputStream tout=new TarOutputStream(fout);   
             for(int i=0;i<length;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);     
                     int num;   
                     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=new GZIPOutputStream(gzFile);   
             //打开需压缩文件作为文件输入流   
              FileInputStream tarin=new FileInputStream(targzipFilePath);   //targzipFilePath是文件全路径   
             int len;   
             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 void addFiles(TarOutputStream tout,String folderPath)   
     {   
         File srcPath =new File(folderPath); 
         int length=srcPath.listFiles().length;   
         byte[] buf = new byte[1024]; //设定读入缓冲区尺寸   
         File[] files = srcPath.listFiles();   
         try   
          {  
              for(int i=0;i<length;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);     
                     int num;   
                     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 void WriteToTarGzip(String folderPath, String targzipFilePath)   
     {   
        byte[] buf = new byte[1024]; //设定读入缓冲区尺寸   
         try   
          {  
            //建立压缩文件输出流   
              FileOutputStream fout=new FileOutputStream(targzipFilePath);   
             //建立tar压缩输出流   
              TarOutputStream tout=new TarOutputStream(fout);  
              addFiles(tout,folderPath);              
              tout.close();
              fout.close();             
                
             //建立压缩文件输出流   
              FileOutputStream gzFile=new FileOutputStream(targzipFilePath+".gz");   
             //建立gzip压缩输出流   
              GZIPOutputStream gzout=new GZIPOutputStream(gzFile);   
             //打开需压缩文件作为文件输入流   
              FileInputStream tarin=new FileInputStream(targzipFilePath);   //targzipFilePath是文件全路径   
             int len;   
             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=new File(targzipFilePath);
          tarfile.delete();
     }
    
    
  //循环遍历目录结构中的文件并添加至tar的输出流(支持多个文件夹)
    public static void addFilesAll(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<sources.length;k++){
                 srcPath=sources[k];
                 int length=srcPath.listFiles().length;   
                 byte[] buf = new byte[1024]; //设定读入缓冲区尺寸   
                 File[] files   =   srcPath.listFiles();   
                  for(int i=0;i<length;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);     
                         int num;   
                         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 void WriteToTarGzipAll(String targzipFilePath,File[] sources)   
     {   
        byte[] buf = new byte[1024]; //设定读入缓冲区尺寸   
         try   
          {  
            //建立压缩文件输出流   
              FileOutputStream fout=new FileOutputStream(targzipFilePath);   
             //建立tar压缩输出流   
              TarOutputStream tout=new TarOutputStream(fout);  
              addFilesAll(tout,sources);              
              tout.close();
              fout.close();             
                
             //建立压缩文件输出流   
              FileOutputStream gzFile=new FileOutputStream(targzipFilePath+".gz");   
             //建立gzip压缩输出流   
              GZIPOutputStream gzout=new GZIPOutputStream(gzFile);   
             //打开需压缩文件作为文件输入流   
              FileInputStream tarin=new FileInputStream(targzipFilePath);   //targzipFilePath是文件全路径   
             int len;   
             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=new File(targzipFilePath);
          tarfile.delete();
     }
    //判断文件夹是否存在
    public static boolean isFile(String path){
        boolean flag=true;
        File file = new File(path);  //判断文件夹是否存在,如果不存在则创建文件夹  
        if (!file.exists()) {   
            flag=false;
        }
        return flag;
    }
    public String runFileTar(String indir,String outdir,String sdate,String edate,int jg){
//        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 = new File[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");
        }
        return returnFlag;
    }
    public static void main(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=new Test();
        String returnFlag=test.runFileTar(indir, outdir, sdate, edate, jg);
    }
}

 

posted @ 2016-08-12 14:58  Allen.Zhao  阅读(407)  评论(0编辑  收藏  举报