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


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

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

你可能感兴趣的:(C++,c,C#,J#)