对文件或文件夹进行压缩解压加密解密

对文件或文件夹进行压缩解压加密解密:
Java代码 复制代码 收藏代码
  1. import java.io.File; 
  2. import java.util.UUID; 
  3.  
  4. public class ZipCipherUtil { 
  5.     /**
  6.      * 对目录srcFile下的所有文件目录进行先压缩后加密,然后保存为destfile
  7.      *
  8.      * @param srcFile
  9.      *            要操作的文件或文件夹
  10.      * @param destfile
  11.      *            压缩加密后存放的文件
  12.      * @param keyfile
  13.      *            密钥
  14.      */ 
  15.     public void encryptZip(String srcFile, String destfile, String keyStr) throws Exception { 
  16.         File temp = new File(UUID.randomUUID().toString() + ".zip"); 
  17.         temp.deleteOnExit(); 
  18.         // 先压缩文件 
  19.         new ZipUtil().zip(srcFile, temp.getAbsolutePath()); 
  20.         // 对文件加密 
  21.         new CipherUtil().encrypt(temp.getAbsolutePath(), destfile, keyStr); 
  22.         temp.delete(); 
  23.     } 
  24.  
  25.     /**
  26.      * 对文件srcfile进行先解密后解压缩,然后解压缩到目录destfile下
  27.      *
  28.      * @param srcfile
  29.      *            要解密和解压缩的文件名
  30.      * @param destfile
  31.      *            解压缩后的目录
  32.      * @param publicKey
  33.      *            密钥
  34.      */ 
  35.     public void decryptUnzip(String srcfile, String destfile, String keyStr) throws Exception { 
  36.         File temp = new File(UUID.randomUUID().toString() + ".zip"); 
  37.         temp.deleteOnExit(); 
  38.         // 先对文件解密 
  39.         new CipherUtil().decrypt(srcfile, temp.getAbsolutePath(), keyStr); 
  40.         // 解压缩 
  41.         new ZipUtil().unZip(temp.getAbsolutePath(),destfile); 
  42.         temp.delete(); 
  43.     } 
  44.      
  45.     public static void main(String[] args) throws Exception { 
  46.         long l1 = System.currentTimeMillis(); 
  47.          
  48.         //加密 
  49. //      new ZipCipherUtil().encryptZip("d:\\test\\111.jpg", "d:\\test\\photo.zip", "12345"); 
  50.         //解密 
  51.         new ZipCipherUtil().decryptUnzip("d:\\test\\photo.zip", "d:\\test\\111_1.jpg", "12345"); 
  52.          
  53.         long l2 = System.currentTimeMillis(); 
  54.         System.out.println((l2 - l1) + "毫秒."); 
  55.         System.out.println(((l2 - l1) / 1000) + "秒."); 
  56.     } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值