成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

Java實現文件自動打包成zip并下載的代碼

2010-10-19 09:56:14來源:作者:

  import java.io.File;

  import java.io.File;

  import java.io.FileInputStream;

  import java.io.FileOutputStream;

  import java.util.zip.ZipEntry;

  import java.util.zip.ZipOutputStream;

  public class ZipOutputStreamDemo {

  public static void main(String[] args) throws Exception {

  byte[] buffer = new byte[1024];

  //生成的ZIP文件名為Demo.zip

  String strZipName = "Demo.zip";

  ZipOutputStream out = new ZipOutputStream(new FileOutputStream(strZipName));

  //需要同時下載的兩個文件result.txt ,source.txt

  File[] file1 = {new File("result.txt"),new File("source.txt")};

  for(int i=0;i<file1.length;i++) {

  FileInputStream fis = new FileInputStream(file1[i]);

  out.putNextEntry(new ZipEntry(file1[i].getName()));

  int len;

  //讀入需要下載的文件的內容,打包到zip文件

  while((len = fis.read(buffer))>0) {

  out.write(buffer,0,len);

  }

  out.closeEntry();

  fis.close();

  }

  out.close();

  System.out.println("生成Demo.zip成功");

  }

關鍵詞:Java
主站蜘蛛池模板: 桂阳县| 新田县| 西乌| 四川省| 巴彦淖尔市| 中方县| 洪湖市| 台南县| 汝城县| 合阳县| 平山县| 溆浦县| 大城县| 肇源县| 习水县| 桂林市| 湟中县| 临夏市| 孟州市| 府谷县| 肥城市| 合水县| 南乐县| 贡觉县| 铁岭市| 亳州市| 定州市| 嘉鱼县| 通河县| 南昌县| 莱州市| 五华县| 驻马店市| 长沙市| 黔南| 绩溪县| 南宫市| 石林| 绥芬河市| 抚远县| 黄浦区|