Java对zip,rar,7z文件带密码解压实例详解
在Java中,可以通过使用第三方库来实现对压缩文件的解压操作。其中,针对带有密码保护的压缩文件,需要借助专门的工具才能解压。本文将介绍如何使用Java对zip、rar、7z文件带密码进行解压的详细攻略。
I. 依赖库
首先,需要引入以下依赖库:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
II. 解压zip文件
1. 示例1
以下代码展示了如何使用Java对带密码的zip文件进行解压:
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.util.zip.ZipEntry;
public class UnzipDemo {
public static void unzip(String src, String dest, String password) throws IOException {
ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(new FileInputStream(src), password.toCharArray());
ArchiveEntry archiveEntry;
while ((archiveEntry = zipArchiveInputStream.getNextEntry()) != null) {
File entryFile = new File(dest, archiveEntry.getName());
if (archiveEntry.isDirectory()) {
entryFile.mkdirs();
} else {
OutputStream os = new FileOutputStream(entryFile);
IOUtils.copy(zipArchiveInputStream, os);
os.close();
}
}
zipArchiveInputStream.close();
}
public static void main(String[] args) throws IOException {
String src = "test.zip";
String dest = ".";
String password = "123456";
unzip(src, dest, password);
}
}
在上述代码中,unzip
方法接收三个参数:压缩文件的路径,解压文件的目标路径,以及解压密码。其中,通过ZipArchiveInputStream
实例化压缩文件,并通过getNextEntry
方法遍历各个压缩文件内容。最后,针对不同类型的条目进行不同的处理:若是目录,则创建目录,否则创建相应的文件并输出到文件流中。最后关闭文件流和压缩文件。
执行main
方法即可解压该zip文件。
2. 示例2
如果需要从InputStream
流中读取并解压zip文件,可以使用以下代码:
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.util.zip.ZipEntry;
public class UnzipDemo {
public static void unzip(InputStream in, String dest, String password) throws IOException {
ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(in, password.toCharArray());
ArchiveEntry archiveEntry;
while ((archiveEntry = zipArchiveInputStream.getNextEntry()) != null) {
File entryFile = new File(dest, archiveEntry.getName());
if (archiveEntry.isDirectory()) {
entryFile.mkdirs();
} else {
OutputStream os = new FileOutputStream(entryFile);
IOUtils.copy(zipArchiveInputStream, os);
os.close();
}
}
zipArchiveInputStream.close();
}
public static void main(String[] args) throws IOException {
String password = "123456";
String dest = ".";
String src = "test.zip"
InputStream in = new FileInputStream(src);
unzip(in, dest, password);
in.close();
}
}
III. 解压rar文件
1. 示例1
以下代码展示了如何使用Java解压带密码的rar文件:
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.rar.RarArchiveEntry;
import org.apache.commons.compress.archivers.rar.RarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.util.zip.ZipEntry;
public class UnrarDemo {
public static void unrar(String src, String dest, String password) throws IOException {
InputStream inputStream = new FileInputStream(src);
RarArchiveInputStream rarArchiveInputStream = new RarArchiveInputStream(inputStream, password);
ArchiveEntry archiveEntry;
while ((archiveEntry = rarArchiveInputStream.getNextEntry()) != null) {
RarArchiveEntry rarArchiveEntry = (RarArchiveEntry) archiveEntry;
File entryFile = new File(dest, rarArchiveEntry.getName());
if (rarArchiveEntry.isDirectory()) {
entryFile.mkdirs();
} else {
OutputStream os = new FileOutputStream(entryFile);
IOUtils.copy(rarArchiveInputStream, os);
os.close();
}
}
rarArchiveInputStream.close();
}
public static void main(String[] args) throws IOException {
String password = "123456";
String src = "test.rar";
String dest = ".";
unrar(src, dest, password);
}
}
在上述代码中,使用InputStream
将压缩文件读入,借助RarArchiveInputStream
实例化rar文件。通过getNextEntry
方法读取各个文件条目,并针对不同类型的条目进行不同的处理。在本例中,通过判断是否是目录来创建对应的文件夹或文件。
2. 示例2
如果需要从InputStream
流中读取并解压rar文件,可以使用以下代码:
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.ArchiveInputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.rar.RarArchiveEntry;
import org.apache.commons.compress.archivers.rar.RarArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.util.zip.ZipEntry;
public class UnrarDemo {
public static void unrar(InputStream in, String dest, String password) throws IOException {
RarArchiveInputStream rarArchiveInputStream = new RarArchiveInputStream(in, password);
ArchiveEntry archiveEntry;
while ((archiveEntry = rarArchiveInputStream.getNextEntry()) != null) {
RarArchiveEntry rarArchiveEntry = (RarArchiveEntry) archiveEntry;
File entryFile = new File(dest, rarArchiveEntry.getName());
if (rarArchiveEntry.isDirectory()) {
entryFile.mkdirs();
} else {
OutputStream os = new FileOutputStream(entryFile);
IOUtils.copy(rarArchiveInputStream, os);
os.close();
}
}
rarArchiveInputStream.close();
}
public static void main(String[] args) throws IOException {
String password = "123456";
String dest = ".";
String src = "test.rar"
InputStream in = new FileInputStream(src);
unrar(in, dest, password);
in.close();
}
}
IV. 解压7z文件
以下代码展示了如何使用Java对带密码的7z文件进行解压:
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveInputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
public class Un7zDemo {
public static void un7z(String src, String dest, String password) throws IOException {
InputStream inputStream = new FileInputStream(src);
SevenZArchiveInputStream sevenZArchiveInputStream = new SevenZArchiveInputStream(inputStream);
SevenZArchiveEntry sevenZArchiveEntry;
while ((sevenZArchiveEntry = sevenZArchiveInputStream.getNextEntry()) != null
&& !sevenZArchiveEntry.isDirectory()
) {
OutputStream os = new FileOutputStream(new File(dest, sevenZArchiveEntry.getName()));
IOUtils.copy(sevenZArchiveInputStream, os);
os.close();
}
sevenZArchiveInputStream.close();
}
public static void main(String[] args) throws IOException {
String password = "123456";
String src = "test.7z";
String dest = ".";
un7z(src, dest, password);
}
}
在以上代码中,使用InputStream
将压缩文件读入,借助SevenZArchiveInputStream
实例化7z文件。通过getNextEntry
方法读取各个文件条目,并针对不同类型的条目进行不同的处理。在本例中,只有未定义的类型时才继续执行输出操作。
V. 结语
以上代码示例展示了如何使用Java实现对带密码的zip、rar、7z文件进行解压,可以根据具体的需求进行改进。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Java对zip,rar,7z文件带密码解压实例详解 - Python技术站