Java的java.util.zip
包提供了对ZIP文件格式的支持,可以用于读取和写入ZIP文件。但是,对于其他压缩格式,如RAR、7z等,Java标准库本身并不直接支持。不过,你可以使用第三方库来处理这些格式。
以下是一些常用的第三方库及其支持的压缩格式:
pom.xml
文件中添加以下依赖:<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
使用示例:
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class UnzipUtility {
public static void main(String[] args) throws IOException {
// 解压ZIP文件
unzip("path/to/your/zipfile.zip", "path/to/output/directory");
// 解压Gzip压缩文件
unzipGzip("path/to/your/file.gz", "path/to/output/directory");
}
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new FileInputStream(zipFilePath))) {
ArchiveEntry entry = zis.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zis, filePath);
} else {
File dir = new File(filePath);
dir.mkdirs();
}
zis.closeEntry();
entry = zis.getNextEntry();
}
}
}
public static void unzipGzip(String gzipFilePath, String destDirectory) throws IOException {
try (GzipCompressorInputStream gis = new GzipCompressorInputStream(new FileInputStream(gzipFilePath));
ZipArchiveInputStream zis = new ZipArchiveInputStream(gis)) {
ArchiveEntry entry = zis.getNextEntry();
while (entry != null) {
String filePath = destDirectory + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zis, filePath);
} else {
File dir = new File(filePath);
dir.mkdirs();
}
zis.closeEntry();
entry = zis.getNextEntry();
}
}
}
private static void extractFile(InputStream inputStream, String filePath) throws IOException {
try (FileOutputStream fos = new FileOutputStream(filePath)) {
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
}
}
}
pom.xml
文件中添加以下依赖:<dependency>
<groupId>net.java.dev.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding</artifactId>
<version>9.28</version>
</dependency>
使用示例:
import net.java.dev.sevenzipjbinding.ArchiveFormat;
import net.java.dev.sevenzipjbinding.SevenZip;
import net.java.dev.sevenzipjbinding.SimpleInStream;
import net.java.dev.sevenzipjbinding.impl.RandomAccessFileInStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class UnzipSevenZip {
public static void main(String[] args) throws IOException {
// 解压7z文件
unzipSevenZip("path/to/your/file.7z", "path/to/output/directory");
}
public static void unzipSevenZip(String sevenZipFile, String destDirectory) throws IOException {
File outputFile = new File(destDirectory);
outputFile.mkdirs();
try (RandomAccessFileInStream rafis = new RandomAccessFileInStream(new FileInputStream(sevenZipFile));
SimpleInStream sis = new SimpleInStream(rafis)) {
SevenZip.decompress(sis, destDirectory, ArchiveFormat.SEVEN_ZIP);
}
}
}
这些库可以帮助你在Java中处理不同的压缩格式。