批量下载文件,IO流

  1. import java.io.File;  
  2. import java.io.FileInputStream;  
  3.   
  4. import java.io.FileOutputStream;  
  5.   
  6. import java.util.zip.ZipEntry;  
  7.   
  8. import java.util.zip.ZipOutputStream;   
  9.   
  10. public class ZipOutputStreamDemo {  
  11.   
  12.     public static void main(String[] args) throws Exception {  
  13.   
  14.        byte[] buffer = new byte[1024];  
  15.   
  16.        //生成的ZIP文件名为Demo.zip  
  17.   
  18.        String strZipName = "Demo.zip";  
  19.   
  20.        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));  
  21.   
  22.        //需要同时下载的两个文件result.txt ,source.txt  
  23.   
  24.        File[] file1 = {new File("result.txt"),new File("source.txt")};  
  25.   
  26.        for(int i=0;i<file1.length;i++) {  
  27.   
  28.            FileInputStream fis = new FileInputStream(file1[i]);  
  29.   
  30.            out.putNextEntry(new ZipEntry(file1[i].getName()));  
  31.   
  32.            int len;  
  33.   
  34.            //读入需要下载的文件的内容,打包到zip文件  
  35.   
  36.           while((len = fis.read(buffer))>0) {  
  37.   
  38.            out.write(buffer,0,len);   
  39.   
  40.           }  
  41.   
  42.            out.closeEntry();  
  43.   
  44.            fis.close();  
  45.   
  46.        }  
  47.   
  48.         out.close();  
  49.   
  50.         System.out.println("生成Demo.zip成功");  
  51.   
  52.     }  
  53.   
  54. }  
posted @ 2014-07-28 16:01  Kevin_Zhou_9  阅读(331)  评论(0)    收藏  举报