企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
要实现对ZIP文件的压缩与解压,最基本需要ZipOutputStream、ZipInputStream、ZipEntry三个类的配合。它们都在java.util.zip包中。 * ZipOutputStream类:负责压缩文件,创建.zip文件。需要调用该类的`putNextEntry`方法为每个文件找到目录进入点。 * ZipInputStream类:负责解压文件。需要调用该类的`getNextEntry`获取每个被压缩文件的目录进入点。 * ZipEntry类:称为目录进入点,负责将文件压缩到压缩文件的指定位置。 ``` 例如:如果要压缩的文件目录为: zipfile| |001 |0011.txt |0012.txt |002 |01.txt 则ZipEntry负责找到上面这些被压缩文件的压缩路径,假设压缩文件为zipfile.zip,则各个 文件的路径为: zipfile.zip| |zipfile |001 |0011.txt |0012.txt |002 |01.txt ``` ```java public class ZipFileTest { /** * 将文件或文件夹压缩为 .zip 文件 * * @param targetPath 需要被压缩的文件/文件夹的绝对路径 * @param outputPath 生成的压缩文件的绝对路径 */ public static void zipCompress(String targetPath, String outputPath) throws Exception { // 创建.zip文件 ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputPath)); BufferedOutputStream bos = new BufferedOutputStream(out); File targetFile = new File(targetPath); System.out.println("压缩中..."); compress(out, bos, targetFile, null); System.out.println("压缩完成!"); bos.close(); out.close(); } /** * 递归,将每个目录下的文件压缩到同一个.zip文件中 * * @param out ZipOutputStream * @param bos BufferedOutputStream * @param targetFile 待压缩的文件或文件夹 * @param name 生成压缩文件的文件名。如果为null,则使用默认方式处理 * @throws IOException */ public static void compress(ZipOutputStream out, BufferedOutputStream bos, File targetFile, String name) throws IOException { if (name == null) { // 如果为不指定生成的压缩文件名,则使用默认方式处理 name = targetFile.getName(); } if (targetFile.isDirectory()) { File[] listFiles = targetFile.listFiles(); if (listFiles.length == 0) { // 假如要压缩的文件夹为emptyfile,则压缩路径为emptyfile.zip/emptyfile out.putNextEntry(new ZipEntry(name + "/")); } else { for (File single : listFiles) { compress(out, bos, single, name + "/" + single.getName()); } } } else { // 如果为文件,则先写入目录进入点,之后将文件写入zip文件中 out.putNextEntry(new ZipEntry(name)); FileInputStream fos = new FileInputStream(targetFile); BufferedInputStream bis = new BufferedInputStream(fos); int len = -1; // 将源文件写入到zip文件中 byte[] buf = new byte[1024]; while ((len = bis.read(buf)) != -1) { bos.write(buf, 0, len); } bis.close(); fos.close(); } } /** * 对 .zip 压缩文件进行解压 * * @param targetPath 待解压的.zip文件的绝对路径 * @param destDirPath 解压路径 */ public static void zipUncompress(String targetPath, String destDirPath) throws Exception { File srcFile = new File(targetPath); // 判断源文件是否存在 if (!srcFile.exists()) { throw new Exception(srcFile.getPath() + "所指文件不存在"); } System.out.println("正在解压..."); ZipInputStream zIn = new ZipInputStream(new FileInputStream(srcFile)); ZipEntry entry = null; File destFile = null; while ((entry = zIn.getNextEntry()) != null) { if (!entry.isDirectory()) { // 只解压文件,空文件夹不解压 // 如果压缩的文件路径是:zipfile.zip/zipfile/word.txt, // 则entry.getName()返回的是zipfile/word.txt,而不是word.txt destFile = new File(destDirPath, entry.getName()); if (!destFile.exists()) { // 文件不存在,则创建此文件的上级目录 // 假如:tzy/zipfile/00.txt文件不存在,则创建tzy/zipfile/目录,但不包括00.txt new File(destFile.getParent()).mkdirs(); } OutputStream out = new FileOutputStream(destFile); BufferedOutputStream bos = new BufferedOutputStream(out); int len = -1; byte[] buf = new byte[1024]; while ((len = zIn.read(buf)) != -1) { bos.write(buf, 0, len); } bos.close(); out.close(); } else { // 对空的目录创建一个路径 new File(destDirPath, entry.getName()).mkdirs(); } } System.out.println("解压完成!"); } public static void main(String[] args) { try { zipCompress("F:\\zipfile", "F:\\zipfile.zip"); zipUncompress("F:\\zipfile.zip", "F:\\tzy"); } catch (Exception e) { e.printStackTrace(); } } } ```