java 压缩包aes加密,java AES加密压缩文件

package com.lvhe.core.util; import java.io.File; import java.util.ArrayList; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; public class ZipUtil { //AES加密压缩 public static int AddFilesWithAESEncryption(String filePath, String destFile, String pwd) { try { ZipFile zipFile = new ZipFile(destFile); ArrayList filesToAdd = new ArrayList(); filesToAdd.add(new File(filePath)); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression // Set the compression level. This value has to be in between 0 to 9 // Several predefined compression levels are available // DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression // DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression // DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed // DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed // DEFLATE_LEVEL_ULTRA - Highest compression level but low speed parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);  // Set the encryption flag to true // If this is set to false, then the rest of encryption properties are ignored parameters.setEncryptFiles(true); // Set the encryption method to AES Zip Encryption parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); // Set AES Key strength. Key strengths available for AES encryption are: // AES_STRENGTH_128 - For both encryption and decryption // AES_STRENGTH_192 - For decryption only // AES_STRENGTH_256 - For both encryption and decryption // Key strength 192 cannot be used for encryption. But if a zip file already has a // file encrypted with key strength of 192, then Zip4j can decrypt this file parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_128); // Set password parameters.setPassword(pwd); // Now add files to the zip file // Note: To add a single file, the method addFile can be used // Note: If the zip file already exists and if this zip file is a split file // then this method throws an exception as Zip Format Specification does not  // allow updating split zip files zipFile.addFiles(filesToAdd, parameters); } catch (ZipException e) { e.printStackTrace(); return -1; } return 0; } public static void extractSingleFile() { try { ZipFile zipFile = new ZipFile("C:\\Users\\Administrator\\Desktop\\ZipTest\\D2A27FF5E9B1CA68200C9FD2703C4379.zip"); if (zipFile.isEncrypted()) { zipFile.setPassword("test123"); } zipFile.extractFile("D2A27FF5E9B1CA68200C9FD2703C4379.ori", "C:\\Users\\Administrator\\Desktop\\ZipTest\\"); } catch (ZipException e) { e.printStackTrace(); } }   public static boolean extractSingleFile(String scretKey,Integer placeId,String fileName,String fileMd5,String fileType) { try { ZipFile zipFile = new ZipFile(Config.ftpdir+"/"+placeId+"\\tmp\\"+fileName+".zip"); if (zipFile.isEncrypted()) { zipFile.setPassword(scretKey); } String fileExtractPath = Config.ftpdir+"/"+placeId+"\\tmp\\"; File file = new File(fileExtractPath); if (!file.exists()) { file.mkdirs(); } String extracFileName = null; if(fileType.equals("1") ){ extracFileName = fileMd5 + ".ori"; //extracFileName = "5e087ae82408b987a57de37615a149f3.ori"; }else if (fileType.equals("2")){ extracFileName = fileMd5 + ".diff"; } zipFile.extractFile(extracFileName, fileExtractPath); } catch (ZipException e) { e.printStackTrace(); return false; } return true; } public static void main(String[] args) { // extractSingleFile(); int flag = ZipUtil.AddFilesWithAESEncryption("D:/ftp/5e087ae82408b987a57de37615a149f3.ori", "d:/ftp/ds.zip", "12345678"); } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值