java 解压文件_java实现解压zip文件,(亲测可用)!!!!!!
时间: 2024-03-17 15:43:00 浏览: 90
(JAVA)利用Java实现zip压缩.解压缩.rar_decompress rar java_zip 压缩
好的,这是一个简单的 Java 实现解压 ZIP 文件的示例代码:
```java
import java.io.*;
import java.util.zip.*;
public class Unzip {
public static void main(String[] args) {
String zipFilePath = "path/to/zipfile.zip";
String destDirPath = "path/to/destination/directory";
unzip(zipFilePath, destDirPath);
}
public static void unzip(String zipFilePath, String destDirPath) {
try {
File destDir = new File(destDirPath);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry = zipIn.getNextEntry();
while (entry != null) {
String filePath = destDirPath + File.separator + entry.getName();
if (!entry.isDirectory()) {
extractFile(zipIn, filePath);
} else {
File dir = new File(filePath);
dir.mkdir();
}
zipIn.closeEntry();
entry = zipIn.getNextEntry();
}
zipIn.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
byte[] bytesIn = new byte[4096];
int read = 0;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
bos.close();
}
}
```
你可以将 `zipFilePath` 和 `destDirPath` 替换为你自己的路径,然后运行这个程序即可解压 ZIP 文件。
阅读全文