导出csv类型文件并创建压缩包

1:`@Test
public void getCsvTest() throws Exception{
//文件名
String fileName = “testCsv2”;
//导出路径(路径最前面要有/,不然会导出失败)
Path path = Paths.get("/Users/ldp/export", “export”, fileName);
BufferedWriter bufferedWriter = null;
File filePath = new File(Paths.get("/Users/ldp/export", “export”, fileName).toString());
boolean pathExists;
//如果路径不存在就创建文件夹
if (!filePath.exists()) {
pathExists = filePath.mkdirs();
} else {
pathExists = true;
}
//第一次创建叫part-0,之后part-1
File[] files = filePath.listFiles();
int initial = -1;
if (pathExists) {
assert files != null;
if (files.length == 0) {
bufferedWriter = Files
.newBufferedWriter(path.resolve(“part-” + 0 + “.csv”),
Charset.forName(“GB18030”), StandardOpenOption.CREATE);
} else {
for (File f : files) {
String str = f.getName().split("-")[1];
int t = Integer.valueOf(str.substring(0, str.length() - 4));
if (t > initial) {
initial = t;
}
}
initial++;
bufferedWriter = Files.newBufferedWriter(path.resolve(“part-” + initial + “.csv”),
Charset.forName(“GB18030”), StandardOpenOption.CREATE);
}
//使用/n换行,先写标题
StringBuilder builder = new StringBuilder();
builder.append(“姓名,”).append(“年龄,”).append("\n");;
bufferedWriter.write(builder.toString());
//导出内容
StringBuilder sb = new StringBuilder();
sb.append(“赵,”).append(“1,”).append("\n").append(“钱,”).append(“2”);
bufferedWriter.write(sb.toString().toCharArray());
bufferedWriter.flush();
bufferedWriter.close();
}
//生成压缩包
String sourcePath = Paths.get("/Users/ldp/export", “export”, fileName)
.toString();
String zipPath = Paths.get("/Users/ldp/export", “export”, fileName + “.zip”)
.toString();
createZip(sourcePath, zipPath);
}
/**

  • 压缩文件
  • @param sourcePath 源文件目录
  • @param zipPath 压缩后文件的全路径
    */
    private void createZip(String sourcePath, String zipPath) throws Exception {
    FileOutputStream fos = null;
    ZipOutputStream zos = null;
    try {
    fos = new FileOutputStream(zipPath);
    zos = new ZipOutputStream(fos);
    zos.setEncoding(“GBK”);//此处修改字节码方式。
    // createXmlFile(sourcePath,“293.xml”);
    writeZip(new File(sourcePath), “”, zos);
    } catch (Exception e) {
    log.error(“创建ZIP文件失败”, e);
    throw e;
    } finally {
    try {
    if (zos != null) {
    zos.close();
    }
    if (fos != null) {
    fos.close();
    }
    } catch (IOException e) {
    log.error(“创建ZIP文件失败”, e);
    }
}

}
private void writeZip(File file, String parentPath, ZipOutputStream zos) {
if (file.exists()) {
if (file.isDirectory()) {// 处理文件夹
parentPath += file.getName() + File.separator;
File[] files = file.listFiles();
if (files.length != 0) {
for (File f : files) {
writeZip(f, parentPath, zos);
}
} else { // 空目录则创建当前目录
try {
zos.putNextEntry(new ZipEntry(parentPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} else {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
ZipEntry ze = new ZipEntry(parentPath + file.getName());
zos.putNextEntry(ze);
byte[] content = new byte[1024];
int len;
while ((len = fis.read(content)) != -1) {
zos.write(content, 0, len);
zos.flush();
}

    } catch (FileNotFoundException e) {
      log.error("创建ZIP文件失败", e);
    } catch (IOException e) {
      log.error("创建ZIP文件失败", e);
    } finally {
      try {
        if (fis != null) {
          fis.close();
        }
      } catch (IOException e) {
        log.error("创建ZIP文件失败", e);
      }
    }
  }
}

}
`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值