java exe 解包_Java 实现解压缩文件
时间: 2024-03-13 10:43:14 浏览: 21
Java 可以使用 ZipInputStream 和 ZipEntry 类来解压缩 Zip 文件。以下是一个示例程序:
```java
import java.io.*;
import java.util.zip.*;
public class Unzip {
public static void main(String[] args) throws IOException {
String zipFilePath = "example.zip";
String destDirPath = "output";
// 创建输出目录
File destDir = new File(destDirPath);
if (!destDir.exists()) {
destDir.mkdir();
}
// 打开 Zip 文件
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
// 读取 Zip 文件中的每个 entry
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String entryName = entry.getName();
String filePath = destDirPath + File.separator + entryName;
// 如果 entry 是目录,则创建目录
if (entry.isDirectory()) {
File dir = new File(filePath);
dir.mkdir();
} else { // 如果 entry 是文件,则写入文件
FileOutputStream fos = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int len;
while ((len = zipIn.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
}
}
```
该程序将解压缩 `example.zip` 文件到 `output` 目录中。如果 Zip 文件中有目录,则程序会创建相应的目录。