/* 1*/ package PFSaver; /* 2*/ /* 3*/ import java.io.BufferedInputStream; /* 4*/ import java.io.File; /* 5*/ import java.io.FileInputStream; /* 6*/ import java.io.FileNotFoundException; /* 7*/ import java.io.FileOutputStream; /* 8*/ import java.io.IOException; /* 9*/ import java.io.ObjectOutputStream; /* 10*/ import java.util.zip.ZipEntry; /* 11*/ import java.util.zip.ZipException; /* 12*/ import java.util.zip.ZipOutputStream; /* 13*/ /* 14*/ public class ZipCompresser /* 15*/ { /* 16*/ private ZipOutputStream zout = null; /* 17*/ private ObjectOutputStream oout = null; /* 18*/ public static final String zipExtension = ".zip"; /* 19*/ /* 20*/ public ZipCompresser(String zippath) throws IOException /* 21*/ { /* 22*/ zout = new ZipOutputStream(new FileOutputStream( new File(zippath) )); /* 23*/ } /* 24*/ /* 25*/ public void addFileInZip(String filepath) /* 26*/ throws FileNotFoundException, ZipException, IOException /* 27*/ { /* 28*/ compress(zout, new File(filepath)); /* 29*/ } /* 30*/ /* 31*/ public void addFileInZip(File file) /* 32*/ throws FileNotFoundException, ZipException, IOException /* 33*/ { /* 34*/ compress(zout, file); /* 35*/ } /* 36*/ /* 37*/ public void addFileInZip(String filepath, String filename) /* 38*/ throws FileNotFoundException, ZipException, IOException /* 39*/ { /* 40*/ compressFile(zout, new File(filepath), filename); /* 41*/ } /* 42*/ /* 43*/ public void addFileInZip(File file, String filename) /* 44*/ throws FileNotFoundException, ZipException, IOException /* 45*/ { /* 46*/ compressFile(zout, file, filename); /* 47*/ } /* 48*/ /* 49*/ public void addFileInZip(Object obj, String filename) throws IOException /* 50*/ { /* 51*/ zout.putNextEntry(new ZipEntry(filename)); /* 52*/ oout = new ObjectOutputStream(zout); /* 53*/ oout.writeObject(obj); /* 54*/ } /* 55*/ /* 56*/ public void close() throws IOException /* 57*/ { /* 58*/ zout.close(); /* 59*/ if(oout!=null) oout.close(); /* 60*/ } /* 61*/ /* 62*/ public static void compress(ZipOutputStream zout, File file) /* 63*/ throws FileNotFoundException, ZipException, IOException /* 64*/ { /* 65*/ if(file.isFile()) /* 66*/ compressFile(zout, file, file.getName()); /* 67*/ else /* 68*/ compressFolder(zout, file); /* 69*/ } /* 70*/ /* 71*/ public static void compressFolder(ZipOutputStream zout, File file) /* 72*/ throws FileNotFoundException, ZipException,IOException /* 73*/ { /* 74*/ File[] subFile = file.listFiles(); /* 75*/ /* 76*/ for(int i=0;i<subFile.length;i++) /* 77*/ { /* 78*/ if(subFile[i].isFile()) /* 79*/ compressFile(zout, subFile[i], file.getName()); /* 80*/ else /* 81*/ compressFolder(zout, subFile[i]); /* 82*/ } /* 83*/ } /* 84*/ /* 85*/ public static void compressFile(ZipOutputStream out, File file, String filename) /* 86*/ throws FileNotFoundException, ZipException,IOException /* 87*/ { /* 88*/ BufferedInputStream in = new BufferedInputStream( /* 89*/ new FileInputStream( file ) ); /* 90*/ /* 91*/ ZipEntry entry = new ZipEntry( filename ); /* 92*/ out.putNextEntry( entry ); /* 93*/ /* 94*/ int buf; /* 95*/ while((buf=in.read()) != -1) /* 96*/ { /* 97*/ out.write(buf); /* 98*/ } /* 99*/ /*100*/ in.close(); /*101*/ out.closeEntry(); /*102*/ } /*103*/ /*104*/ public static void compress(String filename, String zipfile) /*105*/ throws FileNotFoundException, ZipException,IOException /*106*/ { /*107*/ ZipCompresser zipper = new ZipCompresser(zipfile); /*108*/ zipper.addFileInZip(filename); /*109*/ zipper.close(); /*110*/ } /*111*/ /*112*/ public static void compress(Object obj, String zipfile, String filename) /*113*/ throws FileNotFoundException, ZipException, IOException /*114*/ { /*115*/ ObjectOutputStream oout = null; /*116*/ ZipOutputStream zout = null; /*117*/ /*118*/ try /*119*/ { /*120*/ zout = new ZipOutputStream(new FileOutputStream(zipfile)); /*121*/ zout.putNextEntry(new ZipEntry(filename)); /*122*/ oout = new ObjectOutputStream(zout); /*123*/ /*124*/ oout.writeObject(obj); /*125*/ } /*126*/ finally /*127*/ { /*128*/ if(zout!=null) zout.close(); /*129*/ if(oout!=null) oout.close(); /*130*/ } /*131*/ } /*132*/ }