在 Java 中,可以使用多种方式来压缩文件夹下的所有文件。以下是三种常用的实现方式:使用 Java 标准库、使用 Apache Commons Compress 库和使用 Java 的 ZipOutputStream。
Java 标准库提供了 ZipOutputStream 类来进行文件压缩。
步骤流程:
示例代码:
import java.io.*;
import java.util.zip.*;
public class ZipExample {
public static void main(String[] args) throws IOException {
String sourceFolder = "path/to/source/folder";
String zipFile = "path/to/output/zipfile.zip";
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(fos);
File fileToZip = new File(sourceFolder);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) >= 0) {
zipOut.write(buffer, 0, length);
}
fis.close();
}
}
Apache Commons Compress 库提供了更多压缩格式的支持,如 gzip、tar 等。
Maven 依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
Gradle 依赖:
implementation 'org.apache.commons:commons-compress:1.21'
步骤流程:
示例代码:
import org.apache.commons.compress.archivers.*;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
public class CommonsCompressExample {
public static void main(String[] args) throws IOException {
String sourceFolder = "path/to/source/folder";
String zipFile = "path/to/output/zipfile.zip";
FileOutputStream fos = new FileOutputStream(zipFile);
ArchiveOutputStream archiveOut = new ArchiveStreamFactory()
.createArchiveOutputStream(ArchiveStreamFactory.ZIP, fos);
File fileToZip = new File(sourceFolder);
zipFile(fileToZip, "", archiveOut);
archiveOut.close();
fos.close();
}
private static void zipFile(File fileToZip, String entryName, ArchiveOutputStream archiveOut) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
ArchiveEntry archiveEntry = archiveOut.createArchiveEntry(fileToZip, entryName + "/");
archiveOut.putArchiveEntry(archiveEntry);
archiveOut.closeArchiveEntry();
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, entryName + "/" + childFile.getName(), archiveOut);
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ArchiveEntry archiveEntry = archiveOut.createArchiveEntry(fileToZip, entryName);
archiveOut.putArchiveEntry(archiveEntry);
IOUtils.copy(fis, archiveOut);
fis.close();
archiveOut.closeArchiveEntry();
}
}
这是使用 Java 内置的 ZipOutputStream 类的另一种方式。
步骤流程:
示例代码:
import java.io.*;
import java.util.zip.*;
public class ZipOutputStreamExample {
public static void main(String[] args) throws IOException {
String sourceFolder = "path/to/source/folder";
String zipFile = "path/to/output/zipfile.zip";
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zipOut = new ZipOutputStream(fos);
File fileToZip = new File(sourceFolder);
zipFile(fileToZip, fileToZip.getName(), zipOut);
zipOut.close();
fos.close();
}
private static void zipFile(File fileToZip, String fileName, ZipOutputStream zipOut) throws IOException {
if (fileToZip.isHidden()) {
return;
}
if (fileToZip.isDirectory()) {
File[] children = fileToZip.listFiles();
for (File childFile : children) {
zipFile(childFile, fileName + "/" + childFile.getName(), zipOut);
}
return;
}
FileInputStream fis = new FileInputStream(fileToZip);
ZipEntry zipEntry = new ZipEntry(fileName);
zipOut.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) >= 0) {
zipOut.write(buffer, 0, length);
}
fis.close();
}
}
无论你选择哪种实现方式,都可以根据自己的需求来压缩文件夹下的所有文件。请注意替换示例代码中的文件路径以匹配你的实际情况。