在 Java 中,你可以使用不同的方式进行 ZIP 打包而不进行压缩。下面我将介绍两种常见的实现方式,包括使用 Java 标准库和使用第三方库。对于每种方式,我将提供步骤流程以及示例代码,并列出相关的 Maven 和 Gradle 依赖坐标。
Java 的 java.util.zip 包提供了创建 ZIP 文件的功能。这种方式下,你可以创建一个不包含压缩的 ZIP 文件。
步骤流程:
ZipOutputStream 将文件添加到 ZIP 文件中,但不进行压缩。ZipOutputStream 以完成 ZIP 文件的创建。示例代码:
import java.io.*;
import java.util.zip.*;
public class UncompressedZipExample {
    public static void main(String[] args) {
        String sourceFolderPath = "path/to/source/folder";
        String zipFilePath = "path/to/output/file.zip";
        try (FileOutputStream fos = new FileOutputStream(zipFilePath);
             ZipOutputStream zipOut = new ZipOutputStream(fos)) {
            File sourceFolder = new File(sourceFolderPath);
            addFolderToZip(sourceFolder, sourceFolder.getName(), zipOut);
            System.out.println("Uncompressed ZIP file created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void addFolderToZip(File folder, String parentFolderName, ZipOutputStream zipOut) throws IOException {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    addFolderToZip(file, parentFolderName + "/" + file.getName(), zipOut);
                } else {
                    FileInputStream fis = new FileInputStream(file);
                    ZipEntry zipEntry = new ZipEntry(parentFolderName + "/" + file.getName());
                    zipOut.putNextEntry(zipEntry);
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zipOut.write(bytes, 0, length);
                    }
                    fis.close();
                }
            }
        }
    }
}
Maven 依赖:
这种方式不需要额外的依赖。
Apache Commons Compress 库提供了更高级的 ZIP 操作功能,允许你创建未压缩的 ZIP 文件。
步骤流程:
org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream 将文件添加到 ZIP 文件中,但不进行压缩。ZipArchiveOutputStream 以完成 ZIP 文件的创建。示例代码:
import org.apache.commons.compress.archivers.zip.*;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.nio.file.Paths;
public class UncompressedZipCommonsCompressExample {
    public static void main(String[] args) {
        String sourceFolderPath = "path/to/source/folder";
        String zipFilePath = "path/to/output/file.zip";
        try (OutputStream fos = new FileOutputStream(zipFilePath);
             ZipArchiveOutputStream zipOut = new ZipArchiveOutputStream(fos)) {
            File sourceFolder = new File(sourceFolderPath);
            addFolderToZip(sourceFolder, sourceFolder.getName(), zipOut);
            System.out.println("Uncompressed ZIP file created successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void addFolderToZip(File folder, String parentFolderName, ZipArchiveOutputStream zipOut) throws IOException {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    addFolderToZip(file, Paths.get(parentFolderName, file.getName()).toString(), zipOut);
                } else {
                    try (FileInputStream fis = new FileInputStream(file)) {
                        ZipArchiveEntry zipEntry = new ZipArchiveEntry(Paths.get(parentFolderName, file.getName()).toString());
                        zipOut.putArchiveEntry(zipEntry);
                        IOUtils.copy(fis, zipOut);
                        zipOut.closeArchiveEntry();
                    }
                }
            }
        }
    }
}
Maven 依赖:
在你的 pom.xml 文件中添加以下依赖:
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-compress</artifactId>
    <version>1.21</version>
</dependency>
Gradle 依赖:
在你的 build.gradle 文件中添加以下依赖:
dependencies {
    implementation 'org.apache.commons:commons-compress:1.21'
}
这两种方式都可以用来创建不进行压缩的 ZIP 文件,选择其中之一根据你的项目需求和偏好进行使用。