编程语言
首页 > 编程语言> > Java 如何使用压缩包批量导出或下载文件

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); 

 

标签:java,Java,srcfile,导出,listFile,File,new,压缩包,out
来源: https://blog.csdn.net/oWangXiaoPeng/article/details/115477232