根据输入时间段备份压缩日志文件
生活随笔
收集整理的這篇文章主要介紹了
根据输入时间段备份压缩日志文件
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
根據(jù)輸入時(shí)間段備份壓縮日志文件為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 {//不能對(duì)每層都包含文件和目錄的多層次目錄結(jié)構(gòu)打包public static void CompressedFiles_Gzip(String folderPath, String targzipFilePath) { File srcPath =new File(folderPath); int length=srcPath.listFiles().length; byte[] buf = new byte[1024]; //設(shè)定讀入緩沖區(qū)尺寸 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(); //打開(kāi)需壓縮文件作為文件輸入流 FileInputStream fin=new FileInputStream(filename); //filename是文件全路徑 TarEntry tarEn=new TarEntry(files[i]); //此處必須使用new TarEntry(File file); //tarEn.setName(files[i].getName()); //此處需重置名稱,默認(rèn)是帶全路徑的,否則打包后會(huì)帶全路徑 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); //打開(kāi)需壓縮文件作為文件輸入流 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); } }//循環(huán)遍歷目錄結(jié)構(gòu)中的文件并添加至tar的輸出流public static void addFiles(TarOutputStream tout,String folderPath) { File srcPath =new File(folderPath); int length=srcPath.listFiles().length; byte[] buf = new byte[1024]; //設(shè)定讀入緩沖區(qū)尺寸 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(); //打開(kāi)需壓縮文件作為文件輸入流 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()); //此處需重置名稱,默認(rèn)是帶全路徑的,否則打包后會(huì)帶全路徑 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.gzpublic static void WriteToTarGzip(String folderPath, String targzipFilePath) { byte[] buf = new byte[1024]; //設(shè)定讀入緩沖區(qū)尺寸 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); //打開(kāi)需壓縮文件作為文件輸入流 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();}//循環(huán)遍歷目錄結(jié)構(gòu)中的文件并添加至tar的輸出流(支持多個(gè)文件夾)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]; //設(shè)定讀入緩沖區(qū)尺寸 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(); //打開(kāi)需壓縮文件作為文件輸入流 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()); //此處需重置名稱,默認(rèn)是帶全路徑的,否則打包后會(huì)帶全路徑 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.gzpublic static void WriteToTarGzipAll(String targzipFilePath,File[] sources) { byte[] buf = new byte[1024]; //設(shè)定讀入緩沖區(qū)尺寸 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); //打開(kāi)需壓縮文件作為文件輸入流 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); //判斷文件夾是否存在,如果不存在則創(chuàng)建文件夾 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="------執(zhí)行成功----------";Calendar c = Calendar.getInstance();//獲得一個(gè)日歷的實(shí)例SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");try {Date sd=sdf.parse(sdate);Date ed=sdf.parse(edate);if (ed.getTime() < sd.getTime()) { //比較日期大小returnFlag="------開(kāi)始時(shí)間大于結(jié)束時(shí)間----------";}else{File[] sources =null;//遍歷總時(shí)間while(ed.getTime() > sd.getTime()){c.setTime(sd);//設(shè)置日歷時(shí)間c.add(Calendar.MONTH,jg);//在日歷的月份上增加jg個(gè)月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個(gè)月內(nèi)的文件夾//判斷文件夾是否存在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個(gè)月內(nèi)的文件夾//判斷文件夾是否存在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);//設(shè)置日歷時(shí)間c.add(Calendar.MONTH,jg);//在日歷的月份上增加jg個(gè)月sd=c.getTime();}}} catch (Exception e) {e.printStackTrace();returnFlag="------運(yùn)行出錯(cuò)----------";}finally{System.out.println("finished");}return returnFlag;}public static void main(String[] args){//方法一:對(duì)于目錄中只含文件的文件夾打包并壓縮//CompressedFiles_Gzip("D:\\test\\20160302","D:\\test\\20160603.tar");//方法二:對(duì)目錄中既含有文件又含有遞歸目錄的文件夾打包//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");//支持多個(gè)文件夾//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);} }?
轉(zhuǎn)載于:https://www.cnblogs.com/Allen0603/p/5764967.html
總結(jié)
以上是生活随笔為你收集整理的根据输入时间段备份压缩日志文件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Selenium java环境搭建
- 下一篇: 作业题之动画改良