1. 程式人生 > >java 壓縮與解壓

java 壓縮與解壓

//壓縮

    private static void zip(String srcPath, String destPath)

           throws FileNotFoundException, IOException {

       File file =new File(srcPath);

       FileOutputStream fo=new FileOutputStream(destPath);

       CheckedOutputStream csumo=new CheckedOutputStream(fo,new CRC32());//CRC32 new Adler32()

       ZipOutputStream zos=new ZipOutputStream(csumo);

       BufferedOutputStream out=new BufferedOutputStream(zos);

       //zos.setEncoding("utf-8");

       //zos.setComment("long");

       if(file.isDirectory()){

           //壓縮資料夾

           zipFolder(file,zos,out,"");

       }else{

           //壓縮檔案

           zipFile

(file,zos,out);

       }

       out.close();

       System.out.println("Checksum-ZIP: "+csumo.getChecksum().getValue());

    }

    //壓縮資料夾,遞迴壓縮資料夾中所有檔案

    public static void zipFolder(File f, ZipOutputStream zos,BufferedOutputStream out,StringzipPath) throws IOException{

       if(f.isDirectory()){

           File[] fs=f.listFiles();

           if(fs.length==0)return;

           if(zipPath!=null && !"".equals(zipPath)){

              zipPath=zipPath+"/";

              zos.putNextEntry(new ZipEntry(zipPath));

           }

           for(File ff:fs){

              zipFolder(ff,zos,out,zipPath+ff.getName());

           }  

       }else{

           zos.putNextEntry(new ZipEntry(zipPath));

           BufferedInputStream in=new BufferedInputStream(new FileInputStream(f));

           int i;

           while((i=in.read())!=-1){

              out.write(i);

           }

           in.close();

           out.flush();

       }

    }

    //壓縮單個檔案

    public static void zipFile(File f,ZipOutputStream zos,BufferedOutputStream out) throws IOException{

       zos.putNextEntry(new ZipEntry(f.getName()));

       BufferedInputStream in=new BufferedInputStream(new FileInputStream(f));

       int i;

       while((i=in.read())!=-1){

           out.write(i);

       }

       in.close();

       out.flush();

    }

    //解壓

    public static void unZipFile(String srcPath, String destPath) throws IOException{

       File f=new File(srcPath);

       ZipFile zf=new ZipFile(f);

       destPath=destPath+f.getName().replace(".zip", "/");

       FileInputStream fis=new FileInputStream(srcPath);

       CheckedInputStream csumi=new CheckedInputStream(fis,new CRC32());

       ZipInputStream zis=new ZipInputStream(csumi);

       java.util.zip.ZipEntry ze;

       while((ze=zis.getNextEntry())!=null){

           File temp=new File(destPath+ze.getName());

           if(ze.isDirectory()){

              temp.mkdirs();

           }else{

              InputStream in =zf.getInputStream(ze);

              BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(temp));

              int len;

              while((len=in.read())!=-1){

                  out.write(len);

              }

              out.flush();

              in.close();

              out.close();

           }

       }

       zis.close();

       System.out.println("Checksum-UNZIP: "+csumi.getChecksum().getValue());   

    }