Java加密压缩文件

pom.xml net.lingala.zip4jzip4j1.3.2 java代码: import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.zip.ZipEntry; import net.lingala.zip4j.io.ZipOutputStream; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; /** * 文件压缩 * @author */ public class ZipUtil { public static void main(String[] args) throws Exception { //传递文件 OutputStream out = new FileOutputStream(new File("E:/logs/file"+".zip")); out = zipByFilePath(out, "123456", new String[][]{ {"info/demo1.txt","E:/info/info1.txt"}, {"info2/demo2.txt","E:/info/info2.txt"}}); out.flush(); out.close(); //传递字符流 out = new FileOutputStream(new File("E:/logs/streamt"+".zip")); out = zipByFileInput(out, "123456", new Object[][]{{"info/demo1.txt", new FileInputStream("E:/info/info1.txt")}, {"info2/demo2.txt",new FileInputStream("E:/info/info2.txt")}, {"total.txt",new FileInputStream("E:/info/info2.txt")}}); out.flush(); out.close(); out = new FileOutputStream(new File("E:/logs/fileNoPwd"+".zip")); out = zipByFilePath(out,new String[][]{ {"info/demo1.txt","E:/info/info1.txt"}, {"info2/demo2.txt","E:/info/info2.txt"}}); out.flush(); out.close(); out = new FileOutputStream(new File("E:/logs/streamtNoPwd"+".zip")); out = zipByFileInput(out,new Object[][]{{"info/demo1.txt", new FileInputStream("E:/info/info1.txt")}, {"info2/demo2.txt",new FileInputStream("E:/info/info2.txt")}, {"total.txt",new FileInputStream("E:/info/info2.txt")}}); out.flush(); out.close(); } /** * 压缩文件流-加密 * @param out * @param password * @param fileAry * @return * @throws Exception */ public static OutputStream zipByFilePath(OutputStream out,String password,String[][] fileAry) throws Exception { Object[][] info = new Object[2][fileAry.length]; for(int index=0,size=fileAry.length;index-1) { zipOut.write(btAry,0,byteReadSize); byteReadSize = fileInput.read(btAry); } } return zipOut; } /** * 压缩文件流 * @param out * @param password * @param fileAry {{fileName,InputStream}} * @return * @throws Exception */ public static OutputStream zipByFileInput(OutputStream out,String password,Object[][] fileAry) throws Exception { //压缩的规则设置 ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setSourceExternalStream(true); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(password); ZipOutputStream zipOut = new ZipOutputStream(out); for(int index=0,fileSize=fileAry.length;index-1) { zipOut.write(btAry,0,byteReadSize); byteReadSize = fileInput.read(btAry); } zipOut.closeEntry(); fileInput.close(); } /** * 压缩流中放入文件 * @param zipOut * @param parameters * @param fileInput * @throws Exception */ public static void addToZipOut(ZipOutputStream zipOut,ZipParameters parameters,byte[] btAry) throws Exception { zipOut.write(btAry); zipOut.closeEntry(); } }

转载于:https://www.cnblogs.com/freeLess/p/5458148.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值