python java 解压缩
时间: 2023-11-01 17:59:32 浏览: 152
Python和Java都有一些解压缩库可以用来解压缩文件。对于Python,您可以使用zipfile模块,它提供了用于解压缩zip文件的功能。下面是一个示例代码,演示了如何使用zipfile模块解压缩zip文件:
```python
import zipfile
zip_file = "path/to/your/zip/file.zip"
extract_dir = "path/to/extract/folder"
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
zip_ref.extractall(extract_dir)
```
对于Java,您可以使用java.util.zip包中的ZipInputStream类来解压缩zip文件。下面是一个示例代码,演示了如何使用ZipInputStream解压缩zip文件:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public class UnzipExample {
public static void main(String[] args) {
String zipFile = "path/to/your/zip/file.zip";
String extractDir = "path/to/extract/folder";
try {
byte[] buffer = new byte[1024];
File folder = new File(extractDir);
if (!folder.exists()) {
folder.mkdir();
}
ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry zipEntry = zipInputStream.getNextEntry();
while (zipEntry != null) {
String fileName = zipEntry.getName();
File newFile = new File(extractDir + File.separator + fileName);
new File(newFile.getParent()).mkdirs();
FileOutputStream fileOutputStream = new FileOutputStream(newFile);
int length;
while ((length = zipInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}
fileOutputStream.close();
zipEntry = zipInputStream.getNextEntry();
}
zipInputStream.closeEntry();
zipInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
阅读全文