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