您好,登录后才能下订单哦!
在日常开发中,我们经常会遇到需要处理压缩文件的情况。常见的压缩文件格式有zip、rar和7z。Java作为一种广泛使用的编程语言,提供了多种方式来处理这些压缩文件。本文将详细介绍如何使用Java对zip、rar和7z文件进行解压,并且支持带密码解压。
Java标准库中的java.util.zip
包提供了对zip文件的基本支持。我们可以使用ZipFile
和ZipEntry
类来读取和解压zip文件。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class ZipUtil {
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipFile zipFile = new ZipFile(zipFilePath);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File entryDestination = new File(destDir, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
InputStream in = zipFile.getInputStream(entry);
FileOutputStream out = new FileOutputStream(entryDestination);
byte[] buffer = new byte[1024];
int len;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
out.close();
in.close();
}
}
zipFile.close();
}
public static void main(String[] args) {
try {
unzip("example.zip", "output");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Apache Commons Compress库提供了更强大的压缩文件处理功能,支持多种压缩格式,包括zip、tar、gz等。
首先,需要在pom.xml
中添加依赖:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
然后,可以使用以下代码解压zip文件:
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
public class ZipUtil {
public static void unzip(String zipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
try (ZipFile zipFile = new ZipFile(new File(zipFilePath))) {
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
File entryDestination = new File(destDir, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
try (InputStream in = zipFile.getInputStream(entry);
FileOutputStream out = new FileOutputStream(entryDestination)) {
IOUtils.copy(in, out);
}
}
}
}
}
public static void main(String[] args) {
try {
unzip("example.zip", "output");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java标准库并不支持rar文件的解压,但我们可以使用第三方库junrar
来处理rar文件。
首先,需要在pom.xml
中添加依赖:
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>7.5.1</version>
</dependency>
然后,可以使用以下代码解压rar文件:
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class RarUtil {
public static void unrar(String rarFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
try (Archive archive = new Archive(new File(rarFilePath))) {
FileHeader fileHeader;
while ((fileHeader = archive.nextFileHeader()) != null) {
File entryDestination = new File(destDir, fileHeader.getFileNameString());
if (fileHeader.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
try (FileOutputStream out = new FileOutputStream(entryDestination)) {
archive.extractFile(fileHeader, out);
}
}
}
}
}
public static void main(String[] args) {
try {
unrar("example.rar", "output");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java标准库同样不支持7z文件的解压,但我们可以使用SevenZip-JBinding
库来处理7z文件。
首先,需要在pom.xml
中添加依赖:
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding</artifactId>
<version>16.02-2.01</version>
</dependency>
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding-all-platforms</artifactId>
<version>16.02-2.01</version>
</dependency>
然后,可以使用以下代码解压7z文件:
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class SevenZipUtil {
public static void un7z(String sevenZipFilePath, String destDirectory) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
try (RandomAccessFile randomAccessFile = new RandomAccessFile(sevenZipFilePath, "r");
IInArchive inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile))) {
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
if (!item.isFolder()) {
File entryDestination = new File(destDir, item.getPath());
entryDestination.getParentFile().mkdirs();
try (FileOutputStream out = new FileOutputStream(entryDestination)) {
item.extractSlow(data -> {
try {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}
return data.length;
});
}
}
}
}
}
public static void main(String[] args) {
try {
un7z("example.7z", "output");
} catch (IOException e) {
e.printStackTrace();
}
}
}
对于带密码的zip文件,我们可以使用Zip4j
库来处理。
首先,需要在pom.xml
中添加依赖:
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>2.9.0</version>
</dependency>
然后,可以使用以下代码解压带密码的zip文件:
import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.exception.ZipException;
public class Zip4jUtil {
public static void unzipWithPassword(String zipFilePath, String destDirectory, String password) throws ZipException {
ZipFile zipFile = new ZipFile(zipFilePath);
if (zipFile.isEncrypted()) {
zipFile.setPassword(password.toCharArray());
}
zipFile.extractAll(destDirectory);
}
public static void main(String[] args) {
try {
unzipWithPassword("example.zip", "output", "password");
} catch (ZipException e) {
e.printStackTrace();
}
}
}
junrar
库目前不支持带密码的rar文件解压。如果需要解压带密码的rar文件,可以考虑使用命令行工具unrar
,并通过Java调用命令行。
SevenZip-JBinding
库支持带密码的7z文件解压。我们可以在解压时提供密码。
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
public class SevenZipUtil {
public static void un7zWithPassword(String sevenZipFilePath, String destDirectory, String password) throws IOException {
File destDir = new File(destDirectory);
if (!destDir.exists()) {
destDir.mkdir();
}
try (RandomAccessFile randomAccessFile = new RandomAccessFile(sevenZipFilePath, "r");
IInArchive inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile))) {
ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();
for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
if (!item.isFolder()) {
File entryDestination = new File(destDir, item.getPath());
entryDestination.getParentFile().mkdirs();
try (FileOutputStream out = new FileOutputStream(entryDestination)) {
item.extractSlow(data -> {
try {
out.write(data);
} catch (IOException e) {
e.printStackTrace();
}
return data.length;
}, password);
}
}
}
}
}
public static void main(String[] args) {
try {
un7zWithPassword("example.7z", "output", "password");
} catch (IOException e) {
e.printStackTrace();
}
}
}
本文详细介绍了如何使用Java对zip、rar和7z文件进行解压,并且支持带密码解压。对于zip文件,我们可以使用java.util.zip
包或Apache Commons Compress
库;对于rar文件,可以使用junrar
库;对于7z文件,可以使用SevenZip-JBinding
库。带密码解压方面,Zip4j
库支持带密码的zip文件解压,SevenZip-JBinding
库支持带密码的7z文件解压,而junrar
库目前不支持带密码的rar文件解压。
希望本文能帮助你在Java项目中更好地处理压缩文件。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。