poi多文件下载

该博客展示了如何使用Java的POI库来打包多个文件并生成ZIP文件。主要涉及了`ZipOutputStream`和`FileInputStream`的使用,通过遍历文件列表,将每个文件读取并写入到ZIP输出流中,实现文件的压缩和合并。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.poi;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class PoiDemo {
public static void main(String[] args) throws Exception{

    /*List<File> srcfile=new ArrayList<File>();  
    srcfile.add(new File("C:/document/excel\\1.xlsx"));  
    srcfile.add(new File("C:/document/excel\\2.xlsx"));  
    srcfile.add(new File("C:/document/excel\\3.xlsx"));  
    srcfile.add(new File("C:/document/excel\\4.xlsx"));  
    srcfile.add(new File("C:/document/excel\\5.xlsx"));  

    File zipfile = new File("e:\\edm11.zip");  
    ZipFiles(srcfile, zipfile);  */
    String path = "C:/document/excel";
    createFile(path);


    File files[]={new File("C:/document/excel\\1.xlsx"),new File("C:/document/excel\\1.xlsx"),new File("C:/document/excel\\1.xlsx"),new File("C:/document/excel\\1.xlsx"),new File("C:/document/excel\\1.xlsx")};


    Export(path,files);

    File file = new File(path);

    deleteExcelPath(file);
}

/* public static void ZipFiles(List srcfile, File zipfile) {
byte[] buf = new byte[1024];
try {
// Create the ZIP file
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// Compress the files
for (int i = 0; i < srcfile.size(); i++) {
File file = srcfile.get(i);
FileInputStream in = new FileInputStream(file);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(file.getName()));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
*/
public static String createFile(String path){
File file = new File(path);
//判断文件是否存在;
if(!file.exists()){
//创建文件;
boolean bol = file.mkdirs();
if(bol){
System.out.println(path+” 路径创建成功!”);
}else{
System.out.println(path+” 路径创建失败!”);
}
}else{
System.out.println(path+” 文件已经存在!”);
}
return path;
}

 public  static void  Export(String path,File[] files) throws Exception{
        ZipOutputStream zipOutputStream = null;
        File file = new File(path+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".zip");
        zipOutputStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
        files = new File(path).listFiles();
        FileInputStream fileInputStream = null;
        byte[] buf = new byte[1024];
        int len = 0;
        if(files!=null && files.length>0){
            for(File excelFile:files){
                String fileName = excelFile.getName();
                fileInputStream = new FileInputStream(excelFile);
                //放入压缩zip包中;
                zipOutputStream.putNextEntry(new ZipEntry(fileName));

                //读取文件;
                while((len=fileInputStream.read(buf)) >0){
                    zipOutputStream.write(buf, 0, len);
                }
                //关闭;
                zipOutputStream.closeEntry();
                if(fileInputStream != null){
                    fileInputStream.close();
                }
            }
        }

        if(zipOutputStream !=null){
            zipOutputStream.close();
        }

 }
 public static boolean deleteExcelPath(File file){
        String[] files = null;
        if(file != null){
            files = file.list();
        }

        if(file.isDirectory()){
            for(int i =0;i<files.length;i++){
                boolean bol = deleteExcelPath(new File(file,files[i]));
                if(bol){
                    System.out.println("删除成功!");
                }else{
                    System.out.println("删除失败!");
                }
            }
        }
        return file.delete();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值