1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 将多个excel表格打包成一个压缩包文件

将多个excel表格打包成一个压缩包文件

时间:2023-01-31 08:57:18

相关推荐

将多个excel表格打包成一个压缩包文件

上一篇文章讲解了如何生成excel文件,name这一章我们来谈谈如何将多个excel文件打包成压缩包的形式

首先看一下主函数:

public static void main(String[] args) throws IOException {try {File file1 = new File("students1.xls");File file2 = new File("students2.xls");File file3 = new File("students3.xls");//创建三个文件,放入list中ArrayList list = new ArrayList();list.add(file1);list.add(file2);list.add(file3);//创建临时压缩包File file = new File("e:/certpics.rar");//创建文件输入输出流FileOutputStream fous = new FileOutputStream(file); /**打包的方法我们会用到ZipOutputStream这样一个输出流*/ZipOutputStream zipOut= new ZipOutputStream(fous);//进入打包方法zipFile(list, zipOut);zipOut.close();fous.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

主函数里面包含一个zipFile()的方法,接下来展示给大家:

/*** 把接受的全部文件打成压缩包*/@SuppressWarnings("rawtypes")public static void zipFile(List files,ZipOutputStream outputStream) {int size = files.size();for(int i = 0; i < size; i++) {File file = (File) files.get(i);zipFile(file, outputStream);}}

上面的函数里面存在一个zipFile()的函数,注意啊,这个是方法的参数可是不同的啊。

/** * 根据输入的文件与输出流对文件进行打包* @param File* @param org.apache.tools.zip.ZipOutputStream*/public static void zipFile(File inputFile,ZipOutputStream ouputStream) {System.out.println(inputFile.getName());try {ZipEntry entry1 = new ZipEntry(inputFile.getName());ouputStream.putNextEntry(entry1);} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}

运行之后你就可以去你的e盘下面找到这个叫certpics.rar的压缩包了。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。