您可能想要問的是如何在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替換為相應的源文件路徑和目標文件夾路徑,然后運行該程序即可解壓縮文件。