Java 如何使用压缩包批量导出或下载文件

import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

Java的打包代码如下

// 压缩文件
	public static void zipFiles(java.io.File[] srcfile, java.io.File zipfile) {
		byte[] buf = new byte[1024];
		try {
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
			for (int i = 0; i < srcfile.length; i++) {
				FileInputStream in = new FileInputStream(srcfile[i]);
				out.putNextEntry(new ZipEntry(srcfile[i].getName()));
				int len;
				while ((len = in.read(buf)) > 0) {
					out.write(buf, 0, len);
				}
				out.closeEntry();
				in.close();
			}
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

如何将文件放入到压缩包

 List<String> listFile=new ArrayList<String>();
// 循环将文件路径add到listFile中
 File srcfile[] = new File[listFile.size()];  
    for (int i = 0, n1 = listFile.size(); i < n1; i++) {  
	    srcfile[i] = new File(listFile.get(i));  
    } 
 ExportExcelBatch.ziles(srcfile, zip); 

 

上一篇:python-异常


下一篇:激活IDEA方法