java针对单文件 zip压缩加密

package com.yukun;

/**
* 将excel打包生成zip文件,并且打开excel
文件的时候,需要输入密码,才可以打开.
作者 aa00aa00
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;

public class TestMailWithEncryptAttachment {

	/**
	 * 生成zip文件
	 * 
	 * @param srcfile
	 * @param zipFileName
	 * @param password
	 * @throws IOException
	 */
	public static void createZipFile(File srcfile, String zipFileName, String password) throws IOException {

		byte b[] = new byte[1024];
		ZipOutputStream zout = null;
		InputStream in = null;

		try {
			zout = new ZipOutputStream(new FileOutputStream(zipFileName));
			in = new FileInputStream(srcfile);

			String filename = srcfile.getName();// 取得文件名
			ZipEntry e = new ZipEntry(filename); // 压缩后不带路径
			zout.putNextEntry(e);

			int len = 0;
			while ((len = in.read(b)) != -1) {
				zout.write(b, 0, len);
			}

			zout.closeEntry();
			zout.flush();
			zout.close();

			in.close();

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException io) {
			io.printStackTrace();
		} finally {
			if (zout != null) {
				try {
					zout.flush();
					zout.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		TestMailWithEncryptAttachment.createProtectedZip(zipFileName, srcfile, password);
	}

	/**
	 * 生成加密的xls文件
	 * 
	 * @param zipFileName
	 * @param srcfile
	 * @param password
	 * @throws IOException
	 */
	public static void createProtectedZip(String zipFileName, File srcfile, String password) throws IOException {
		try {
			ZipFile zipFile = new ZipFile(zipFileName);

			ZipParameters parameters = new ZipParameters();
			parameters.setCompressionMethod(8);
			parameters.setCompressionLevel(5);
			parameters.setEncryptFiles(true);
			parameters.setEncryptionMethod(0);
			parameters.setPassword(password);
			zipFile.addFile(srcfile, parameters);
		} catch (ZipException e) {
			e.printStackTrace();
		}
	}

	

	public static void main(String[] args) throws IOException {
		// 压缩, 加密文件
		String zipFileName = "E:/resource.zip";
		String compressPassword = "1";
		File srcfile = new File("E:/文件名.doc");

		createZipFile(srcfile, zipFileName, compressPassword);
	}

}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
压缩文件方法 该方法需要引用zip4j的jar文件 文件、多个文件压缩 /** * 使用给定密码压缩指定文件文件夹到指定位置. * * dest可传最终压缩文件存放的绝对路径,也可以传存放目录,也可以传null或者"". * 如果传null或者""则将压缩文件存放在当前目录,即跟源文件同目录,压缩文件名取源文件名,以.zip为后缀; * 如果以路径分隔符(File.separator)结尾,则视为目录,压缩文件名取源文件名,以.zip为后缀,否则视为文件名. * @param src 要压缩文件文件夹路径 * @param dest 压缩文件存放路径 * @param isCreateDir 是否在压缩文件里创建目录,仅在压缩文件为目录时有效. * 如果为false,将直接压缩目录下文件压缩文件. * @param passwd 压缩使用的密码 * @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败. */ 方法详细见文件! 可选择文件list压缩 /** * 使用给定密码压缩指定文件list * dest可传最终压缩文件存放的绝对路径,也可以传存放目录,也可以传null或者"". * 如果传null或者""则将压缩文件存放在当前目录,即跟源文件同目录,压缩文件名取源文件名,以.zip为后缀; * 如果以路径分隔符(File.separator)结尾,则视为目录,压缩文件名取源文件名,以.zip为后缀,否则视为文件名. * @param src 要压缩文件集合 * @param dest 压缩文件存放路径 * @param isCreateDir 是否在压缩文件里创建目录,仅在压缩文件为目录时有效. * 如果为false,将直接压缩目录下文件压缩文件. * @param passwd 压缩使用的密码 * @return 最终的压缩文件存放的绝对路径,如果为null则说明压缩失败. */ 方法详细见文件! 解压 /** * 使用给定密码解压指定的ZIP压缩文件到指定目录 * * 如果指定目录不存在,可以自动创建,不合法的路径将导致异常被抛出 * @param zipFile 指定的ZIP压缩文件 * @param dest 解压目录 * @param passwd ZIP文件的密码 * @return 解压后文件数组 * @throws ZipException 压缩文件有损坏或者解压缩失败抛出 */ 方法详细见文件! 一个简的demo 欢迎大家指点,一起提升
您可以使用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、付费专栏及课程。

余额充值