马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
需求阐明:
许多人渴望可以或许对自己的Office文件以及压缩的ZIP文件举行加密,以防止未经授权的访问和信息泄漏。然而,使用WPS、Microsoft Office软件或别的压缩工具逐一对每一个文件举行加密,着实是耗时且费力,通常让人感到疲劳与沮丧。
因此,本文将详细先容一种更为高效的办理方案,以免各人在人力和时间上面无谓的斲丧。本文的重要内容布局包罗以下几个部分:
1、所需依赖
2、文档类加密工具类 + 测试示例
3、zip包加密工具类 + 测试示例
效果如下:
1、所需依赖:
提前声明(JAR包版本已经颠末筛选 只管不消对依赖做变动,否则部分方法大概类不存在)且注意各个POI依赖之间的版本信息。
- <!--其他包 与主题无关 为保完整性故完整列出 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.13.1</version>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-api</artifactId>
- <version>1.7.21</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
- <dependency>
- <groupId>org.slf4j</groupId>
- <artifactId>slf4j-log4j12</artifactId>
- <version>1.7.25</version>
- <scope>compile</scope>
- </dependency>
- <!-- office加密所需 -->
- <dependency>
- <groupId>org.apache.pdfbox</groupId>
- <artifactId>pdfbox</artifactId>
- <version>2.0.27</version>
- </dependency>
- <dependency>
- <groupId>com.itextpdf</groupId>
- <artifactId>itext7-core</artifactId>
- <version>7.1.11</version>
- <type>pom</type>
- </dependency>
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- </dependency>
- <!-- Apache POI -->
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi</artifactId>
- <version>5.2.3</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml</artifactId>
- <version>5.2.3</version>
- </dependency>
- <dependency>
- <groupId>org.apache.poi</groupId>
- <artifactId>poi-ooxml-schemas</artifactId>
- <version>4.1.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.xmlbeans</groupId>
- <artifactId>xmlbeans</artifactId>
- <version>5.1.1</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-collections4</artifactId>
- <version>4.4</version>
- </dependency>
- <dependency>
- <groupId>fr.opensagres.xdocreport</groupId>
- <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
- <version>1.0.6</version>
- </dependency>
- <!--ZIP包加密 所需 -->
- <dependency>
- <groupId>net.lingala.zip4j</groupId>
- <artifactId>zip4j</artifactId>
- <version>2.9.1</version>
- </dependency>
复制代码 2、文档加密方法工具类:
测试文档加密工具类
3、测试及测试效果展示:
- public static void main(String[] args) {
- String directoryPath = "D:\\桌面\\项目文档"; // 替换为你本地的路径
- String backupDirectoryPath = "D:\\桌面\\备份"; // 替换为你的备份路径
- String password = "aaaaa111112DDD"; // 密码设置
- try {
- Files.createDirectories(Paths.get(backupDirectoryPath));
- Files.walkFileTree(Paths.get(directoryPath), new SimpleFileVisitor<>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- String fileName = file.getFileName().toString().toLowerCase();
- if (fileName.endsWith(".docx") || fileName.endsWith(".xlsx") || fileName.endsWith(".pdf")) {
- Path backupFile = Paths.get(backupDirectoryPath, file.toString().substring(directoryPath.length()));
- Files.createDirectories(backupFile.getParent());
- Files.copy(file, backupFile, StandardCopyOption.REPLACE_EXISTING);
- try {
- if (fileName.endsWith(".docx")) {
- encryptWordFile(file.toFile(), password);
- } else if (fileName.endsWith(".xlsx")) {
- encryptExcelFile(file.toFile(), password);
- } else if (fileName.endsWith(".pdf")) {
- encryptPdfFile(file.toFile(), password);
- }
- }catch (Exception ex){
- ex.printStackTrace();
- }
- System.out.println("Processed file: " + file.toString());
- } else {
- System.out.println("Skipped file: " + file.toString());
- }
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
- Path backupDir = Paths.get(backupDirectoryPath, dir.toString().substring(directoryPath.length()));
- Files.createDirectories(backupDir);
- System.out.println("Directory: " + dir.toString());
- return FileVisitResult.CONTINUE;
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
复制代码 4、 zip包加密工具类
5、ZIP 方式1测试:
- public static void main(String[] args) {
- String directoryPath = "D:\\桌面\\新建文件夹\\已压缩_未归类"; // 替换为你本地的路径
- String backupDirectoryPath = "D:\\桌面\\备份"; // 替换为你的备份路径
- String password = "741841aaa333.";//密码
- long maxSize = 1024L * 1024L * 1024L; // 1GB
- try {
- Files.createDirectories(Paths.get(backupDirectoryPath));
- // 首先收集所有要处理的文件
- List<Path> filesToProcess = new ArrayList<>();
- Files.walkFileTree(Paths.get(directoryPath), new SimpleFileVisitor<>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- String fileName = file.getFileName().toString().toLowerCase();
- //由于超过1G的文件处理时间过长 所需对此文件大小进行限制 最大为1GB
- if (fileName.endsWith(".zip") && Files.size(file) <= maxSize) {
- filesToProcess.add(file);
- }
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
- Path backupDir = Paths.get(backupDirectoryPath, dir.toString().substring(directoryPath.length()));
- Files.createDirectories(backupDir);
- return FileVisitResult.CONTINUE;
- }
- });
- // 然后处理收集到的文件
- int totalFiles = filesToProcess.size();
- for (int i = 0; i < totalFiles; i++) {
- Path file = filesToProcess.get(i);
- Path backupFile = Paths.get(backupDirectoryPath, file.toString().substring(directoryPath.length()));
- Files.createDirectories(backupFile.getParent());
- Files.copy(file, backupFile, StandardCopyOption.REPLACE_EXISTING);
- ZipEncryptionUtil.encryptZipFile(file.toFile(), password);
- // 记录进度
- System.out.printf("Processed file (%d/%d): %s%n", i + 1, totalFiles, file.toString());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
复制代码 6:zip2加密方式测试 :
须要创建一个新的 ZIP 文件,并将原有的 ZIP 文件(OLD 压缩文件)作为一个单独的文件放入新 ZIP 文件中,同时对新 ZIP 文件举行加密。
方法测试:
- public static void main(String[] args) {
- String directoryPath = "D:\\桌面\\AAA"; // 替换为你本地的路径
- String backupDirectoryPath = "D:\\桌面\\备份"; // 替换为你的备份路径
- String password = "ssssssss.";
- long maxSize = 1024L * 1024L * 1024L; // 1GB
- try {
- Files.createDirectories(Paths.get(backupDirectoryPath));
- // 首先收集所有要处理的文件
- List<Path> filesToProcess = new ArrayList<>();
- Files.walkFileTree(Paths.get(directoryPath), new SimpleFileVisitor<>() {
- @Override
- public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
- String fileName = file.getFileName().toString().toLowerCase();
- if (fileName.endsWith(".zip") && Files.size(file) <= maxSize) {
- filesToProcess.add(file);
- }
- return FileVisitResult.CONTINUE;
- }
- @Override
- public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
- Path backupDir = Paths.get(backupDirectoryPath, dir.toString().substring(directoryPath.length()));
- Files.createDirectories(backupDir);
- return FileVisitResult.CONTINUE;
- }
- });
- // 然后处理收集到的文件
- int totalFiles = filesToProcess.size();
- for (int i = 0; i < totalFiles; i++) {
- Path file = filesToProcess.get(i);
- Path backupFile = Paths.get(backupDirectoryPath, file.toString().substring(directoryPath.length()));
- Files.createDirectories(backupFile.getParent());
- Files.copy(file, backupFile, StandardCopyOption.REPLACE_EXISTING);
- ZipEncryptionUtil.createEncryptedZipWithOldZip(file.toFile(), password);
- // 记录进度
- System.out.printf("Processed file (%d/%d): %s%n", i + 1, totalFiles, file.toString());
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
复制代码 |