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