软件编程
位置:首页>> 软件编程>> java编程>> Java实现多文件压缩加密并重命名压缩文件对象的方法

Java实现多文件压缩加密并重命名压缩文件对象的方法

作者:weixin_44980116  发布时间:2021-07-02 15:44:31 

标签:java,多文件,压缩,加密

Java 多文件加密压缩 添加文件加密压缩工具包依赖


<!-- zip4j压缩工具 -->
 <dependency>
  <groupId>net.lingala.zip4j</groupId>
  <artifactId>zip4j</artifactId>
  <version>1.3.2</version>
 </dependency>

话不多说,直接上干货

完整代码如下:


package com.rhtcms.cms.api.admin.main;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import java.io.*;
import java.util.*;

public class FileCompressionApiAct {
/**
 * 复制压缩文件路径 ps:此路径必须为空文件夹,在压缩完成后此文件夹将被清空目录
 */
private static String copyPath = "c:/Users/Administrator/Desktop/压缩测试/压缩测试作业复制";

private static long time = System.currentTimeMillis();//以时间戳作为文件名,防止重命名问题

/**
 * 压缩包路径: 路径+压缩包名称 eg: C:/Users/Administrator/Desktop/压缩测试/ + test.zip
 */
private static String zipPath = "C:/Users/Administrator/Desktop/压缩测试/" + time + ".zip";

/**
 * 可支持的压缩文件格式
 */
private static String[] fileType = {"doc", "docx", "pdf", "txt"};

/**
 * @param filePath 压缩文件路径
 * @param fileRename 压缩文件重命名名称
 * @param password 加密密码
 * @return
 * @Title: zipFilesAndEncrypt
 * @Description: 将指定路径下的文件压缩至指定zip文件,并以指定密码加密,若密码为空,则不进行加密保护
 * @Author: 张庆裕
 * @Date: 2021/01/04
 */
//@RequestMapping("/fileCompression/list")
public String zipFilesAndEncrypt(List<File> filePath, List<String> fileRename, String password) {
 /**
  * 压缩成功的文件数量
  */
 int successCount = 0;
 /**
  * 压缩失败的文件数量
  */
 int failCount = 0;
 /**
  * 返回数据
  */
 JSONObject ob = new JSONObject();

ArrayList<String> failFile = new ArrayList<>();//压缩失败的文件路径

ArrayList<String> failFilePath = new ArrayList<>();//路径错误的文件

ArrayList<File> filesToAdd = new ArrayList<>();//压缩路径的集合

//创建复制文件夹
 File folder = new File(copyPath);
 if(!folder.exists()){//如果文件夹不存在
  boolean mkdir = folder.mkdir();//创建文件夹
  if(!mkdir){//系统未找到该路径
   throw new RuntimeException("复制文件路径出错,请修改复制文件夹路径");
  }
 }else{//文件夹存在
  File[] listFiles = folder.listFiles();
  if(listFiles.length > 0){//如何文件夹下存在目录则,停止压缩,防止删除其他文件
   throw new RuntimeException("复制的文件夹不为空,请选择空文件夹!");
  }
 }

for (int i = 0; i < filePath.size(); i++) {//遍历压缩文件数据
  File file = filePath.get(i);//获取原文件
  if (!file.exists()) {//防止文件异常,首先再次确认文件路径是否存在
   // 文件不存在
   failCount++;
   failFilePath.add(file.getPath());
   System.out.println("文件:" + file.getPath() + " 路径不存在!");
   ob.put("failFilePath", failFilePath);
  } else {//文件存在
   //获取原文件路径
   String path = filePath.get(i).getPath();
   //获取最后一个点的位置
   int lastIndexOf = path.lastIndexOf(".");
   //获取文件后缀 eg: txt , doc , pdf ....
   String suffix = path.substring(lastIndexOf + 1);
   if (Arrays.asList(fileType).contains(suffix)) {  //判断文件格式是否合格,合格添加至压缩文件中
    //获取原文件名称
    File oldName = new File(file.getPath());

//先复制文件
    File newName = new File(copyPath + "/" + file.getName());
    try {
     copyFile(oldName, newName);
    } catch (Exception e) {
     e.printStackTrace();
    }
    //String path = newName.getPath();//获取复制文件的路径
    String parentPath = newName.getParent();//获取复制出来的文件的父级目录
    String reName = fileRename.get(i);//获取重命名的名称

newName.renameTo(new File(parentPath + "/" + reName));//重命名复制出来的文件
    filesToAdd.add(new File(parentPath + "/" + reName));//将赋值出来的文件添加到压缩文件集合中
    successCount++;//压缩成功文件数量+1
   } else {
    failFile.add(file.getPath());
    failCount++;//压缩失败文件数量+1

ob.put("filePath", failFile);
    System.out.println("该文件压缩失败:" + file.getPath() + " 文件格式错误!");
   }
  }
 }
 //压缩配置
 try {
  ZipParameters parameters = new ZipParameters();
  parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);//压缩方式
  //设置压缩级别
  //DEFLATE_LEVEL_FASTEST - 最低压缩级别,但压缩速度更高
  //DEFLATE_LEVEL_FAST - 低压缩级别,但压缩速度更高
  //DEFLATE_LEVEL_NORMAL - 压缩水平速度之间的最佳平衡
  //DEFLATE_LEVEL_MAXIMUM - 高压缩级别,但速度不佳
  //DEFLATE_LEVEL_ULTRA - 最高压缩级别但速度较低
  parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);//压缩级别
  if (password != null && password!="") {
   parameters.setEncryptFiles(true);//设置压缩文件加密
   parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//加密方式
   parameters.setPassword(password);//设置加密密码
  }
  ZipFile zipFile = new ZipFile(zipPath);//创建压缩路径
  zipFile.setFileNameCharset("gbk");//设置压缩编码
  zipFile.addFiles(filesToAdd, parameters);//添加压缩文件并进行加密压缩
  //压缩完成后清空复制的文件目录
  deleteDir(copyPath);
  ob.put("zipPath", zipPath);
  ob.put("successCount", successCount);
  ob.put("failCount", failCount);
 } catch (ZipException e) {
  //清空复制的文件目录
  deleteDir(copyPath);
  ob.put("unKnown", "未知异常,压缩失败!");
  System.out.println("文件压缩出错");
  e.printStackTrace();
 }
 return ob.toString();
}

