JAVA,导出CSV,设最大行数限制,使用winzipaes压缩成带密码的zip文件
2011-10-21 11:22
429 查看
由于工作需要,导出CSV功能,太大的话要分成多个,并且导出文件要压缩成带密码的zip。
JAVA本身的ZIP输入输出流是支持多个文件的,但是没有设置密码功能。
网上搜索了很久,最后选择使用开源的winzipaes。
地址 http://code.google.com/p/winzipaes/
下载它的source,自己编译生成jar。在编译时,要用到bcprov-jdk jar包,到下面的网页里下载。
目前的winzipaes_src_20110911.zip,官网推荐使用bcprov-jdk15-146.jar
http://www.bouncycastle.org/latest_releases.html
1. 使用示例(源码里的例子,没测试过)
压缩
解压
2. 压缩多个文件时,有两个方法
(1) 预先把多个文件压缩成zip,使用enc.addAll(inZipFile, password);方法。
(2)循环调用enc.add(inFile, password);,每次用相同的密码
3. 压缩包里的path
使用add(File file, String password)方法,会把file的路径(绝对路径或相对路径,有创建file时决定)自动作为zip里的path。
不想要路径或者想自己指定的话,使用add(File file, String password)方法。
4. 不用inFile,通过流创建(省却创建临时文件,我就是用这个)
使用add(String name, InputStream is, String password)方法。
5. 我的导出CSV并带密码压缩代码(大体样子,不完全)
遗留问题(有时间研究一下代码看看有什么原因和解决方法):
1. 好像没有不设密码的方法
2. 压缩包里的文件名会出现乱码
JAVA本身的ZIP输入输出流是支持多个文件的,但是没有设置密码功能。
网上搜索了很久,最后选择使用开源的winzipaes。
地址 http://code.google.com/p/winzipaes/
下载它的source,自己编译生成jar。在编译时,要用到bcprov-jdk jar包,到下面的网页里下载。
目前的winzipaes_src_20110911.zip,官网推荐使用bcprov-jdk15-146.jar
http://www.bouncycastle.org/latest_releases.html
1. 使用示例(源码里的例子,没测试过)
压缩
File inFile; File outFile; ... AESEncrypter encrypter = new AESEncrypterBC(); AesZipFileEncrypter enc = new AesZipFileEncrypter(outFile,encrypter); try { enc.add(inFile, password); } finally { enc.close(); }
解压
AesZipFileDecrypter zipFile = new AesZipFileDecrypter( new File("doc/zipSpecificationAes.zip"), new AESDecrypterBC() ); ExtZipEntry entry = zipFile.getEntry( "zipSpecification.txt" ); zipFile.extractEntryWithTmpFile( entry, new File("doc/zipSpecification.txt"), "foo" );
2. 压缩多个文件时,有两个方法
(1) 预先把多个文件压缩成zip,使用enc.addAll(inZipFile, password);方法。
(2)循环调用enc.add(inFile, password);,每次用相同的密码
3. 压缩包里的path
使用add(File file, String password)方法,会把file的路径(绝对路径或相对路径,有创建file时决定)自动作为zip里的path。
不想要路径或者想自己指定的话,使用add(File file, String password)方法。
4. 不用inFile,通过流创建(省却创建临时文件,我就是用这个)
使用add(String name, InputStream is, String password)方法。
5. 我的导出CSV并带密码压缩代码(大体样子,不完全)
List<DTO> dtoList = getData(); OutputStream out = null; AesZipFileEncrypter enc = null; byte[] bom ={(byte) 0xEF,(byte) 0xBB,(byte) 0xBF}; try { out = response.getOutputStream(); enc = new AesZipFileEncrypter(out, new AESEncrypterBC()); int totalRowNum = dtoList.size(); int rowNum = 0; final int MAX_ROW_NUM = 1000; int fileNum = 0; String csvData = ""; for (DTO dto : dtoList) { rowNum++; // edit csv data ... if (rowNum % MAX_ROW_NUM == 0 || rowNum == totalRowNum) { // excel打开UTF乱码的对应 byte[] csvDataBytes = csvData.getBytes(); byte[] outputDataBytes = new byte[csvDataBytes.length + bom.length]; System.arraycopy(bom, 0, outputDataBytes , 0, bom.length); System.arraycopy(csvDataBytes , 0, outputDataBytes , bom.length, csvDataBytes .length); DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(outputDataBytes )); // 添加到zip enc.add((++fileNum) + ".csv", dataInputStream, “123456”); if (rowNum < totalRowNum) { csvData = ""; } } } out.flush(); } catch (IOException e) { e.printStackTrace(); } finally { if (enc != null) {enc.close();} if (out != null) {out.close();} }
遗留问题(有时间研究一下代码看看有什么原因和解决方法):
1. 好像没有不设密码的方法
2. 压缩包里的文件名会出现乱码
相关文章推荐
- JAVA使用7-zip解压缩带密码的Zip文件(非Proccess方法)
- PLSQL导出大量数据-超出excel限制,使用csv
- Java 解压和压缩密码zip文件
- Java解压和压缩带密码的zip文件(转…
- java 使用7-zip解压缩带密码的压缩文件(非Proccess方法)
- PLSQL导出大量数据-超出excel限制,使用csv
- 让<EditView>最大行数为一行,在账号和密码输入时使用
- 使用java解压或者压缩zip文件
- 使用BO JAVA SDK打开WEBI报表,并进行PDF、EXCEL、CSV、HTML导出
- Java解压和压缩带密码的zip文件
- java 压缩解压zip文件--使用ant支持中文
- 命令行使用7-zip进行有密码的压缩
- sqluldr2全量数据库导入导出和java-安装和使用(2)
- 如何为ios酷我音乐盒下载导出的音乐文件(使用Java程序设计)
- JAVA使用POI根据模版导出EXCEL
- 使用sqlplus工具导出数据到csv文件,要求文件带有时间戳
- JAVA使用POI如何导出百万级别数据
- java 导出成.csv文件的乱码问题
- Java实现从网页表格导出CSV文件的例子
- Java 导出CSV文件及实现web下载CSV