1. package compress.javaio; 
  2.  
  3. import java.io.*; 
  4. import java.util.zip.*; 
  5.  
  6. public class CompressFiles{ 
  7.  
  8.     /** 
  9.      * @param args 
  10.      * @throws IOException  
  11.      */ 
  12.     public static void main(String[] args) throws IOException { 
  13.         // TODO Auto-generated method stub 
  14.         String sourPath=File.separator+"root"+File.separator+"ziptest02"
  15.         File file=new File(sourPath); 
  16.         ZipOutputStream zipOut=null;//此对象需在主函数中创建 
  17.         zipOut=new ZipOutputStream(new FileOutputStream(File.separator+"root"+File.separator+"ziptest02.zip")); 
  18.         String str=file.getName(); 
  19.         Recurrence(file,zipOut,str);//调用递归函数,判断该当前项是目录或者文件 
  20.         zipOut.close(); 
  21.         System.out.println("压缩流已经关闭"); 
  22.     } 
  23.      
  24.     private static void Recurrence(File file,ZipOutputStream zipOut,String str) throws IOException{ 
  25.         //TODO 判断该项是否是文件,若是文件,直接传给Compress()函数处理 
  26.         if(file.isDirectory()){ 
  27.             File files[]=file.listFiles(); 
  28.             for(int i=0;i<files.length;i++){ 
  29.                 Recurrence(files[i],zipOut,str); 
  30.             } 
  31.         }else
  32.             Compress(file,zipOut,str); 
  33.         } 
  34.     } 
  35.  
  36.     private static void Compress(File file,ZipOutputStream zipOut,String str) throws IOException { 
  37.         // TODO Auto-generated method stub 
  38.         InputStream input=null
  39.         input=new FileInputStream(file); 
  40.         //String str1=str+File.separator+file.getName(); 
  41.         System.out.println("此次处理文件为:"+file.getPath()); 
  42.         ZipEntry entry=new ZipEntry(file.getPath()); 
  43.         zipOut.putNextEntry(entry); 
  44.         int temp=0
  45.         while((temp=input.read())!=-1){ 
  46.             zipOut.write(temp); 
  47.         } 
  48.         input.close(); 
  49.     } 
  50.