欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java批量将多文件打包成zip格式

程序员文章站 2022-06-12 09:54:31
...
public void createzip(){
		List<File> nFileList = new ArrayList<File>();
		nFileList.add(new File("C:\\Users\\Administrator\\Desktop\\测试移动到\\0001+国务院办公厅关于积极推进供应链创新与应用的指导意见.doc"));
		nFileList.add(new File("C:\\Users\\Administrator\\Desktop\\测试移动到\\0001+国务院办公厅关于积极推进供应链创新与应用的指导意见.pdf"));
		nFileList.add(new File("C:\\Users\\Administrator\\Desktop\\zenith.product.fms.main.js"));
		String strZipName = "C:\\Users\\Administrator\\Desktop\\lx.zip"; 
		FileInputStream nFileInputStream = null;
		ZipOutputStream nZipOutputStream = null;
		try {
			nZipOutputStream = new ZipOutputStream(new FileOutputStream(strZipName));
			for (File file : nFileList) {
				nFileInputStream = new FileInputStream(file);
				nZipOutputStream.putNextEntry(new ZipEntry(file.getName()));
				int len;
				while ((len = nFileInputStream.read()) != -1) {
					nZipOutputStream.write(len);
					nZipOutputStream.flush();
				}
			}
			nZipOutputStream.close();
			nFileInputStream.close();
			System.out.println("生成lx.zip成功");   
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
相关标签: java file