您可能想要问的是如何在Java中解压缩文件。以下是一个简单的示例代码,演示如何使用Java中的ZipInputStream和ZipEntry类来解压缩文件:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnzipExample {
public static void main(String[] args) {
String zipFilePath = "path/to/your/file.zip";
String destDir = "path/to/destination/folder";
try {
File destDirectory = new File(destDir);
if (!destDirectory.exists()) {
destDirectory.mkdir();
}
byte[] buffer = new byte[1024];
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry zipEntry = zis.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(destDir + File.separator + fileName);
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("File is unzipped successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
您只需将zipFilePath和destDir替换为相应的源文件路径和目标文件夹路径,然后运行该程序即可解压缩文件。