1、/***递归压缩文件夹*@paramsrcRootDir压缩文件夹根目录的子路径*@paramfile当前递归压缩的文件或目录对象*@paramzos压缩文件存储对象*@throwsException*/privatestaticvoidzip(StringsrcRootDir,Filefile,ZipOutputStreamzos)throwsException{if(file==null){return;}
2、 //如果是文件,则直接压缩该文件if(file.isFile()){intcount,bufferLen=1024;bytedata[]=newbyte[bufferLen];//获取文件相对于压缩文件夹根目录的子路径StringsubPath=file.getAbsolutePath();intindex=subPath.indexOf(srcRootDir);if(index!=-1){
3、subPath=subPath.substring(srcRootDir.length()+File.separator.length());}ZipEntryentry=newZipEntry(subPath);zos.putNextEntry(entry);BufferedInputStreambis=newBufferedInputStream(newFileInputStream(file));while((count=bis.read(data,0,bufferLen))!=-1){zos.write(data,0,count);}bis.close();zos.closeEntry();}//如果是目录,则压缩整个目录else{
4、 //压缩目录中的文件或子目录File[]childFileList=file.listFiles();for(intn=0;n<c茑霁酌绡hildFileList.length;n++){childFileList[n].getAbsolutePath().indexOf(file.getAbsolutePath());zip(srcRootDir,childFileList[n],zos);}}}/***对文件或文件目录进行压缩*@paramsrcPath要压缩的源文件路径。如果压缩一个文件,则为该文件的全路径;如果压缩一个目录,则为该目录的顶层目录路径*@paramzipPath压缩文件保存的路径。注意:zipPath不能是srcPath路径下的子文件夹*@paramzipFileName压缩文件名*@throwsException*/
5、 publicstaticvoidzip(StringsrcPath,StringzipPath,StringzipFileName)throwsException{if(StringUtils.isEmpty(srcPath)||StringUtils.isEmpty(zipPath)||StringUtils.isEmpty(zipFileName)){thrownewParameterException(ICommonResultCode.PARAMETER_IS_NULL);}CheckedOutputStreamcos=null;ZipOutputStreamzos=null;try{FilesrcFile=newFile(srcPath);