忙了一天就為了把幾個文件變成zip包:
發(fā)現(xiàn):1,java的zip中文有問題用ant的
???????????? 2,只能生成zip,不能往原有的zip里面添加?xùn)|西
/**
? *
? * @param list 準(zhǔn)備壓縮文件bean列表,bean里面是壓縮文件的路進和名字
? * @param outDir 輸出路徑
? * @param outName? 輸出文件名
? */
?
?public static void zipFilesAntJar(List list,String outDir,String outName){
??FileOutputStream fout;
??org.apache.tools.zip.ZipOutputStream zipOut;
??try {
???fout = new FileOutputStream(outDir+outName);
???
???zipOut = new org.apache.tools.zip.ZipOutputStream(fout);
???for (Iterator iter = list.iterator(); iter.hasNext();) {
????FileBean fb = (FileBean) iter.next();
????
?????FileInputStream fin = new FileInputStream(fb.getPathName());
?????
?????zipOut.setEncoding("GBK");
?????zipOut.putNextEntry(new org.apache.tools.zip.ZipEntry(fb.getName()));
?????
?????int number = 0;
?????byte[] b = new byte[1024];
?????while((number=fin.read(b))!=-1){
??????zipOut.write(b, 0, number);
?????}
?????zipOut.flush();
?????fin.close();
????
???}
???zipOut.close();
???fout.close();
??} catch (FileNotFoundException e) {
???e.printStackTrace();
??} catch (IOException e) {
???e.printStackTrace();
??}
??
?}
public class FileBean {
?private String path;
?private String name;
?
?public FileBean(String path,String name){
??this.path = path;
??this.name = name;
?}
?public String getName() {
??return name;
?}
?public void setName(String name) {
??this.name = name;
?}
?public String getPath() {
??return path;
?}
?public void setPath(String path) {
??this.path = path;
?}
?public String getPathName(){
??return path+name;
?}
}
?