package test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class DaBao_ZIP {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
byte[] buffer = new byte[1024];
//生成的ZIP文件名为Demo.zip
String strZipName = "e:/Demo.zip";
ZipOutputStream out;
try {
out = new ZipOutputStream(new FileOutputStream(strZipName));
//需要打包的文件
File[] file1 = {new File("e:/test2/a.txt"),new File("e:/test2/b.txt"),new File("e:/test2/aa.txt"),new File("e:/test2/bb.txt")};
for(int i=0;i
FileInputStream fis = new FileInputStream(file1[i]);
try {
out.putNextEntry(new ZipEntry(file1[i].getName()));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int len;
//读入需要下载的文件的内容,打包到zip文件
try {
while((len = fis.read(buffer))>0) {
out.write(buffer,0,len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.closeEntry();
fis.close();
}
out.close();
System.out.println("生成Demo.zip成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//需要同时下载的两个文件result.txt ,source.txt
}
}