/**
 * @Description: 文件复制
 * @Param: resource 原文件路径
 * @Param: target 新文件路径
 * @return:
 * @Author: 张庆裕
 * @Date: 2021/1/6
 */
public void copyFile(File resource, File target) throws Exception {
 // 输入流 --> 从一个目标读取数据
 // 输出流 --> 向一个目标写入数据
 long start = System.currentTimeMillis();
 // 文件输入流并进行缓冲
 FileInputStream inputStream = new FileInputStream(resource);
 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
 // 文件输出流并进行缓冲
 FileOutputStream outputStream = new FileOutputStream(target);
 BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
 // 缓冲数组
 // 大文件 可将 1024 * 2 改大一些,但是 并不是越大就越快
 byte[] bytes = new byte[1024 * 2];
 int len = 0;
 while ((len = inputStream.read(bytes)) != -1) {
  bufferedOutputStream.write(bytes, 0, len);
 }
 // 刷新输出缓冲流
 bufferedOutputStream.flush();
 //关闭流
 bufferedInputStream.close();
 bufferedOutputStream.close();
 inputStream.close();
 outputStream.close();
 long end = System.currentTimeMillis();
 System.out.println("复制文件:" + resource.getPath() + " 成功 耗时:" + (end - start) / 1000 + " s");
}

/**
 * @Description: 清空复制压缩文件下的内容
 * @Param: path 复制文件夹的路径
 * @return:
 * @Author: 张庆裕
 * @Date: 2021/1/6
 */
public boolean deleteDir(String path) {
 File file = new File(path);
 if (!file.exists()) {//判断是否待删除目录是否存在
  System.err.println("The dir are not exists!");
  return false;
 }
 String[] content = file.list();//取得当前目录下所有文件和文件夹
 for (String name : content) {
  File temp = new File(path, name);
  if (temp.isDirectory()) {//判断是否是目录
   deleteDir(temp.getAbsolutePath());//递归调用,删除目录里的内容
   temp.delete();//删除空目录
  } else {
   if (!temp.delete()) {//直接删除文件
    System.err.println("Failed to delete " + name);
   }
  }
 }
 return true;
}

/**
 * @Description: 文件压缩测试接口
 * @Param:
 * @return:
 * @Author: 张庆裕
 * @Date: 2021/1/7
 */
public static void main(String[] args) {
 List<File> filePath = new ArrayList<>();//压缩文件路径
 filePath.add(new File("C:/Users/Administrator/Desktop/压缩测试/yasuo/OA平台问题.docx"));
 filePath.add(new File("C:/Users/Administrator/Desktop/压缩测试/yasuo/OA平台问题1.docx"));
 filePath.add(new File("C:/Users/Administrator/Desktop/压缩测试/yasuo/OA平台问题2.docx"));
 filePath.add(new File("C:/Users/Administrator/Desktop/压缩测试/yasuo/OA平台问题3.docx"));
 filePath.add(new File("C:/Users/Administrator/Desktop/压缩测试/yasuo/邮箱1.md"));
 filePath.add(new File("C:/Users/Administrator/Desktop/压缩测试/yasuo/邮箱2.md"));

List<String> fileRename = new ArrayList<>();//压缩文件重命名名称
 fileRename.add("oa平台问题.docx");
 fileRename.add("oa平台问题1.docx");
 fileRename.add("oa平台问题2.docx");
 fileRename.add("oa平台问题3.docx");
 fileRename.add("邮箱副本1.md");
 fileRename.add("邮箱副本2.md");

String password = "123456";//加密密码
 //请在单元测试进行测试, 或者将方法改为 static 方法
 //String result = zipFilesAndEncrypt(filePath, fileRename, password);
 //System.out.println(result);
}

}

效果如下:

Java实现多文件压缩加密并重命名压缩文件对象的方法

来源:https://blog.csdn.net/weixin_44980116/article/details/112315174

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com