如何把多个CSV文件压缩zip格式输出

[list]
1. 处理CSV文件很简单,因为我们强大的Offic 就可直接打开CSV格式
[/list]

public class ZipServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// Test data
List<Object> listObject1 = new ArrayList<Object>();
List<Object> listObject2 = new ArrayList<Object>();
List<Object> listObject3 = new ArrayList<Object>();

listObject1.add("a");
listObject2.add("b");
listObject3.add("c");

List<List<Object>> objects = new ArrayList<List<Object>>();
objects.add(listObject1);
objects.add(listObject2);
objects.add(listObject3);

String projectLocalPath = "D:/tomcat6.18/webapp/ROOT/pictures/zip_files/";
String exportedFileName = "name1";
fileInputStream(objects,projectLocalPath, response, exportedFileName);
fileOutputStream(response, projectLocalPath, exportedFileName);
delAllFiles(projectLocalPath);
}

private static void fileInputStream(List<List<Object>> csvList,String projectLocalPath, HttpServletResponse response, String exportedFileName) {
setCSVContentType(response, exportedFileName);
List<Object[]> listObj = new ArrayList<Object[]>();
int size = csvList.size();
for (int i = 0; i < size; i++) {
List<Object> listObject = csvList.get(i);
for(int j = 0; j < listObject.size(); j ++) {
Object[] objects = new Object[size];
objects[i] = listObject.get(j);
listObj.add(objects);
}
}

try {
for (int i = 0; i <listObj.size(); i++) {
File tempFile = new File(projectLocalPath+"/out"+i+".csv");
CSVWriter writer = new CSVWriter(new FileWriter(tempFile));
Object[] obj = listObj.get(i);
for(int j = 0; j < obj.length; j++) {
if(null != obj[j]) {
String value = String.valueOf(obj[i]);
String values[] = new String[1];
values[0] = value;
writer.writeNext(values);
}
}
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

private static void fileOutputStream(HttpServletResponse response,
String projectLocalPath, String exportedFileName) throws IOException {
setZIPContentType(response, exportedFileName);
File file = new File(projectLocalPath);
File[] listFiles = file.listFiles();
ZipOutputStream out = null;
try {
out = new ZipOutputStream(response.getOutputStream());
for (int i = 0; i < listFiles.length; i++) {
file = listFiles[i];
writeZipOutputStream(file, out, "");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
out.close();
}
}

@SuppressWarnings("unused")
private synchronized static void writeZipOutputStream(File inputFile, ZipOutputStream out,String base) throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
writeZipOutputStream(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
}
public static boolean delAllFiles(String path) {
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
return flag;
}
if (!file.isDirectory()) {
return flag;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFiles(path + "/" + tempList[i]);
flag = true;
}
}
return flag;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值