| | |
| | | package com.dji.sample.patches.utils; |
| | | |
| | | import com.amazonaws.util.IOUtils; |
| | | import org.springframework.context.annotation.Configuration; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import javax.servlet.ServletOutputStream; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.nio.charset.Charset; |
| | | import java.util.ArrayList; |
| | | import java.util.Enumeration; |
| | | import java.util.List; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | |
| | | /** |
| | | * @author soulmate丶 |
| | | * @date 2021-10-26 |
| | | */ |
| | | import java.util.zip.ZipOutputStream; |
| | | @Configuration |
| | | public class ZipUtil { |
| | | |
| | | |
| | | /** |
| | | * 保存zip文件到本地并调用解压方法并返回解压出的文件的路径集合 |
| | | * |
| | | * @param file 文件 |
| | | * @return list //解压出的文件的路径合集 |
| | | */ |
| | | private static String zipPath = "D:/ceshi/tuban.zip";//zip根路径 |
| | | |
| | | /** |
| | | * zip解压 |
| | | * |
| | |
| | | // 如果是文件,就先创建一个文件,然后用io流把内容copy过去 |
| | | File targetFile = new File(destDirPath + File.separator + entry.getName()); |
| | | // 保证这个文件的父文件夹必须要存在 |
| | | File parentFile = targetFile.getParentFile(); |
| | | if (!parentFile.exists()) { |
| | | parentFile.mkdirs(); |
| | | } |
| | | |
| | | list.add(destDirPath + entry.getName()); |
| | | if (!targetFile.getParentFile().exists()) { |
| | | |
| | | } |
| | | targetFile.createNewFile(); |
| | | // 将压缩文件内容写入到这个文件中 |
| | | InputStream is = zipFile.getInputStream(entry); |
| | |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | | * @param filePath 临时文件的删除 |
| | | * 删除文件夹里面子目录 |
| | | * 再删除文件夹 |
| | | */ |
| | | public static void deleteFiles(String filePath) { |
| | | File file = new File(filePath); |
| | | if ((!file.exists()) || (!file.isDirectory())) { |
| | | // 定义一个公共的静态方法zipFolder,用于压缩文件夹 |
| | | public static boolean zipFolder(String sourceFolderPath, String zipFilePath) { |
| | | File sourceFile = new File(sourceFolderPath); |
| | | try ( |
| | | FileOutputStream fos = new FileOutputStream(zipFilePath); |
| | | ZipOutputStream zos = new ZipOutputStream(fos) |
| | | ) { |
| | | zipFile(sourceFile, sourceFile.getName(), zos); |
| | | return true; |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | // 将文件夹整体压缩 |
| | | private static void zipFile(File fileToZip, String fileName, ZipOutputStream zos) throws IOException { |
| | | if (fileToZip.isHidden()) { |
| | | return; |
| | | } |
| | | String[] tempList = file.list(); |
| | | File temp = null; |
| | | for (int i = 0; i < tempList.length; i++) { |
| | | if (filePath.endsWith(File.separator)) { |
| | | temp = new File(filePath + tempList[i]); |
| | | if (fileToZip.isDirectory()) { |
| | | if (fileName.endsWith("/")) { |
| | | zos.putNextEntry(new ZipEntry(fileName)); |
| | | zos.closeEntry(); |
| | | } else { |
| | | temp = new File(filePath + File.separator + tempList[i]); |
| | | zos.putNextEntry(new ZipEntry(fileName + "/")); |
| | | zos.closeEntry(); |
| | | } |
| | | if (temp.isFile()) { |
| | | temp.delete(); |
| | | File[] children = fileToZip.listFiles(); |
| | | for (File childFile : children) { |
| | | zipFile(childFile, fileName + "/" + childFile.getName(), zos); |
| | | } |
| | | if (temp.isDirectory()) { |
| | | deleteFiles(filePath + "\\" + tempList[i]); |
| | | } |
| | | return; |
| | | } |
| | | // 空文件的删除 |
| | | file.delete(); |
| | | FileInputStream fis = new FileInputStream(fileToZip); |
| | | ZipEntry zipEntry = new ZipEntry(fileName); |
| | | zos.putNextEntry(zipEntry); |
| | | byte[] bytes = new byte[1024]; |
| | | int length; |
| | | while ((length = fis.read(bytes)) >= 0) { |
| | | zos.write(bytes, 0, length); |
| | | } |
| | | fis.close(); |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // 调用方法进行测试 |
| | | } |
| | | } |