扩展zip4j内存加密压缩包
import java.io.ByteArrayOutputStream;import java.io.FileOutputStream;import java.io.IOException;import net.lingala.zip4j.exception.ZipException;import net.lingala.zip4j.io.ZipOutputStream;import net.lingala.zip4j.model.ZipModel;import net.lingala.zip4j.model.ZipParameters;import net.lingala.zip4j.util.Zip4jConstants;import net.lingala.zip4j.zip.ZipEngine;public class Test extends ZipEngine{private ZipModel zipModel;public Test(ZipModel zipModel) throws ZipException {super(zipModel);this.zipModel=zipModel;}/*** @param args*/public static void main(String[] args) {TestSendAttatch att=null;try {att = new TestSendAttatch(new ZipModel());att.doZipEnc();System.out.println("成功");} catch (Exception e) {System.out.println("失败");e.printStackTrace();}}public void doZipEnc() {try {//将压缩流写到内存ByteArrayOutputStream saos = new ByteArrayOutputStream();//本地测试FileOutputStream f = new FileOutputStream("d:\\test1.zip" ,true);for(int i=0;i<1;i++){String passwd = "111";ZipParameters parameters = new ZipParameters();parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // 压缩方式parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 压缩级别parameters.setSourceExternalStream(true);parameters.setFileNameInZip("aaaa_"+i+".xls");//后缀可以更改的if (!"".equals(passwd)) {parameters.setEncryptFiles(true);parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 加密方式parameters.setPassword(passwd.toCharArray());}addNewStreamToZipTmp(saos, parameters);}saos.writeTo(f);f.close();saos.close();//文件大小System.out.println("size:" +(saos.toByteArray().length/1024));} catch (Exception e) {e.printStackTrace();}}public void addNewStreamToZipTmp(ByteArrayOutputStream baos,ZipParameters parameters) throws ZipException {ZipOutputStream outputStream = null;try {outputStream = new ZipOutputStream(baos, this.zipModel);if (zipModel.getEndCentralDirRecord() == null) {throw new ZipException("invalid end of central directory record");}checkParameters(parameters);outputStream.putNextEntry(null, parameters);if (!parameters.getFileNameInZip().endsWith("/")&& !parameters.getFileNameInZip().endsWith("\\")) {for (int i = 0; i < 10; i++) {outputStream.write("ccccccccccccccccccccc".getBytes());outputStream.flush();}}outputStream.closeEntry();outputStream.finish();} catch (ZipException e) {throw e;} catch (Exception e) {throw new ZipException(e);} finally {if (outputStream != null) {try {outputStream.close();} catch (IOException e) {}}}}private void checkParameters(补充:软件开发 , Java ,
上一个:java线程的实现方式
下一个:Java常用类库--日期操作类
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值