java 加密并打包_java实现将多个文件打包成zip压缩文件以及对压缩文件的加密

如果仅仅需要对文件进行压缩,方法比较简单,因为java的util包中提供了相关的压缩方法.

首先,引入java.util.zip.ZipEntry,java.util.zip.ZipOutputStream包

然后加入以下代码:

String now = StringUtils.dateToString(new Date());

String path = request.getServletPath();

path = request.getRealPath(path);

path = path.substring(0,path.lastIndexOf("\\"))+"\\";

File zipFile = new File(path+"edm_expcsv\\"+"EDM_expcsv_"+now+".zip"); //压缩的文件名和地址

ZipOutputStream out2 = new ZipOutputStream(new FileOutputStream(zipFile));//传入压缩文件路径,得到压缩流

byte[] buffer = new byte[1024];

File[] file1 = {new File(path+file),new File(path+file2)};

for(int x=0;x

FileInputStream fis = new FileInputStream(file1[x]);

out2.putNextEntry(new ZipEntry(file1[x].getName()));//将要压缩的文件放入压缩流

int len;

//读入需要下载的文件的内容,打包到zip文件

while((len = fis.read(buffer))>0) {

out2.write(buffer,0,len);

}

out2.closeEntry();

fis.close();

}

out2.close();

如果是要对文件压缩并加密,则使用下面的方法,但是值支持单个文件压缩

先将ant.jar commons-io.jar EncryptZip.jar EncryptZip.jar winzip.1.1.0.jar

5文件放到lib包中

然后在头文件import中加入String,java.io.File,de.idyl.winzipaes.AesZipFileEncrypter,de.idyl.winzipaes.impl.AESEncrypterBC

然后加入代码

File file = newFile(s); //定义压缩文件的路径

File zipFile = new File(b + "\\demo.zip");//压缩的文件名和地址

AESEncrypterBC bc = new AESEncrypterBC();

AesZipFileEncrypter azfe = new AesZipFileEncrypter(zipFile, bc);

azfe.setEncoding("UTF-8"); // 编码格式是在这传进去的

azfe.add(file,"123.jpg", "123"); //1:要压缩的文件路径,2:为压缩的文件命名3:为密码

azfe.close();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用JavaZipOutputStream类和Java加密库来实现文件压缩和加密。下面是一个示例代码,演示如何压缩文件并对其进行加密: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import javax.crypto.Cipher; import javax.crypto.CipherOutputStream; import javax.crypto.spec.SecretKeySpec; public class ZipEncryptExample { public static void main(String[] args) { String sourceFile = "path/to/source/file"; String zipFile = "path/to/output/zip/file"; String password = "your_password"; try { // 创建ZipOutputStream对象 FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zos = new ZipOutputStream(fos); // 创建加密Cipher对象 Cipher cipher = Cipher.getInstance("AES"); SecretKeySpec secretKey = new SecretKeySpec(password.getBytes(), "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 添加文件zip文件中 addToZip(sourceFile, sourceFile, zos, cipher); // 关闭流 zos.close(); fos.close(); System.out.println("压缩并加密!"); } catch (Exception e) { e.printStackTrace(); } } private static void addToZip(String fileName, String sourceFile, ZipOutputStream zos, Cipher cipher) throws Exception { File file = new File(sourceFile); FileInputStream fis = new FileInputStream(file); // 创建ZipEntry对象,并设置文件ZipEntry zipEntry = new ZipEntry(fileName); zos.putNextEntry(zipEntry); // 创建CipherOutputStream对象,将加密的数据写入ZipOutputStream CipherOutputStream cos = new CipherOutputStream(zos, cipher); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) >= 0) { cos.write(buffer, 0, length); } // 关闭流 cos.close(); fis.close(); } } ``` 在上面的示例代码中,您需要将`sourceFile`替换为要压缩的文件的路径,`zipFile`替换为输出的ZIP文件路径,`password`替换为您想要使用的加密密码。代码会将指定的文件压缩ZIP文件,并使用AES算法对其进行加密。 请确保您已经包含了Java加密库,通过`import javax.crypto.Cipher`和`import javax.crypto.CipherOutputStream`导入相应的类。 请注意,加密和解密文件需要相同的密码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值