有关java对压缩文件的加密

本文提供了一种使用Java实现将指定目录下的文件压缩为ZIP格式,并设置密码保护的方法。通过提供的示例代码,展示了如何利用第三方库实现文件的加密压缩。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

做一个项目的过程中遇到这样一个问题:讲数据库导出并带密码压缩成zip文件,在加密的时候卡了我一整天,网上找了很多方法都失败了,现在刚刚搞定~还是贴出来吧,应该能帮到不少程序员的忙~

 

Java代码 
  1. package test1;  
  2.   
  3. import java.io.File;  
  4.   
  5. import nochump.util.extend.ZipOutput;  
  6.   
  7. import com.training.commons.file.FileUtils;  
  8.   
  9. public class ZipFileWithPw {  
  10.     public static void main(String[] args){  
  11. <span style="white-space: pre;">    </span>final String zipDir = "d:\\backupDB";  
  12.         final String EncryptZipFile = "d:\\backupDB\\db_user.zip";  
  13.         final String password = "123456";  
  14.           
  15.         System.out.println("===== 加密 =====");  
  16.         File file = new File(zipDir);  
  17.         byte[] zipByte = ZipOutput.getEncryptZipByte(file.listFiles(), password);  
  18.         FileUtils.writeByteFile(zipByte, new File(EncryptZipFile));  
  19.         System.out.println("===== Encrypt Success =====");  
  20.           
  21.     }  
  22. }  

 这里我附上整理好的jar包~

下载地址:http://dl.iteye.com/topics/download/e4a1c011-6adf-3a75-a8d6-2cae46670f7c

 

 

 

 

 

 

 

 

如果要详细理解源代码结构,读者可以移步到这里作参考http://blog.youkuaiyun.com/flexworks/article/details/6568664

Java中,我们可以使用多种库来对压缩包进行加密和解密操作,常见的有Zip和JCE(Java Cryptography Extension)API。以下是基本步骤: 1. 加密压缩包: - 使用`java.util.zip`包中的`ZipOutputStream`和`DeflaterOutputStream`组合,先创建一个压缩流,然后可以使用`javax.crypto.Cipher`类对压缩后的数据进行加密。例如,你可以将数据写入压缩流,然后使用一个密钥和模式(如AES或DES)进行加密。 ```java import java.io.*; import java.util.zip.*; import javax.crypto.*; public void encryptZip(String inputFile, String outputFile, String password) { try (FileInputStream fis = new FileInputStream(inputFile); FileOutputStream fos = new FileOutputStream(outputFile); ZipOutputStream zos = new ZipOutputStream(fos); Cipher cipher = Cipher.getInstance("AES")) { // 创建加盐的IvGenerator IvParameterSpec iv = new IvParameterSpec(password.getBytes(StandardCharsets.UTF_8)); SecretKeySpec keySpec = new SecretKeySpec(password.getBytes(), "AES"); // 初始化Cipher对象为ENCRYPT_MODE cipher.init(Cipher.ENCRYPT_MODE, keySpec, iv); // 开始加密 byte[] buffer = new byte[1024]; int read; while ((read = fis.read(buffer)) != -1) { byte[] encryptedBuffer = cipher.doFinal(buffer, 0, read); zos.write(encryptedBuffer, 0, encryptedBuffer.length); } // 添加加密文件到压缩包 ZipEntry entry = new ZipEntry(inputFile.getName()); zos.putNextEntry(entry); zos.closeEntry(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } ``` 2. 解密压缩包: - 首先读取加密的数据,然后使用相同的密钥和IV解密。解密之后再使用`ZipInputStream`处理解压操作。 ```java public void decryptZip(String encryptedFile, String decryptedOutputDir, String password) { try (FileInputStream fis = new FileInputStream(encryptedFile); FileOutputStream fos = new FileOutputStream(decryptedOutputDir + "/" + encryptedFile.substring(encryptedFile.lastIndexOf(".") + 1))); ZipInputStream zis = new ZipInputStream(new CipherInputStream(fis, Cipher.getInstance("AES"), password)); Deflater decompressor = new Deflater()) { // 初始化解密过程 ... // 解压并写入文件 ZipEntry entry; while ((entry = zis.getNextEntry()) != null) { byte[] buffer = new byte[1024]; int length; decompressor.setInput(buffer); while ((length = zis.read(buffer)) > 0) { fos.write(decompressor.inflate(buffer, 0, length)); } zis.closeEntry(); } } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值