在日常开发中,我们经常需要对文件进行压缩以节省存储空间或加快网络传输速度。Java 提供了多种内置方式来实现文件压缩功能,比如使用 java.util.zip 包中的类来处理 ZIP 和 GZIP 格式。本教程将带你从零开始,掌握 Java压缩文件 的基本方法,无论你是初学者还是有一定经验的开发者,都能轻松上手。
ZIP 是最常见的压缩格式之一。Java 提供了 ZipOutputStream 类来创建 ZIP 文件。
import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipCompressor { public static void compressFileToZip(String sourceFilePath, String zipFilePath) throws IOException { try (FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos)) { File sourceFile = new File(sourceFilePath); FileInputStream fis = new FileInputStream(sourceFile); // 创建 ZIP 条目 ZipEntry zipEntry = new ZipEntry(sourceFile.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } public static void main(String[] args) { try { compressFileToZip("example.txt", "example.zip"); System.out.println("文件压缩成功!"); } catch (IOException e) { e.printStackTrace(); } }} 如果你要压缩一个包含多个文件和子目录的文件夹,可以递归遍历目录结构:
import java.io.*;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class FolderZipper { public static void zipFolder(String sourceDirPath, String zipFilePath) throws IOException { try (FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos)) { File sourceDir = new File(sourceDirPath); addFolderToZip("", sourceDir, zos); } } private static void addFolderToZip(String path, File folder, ZipOutputStream zos) throws IOException { for (File file : folder.listFiles()) { if (file.isDirectory()) { addFolderToZip(path + file.getName() + "/", file, zos); } else { byte[] buffer = new byte[1024]; FileInputStream fis = new FileInputStream(file); zos.putNextEntry(new ZipEntry(path + file.getName())); int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } zos.closeEntry(); fis.close(); } } } public static void main(String[] args) { try { zipFolder("myFolder/", "myFolder.zip"); System.out.println("文件夹压缩成功!"); } catch (IOException e) { e.printStackTrace(); } }} GZIP 是另一种常见的压缩格式,特别适合压缩单个大文件。Java 提供了 GZIPOutputStream 来实现 GZIP 压缩。
import java.io.*;import java.util.zip.GZIPOutputStream;public class GzipCompressor { public static void compressToGzip(String sourceFilePath, String gzipFilePath) throws IOException { try (FileInputStream fis = new FileInputStream(sourceFilePath); FileOutputStream fos = new FileOutputStream(gzipFilePath); GZIPOutputStream gzos = new GZIPOutputStream(fos)) { byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { gzos.write(buffer, 0, length); } } } public static void main(String[] args) { try { compressToGzip("largefile.txt", "largefile.txt.gz"); System.out.println("GZIP 压缩成功!"); } catch (IOException e) { e.printStackTrace(); } }} FileNotFoundException。通过本教程,你已经学会了如何使用 Java 实现 Java ZIP压缩 和 Java GZIP压缩。无论是压缩单个文件还是整个目录,Java 都提供了简洁高效的 API。希望这篇 Java文件压缩教程 能帮助你在项目中轻松处理压缩需求!
提示:实际开发中可结合 Apache Commons Compress 等第三方库实现更复杂的压缩逻辑,但掌握原生 Java 方法是基础。
本文由主机测评网于2025-12-16发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/2025128689.html