# 需求:
将文件加密压缩。如果想要查看文件,需要密码才可以。
# 实现逻辑:
将文件加密压缩,将压缩后文件上传ftp服务器,并发送邮件通知对方,文件已经上传,将密码发送至对方手机短信。
# 代码
```java
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 文件压缩处理类
* @author
*/
public class Compressor {
private static Logger log = LoggerFactory.getLogger(Compressor.class);
final CompressionEnum compressionEnum;
static final int BUFFER_SIZE = 8192;
public Compressor(CompressionEnum compressionEnum) {
this.compressionEnum = compressionEnum;
}
/**
* 数据压缩 加密
* @param nameOfFile2Compress
* @param nameOfCompressedFile
*/
public void compress(String nameOfFile2Compress, String nameOfCompressedFile,String passwd) {
switch (compressionEnum) {
case GZ:
gzCompress(nameOfFile2Compress, nameOfCompressedFile);
break;
case ZIP:
encryZipFile(new File(nameOfCompressedFile), new File(nameOfFile2Compress),passwd);
break;
default:
encryZipFile(new File(nameOfCompressedFile), new File(nameOfFile2Compress),passwd);
break;
}
}
private void zipCompress(String nameOfFile2zip, String nameOfZippedFile, String innerEntryName) {
File file2zip = new File(nameOfFile2zip);
if (!file2zip.exists()) {
log.error("压缩的文件 [" + nameOfFile2zip + "] 不存在.", nameOfFile2zip);
return;
}
if (innerEntryName == null) {
log.error("压缩的innerEntryName 不能为空");
return;
}
if (!nameOfZippedFile.endsWith(".zip")) {
nameOfZippedFile = nameOfZippedFile + ".zip";
}
File zippedFile = new File(nameOfZippedFile);
if (zippedFile.exists()) {
log.error("压缩的文件 [" + nameOfZippedFile + "] 已经存在");
return;
}
BufferedInputStream bis = null;
ZipOutputStream zos = null;
try {
bis = new BufferedInputStream(new FileInputStream(nameOfFile2zip));
zos = new ZipOutputStream(new FileOutputStream(nameOfZippedFile));
ZipEntry zipEntry = new ZipEntry(innerEntryName);
zos.putNextEntry(zipEntry);
byte[] inbuf = new byte[BUFFER_SIZE];
int n;
while ((n = bis.read(inbuf)) != -1) {
zos.write(inbuf, 0, n);
}
bis.close();
bis = null;
zos.close();
zos = null;
if (!file2zip.delete()) {
log.error("不能删除 [" + nameOfFile2zip + "] ");
}
} catch (Exception e) {
log.error("压缩文件 [" + nameOfFile2zip + "] 到 [" + nameOfZippedFile + "] 报错");
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
log.error("压缩文件 [" + nameOfFile2zip + "] 到 [" + nameOfZippedFile + "] 报错");
}
}
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
log.error("压缩文件 [" + nameOfFile2zip + "] 到 [" + nameOfZippedFile + "] 报错");
}
}
}
}
/**
* 对压缩文件进行加密
* @param zipFile
* @param addFile
*/
private void encryZipFile(File zipFile,File addFile,String password){
try {
//创建压缩文件
ZipFile respFile = new ZipFile(zipFile);
//设置压缩文件参数
ZipParameters parameters = new ZipParameters();
//设置压缩方法
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
//设置压缩级别
//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_FAST);
//设置压缩文件加密
parameters.setEncryptFiles(true);
//设置加密方法
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
//设置aes加密强度
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
//设置密码
if(StringUtils.isNoneEmpty(password)){
parameters.setPassword(password);
}
//添加文件到压缩文件
respFile.addFile (addFile,parameters);
} catch (Exception e) {
log.error("压缩文件 [" + addFile.getName() + "] 到 [" + zipFile.getName() + "] 报错");
}
}
/**
* 压缩gz
* @param nameOfFile2gz
* @param nameOfgzedFile
*/
private void gzCompress(String nameOfFile2gz, String nameOfgzedFile) {
}
public static void main(String[] args) {
//生成密码
Random ra =new Random(100);
String passwd = Md5.MD5(ra.nextInt() + String.valueOf(System.currentTimeMillis()));
System.out.println(passwd);
Compressor c = new Compressor(CompressionEnum.ZIP);
c.compress("D:\\电子书\\faefd.txt","D:\\电子书\\电子.zip",passwd);
}
}
```
main()用到的Md5加密方法。
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import java.io.UnsupportedEncodingException;
public class Md5 {
public Md5() {
}
public static final String MD5(String s) {
return MD5(s, "UTF-8").toUpperCase();
}
public static final String MD5(String s, String input_charset) {
return DigestUtils.md5Hex(getContentBytes(s, input_charset));
}
public static final String MD5(String s, String key, String input_charset) {
s = s + key;
return MD5(s, input_charset);
}
public static boolean verify(String text, String sign, String key, String input_charset) {
text = text + key;
return verify(text, sign, input_charset);
}
public static boolean verify(String text, String sign) {
return verify(text, sign, "UTF-8");
}
public static boolean verify(String text, String sign, String input_charset) {
String mysign = MD5(text, input_charset);
return mysign.equals(sign);
}
private static byte[] getContentBytes(String content, String charset) {
if (StringUtils.isEmpty(charset)) {
return content.getBytes();
} else {
try {
return content.getBytes(charset);
} catch (UnsupportedEncodingException var3) {
throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
}
}
}
}