导包
入口
public static void writeCsvAsZip(HttpServletResponse response, List<CsvEntity> entities, String fileName, String password) { String csv = ".csv"; String zip = ".zip"; String url = ""; try { File csvFile = CsvCreator.out(entities); url = csvFile.getPath(); url = url.substring(0, url.length() - 3); fileChannelCopy(csvFile.getPath(), url + csv); makeZip(url + zip, new File(url + csv), password); StringBuilder uri = new StringBuilder(); uri.append(url); uri.append(zip); downloadFile(response, uri, fileName); } catch (IOException e) { AxaNaviLogHelper.error("CSV出力失败", e); throw new SystemException(ERROR, ErrorCode.CSV_OUT_ERROR, "CSV出力失败。", e); } catch (ZipException e) { AxaNaviLogHelper.error("CSV出力失败。", e); throw new SystemException(ERROR, ErrorCode.CSV_OUT_ERROR, "CSV出力失败。", e); } finally { deleteFile(url + csv); deleteFile(url + zip); } }
文件复制
public static void fileChannelCopy(String ChangeBeforeName, String ChangeAfterName) { File s = new File(ChangeBeforeName); File t = new File(ChangeAfterName); FileInputStream fi = null; FileOutputStream fo = null; FileChannel in = null; FileChannel out = null; try { fi = new FileInputStream(s); fo = new FileOutputStream(t); in = fi.getChannel(); out = fo.getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); } finally { try { fi.close(); in.close(); fo.close(); out.close(); } catch (IOException e) { AxaNaviLogHelper.error("CSV失败。", e); throw new SystemException(ERROR, ErrorCode.CSV_OUT_ERROR, "CSV失败。", e); } } }
压缩加密
引用zip4j_1.3.2.jar包方法
public static void makeZip(String zipFileName, File inputFile, String password) throws ZipException { ZipFile zipFile = new ZipFile(zipFileName); ArrayList<File> filesToAdd = new ArrayList<File>(); filesToAdd.add(inputFile); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); if (!StringUtils.isEmpty(password)) { parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(password); } zipFile.addFiles(filesToAdd, parameters); }
下载
public static void downloadFile(HttpServletResponse response, StringBuilder uri, String fileName) throws IOException { StringBuffer filename = new StringBuffer(); filename.append(uri); File file = new File(filename.toString()); StringBuffer sb = new StringBuffer(); sb.append("attachment; filename=").append(fileName); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentType("application/x-msdownload;charset=UTF-8"); response.setHeader("Content-Disposition", new String(sb.toString() .getBytes(), "Windows-31J")); FileInputStream inputStream = new FileInputStream(file); OutputStream outputStream = response.getOutputStream(); byte[] buffer = new byte[1024]; int i = -1; while ((i = inputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } outputStream.flush(); outputStream.close(); inputStream.close(); }
删除临时文件
public static void deleteFile(String sPath) { File file = new File(sPath); if (file.isFile() && file.exists()) { file.delete(); } }
具体参考
http://www.open-open.com/lib/view/open1378556210553.html
相关推荐
zip4j_1.3.2.jar直接导入到工程中即可,示例代码可以见我的博客
net.lingala.zip4j.core.ZipFile_1.3.2.jar 解压缩zip,带解压进度
1.读/写受密码保护的Zip文件 2.创建,添加,提取,更新,从Zip文件中删除文件
zip4j默认采用UTF-8编码,所以它支持中文,同时也支持密码,而且支持多种压缩算法,可以说功能强大,但使用起来却非常简单,当然,如果需求比较复杂,那就得好好去研究了。如果你仅仅是简单地解压一个zip压缩文件,...
runjettyrun.jetty8_1.3.2.jar
zip4j-1.3.2.jar,已经测试,可以正常使用,支持压缩本地文件,服务器文件.支持AES 128/256算法加密,支持标准Zip算法加密,支持zip64格式
java项目中压缩解压jar包,附工具类代码,及测试main函数。
zip4j-1.3.2zip4j-1.3.2
这是最新版本的zip4j,版本号是1.3.2 官网上没有提供api文档,我生了一份,一并打在zip包中,希望能有所帮助。。。 为了方便大家,本资源不设下载积分。 Java操作zip压缩格式的开源项目,功能强大而且使用方便,...
包含 最新的zip4j_1.3.2.jar包,例子及API文档。 希望大家在对zip文件操作时有所帮助。 官网地址 http://www.lingala.net/zip4j/ Key features: Create, Add, Extract, Update, Remove files from a Zip file ...
最新zip4j包,版本1.3.2 1.读/写受密码保护的Zip文件 2.创建,添加,提取,更新,从Zip文件中删除文件
蓝牙A2DP最新协议版本,A2DP_v1.3.2.pdf;蓝牙A2DP最新协议版本,A2DP_v1.3.2.pdf;蓝牙A2DP最新协议版本,A2DP_v1.3.2.pdf;蓝牙A2DP最新协议版本,A2DP_v1.3.2.pdf;蓝牙A2DP最新协议版本,A2DP_v1.3.2.pdf;蓝牙A...
Apache的FileUpload1.3.2 jar包,需要JDK1.5+
zigbee作为一种短距离、低功耗、低数据传输速率的无线网络技术,它是介于无线标记技术和蓝牙之间的技术方案,在传感器网络等领域应用非常广泛,这得益于它强大的组网能力,可以形成星型、树型和网状网三种zigbee网络...
commons-fileupload-1.3.2.jar和commons-io-2.5.jar
包含commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar 下载即可使用
android zip4j jar 包下载 下载后解压使用即可 zip4j 1.3.2.jar
shiro-all-1.3.2.jar
commons-fileupload-1.2.1.jar和commons-io-1.3.2.jar