java解压zip文件
java解压zip文件并且获取所有的文件名
1 解压方式一
1.1 Java代码
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
public class UnzipAndListFiles {
public static void main(String[] args) {
String zipFilePath = "D:\\河流1.zip";
String parentFilepath = getParentFilepath(zipFilePath);
System.out.println("parentFilepath = " + parentFilepath);
String s = unzipAndPrintPaths(zipFilePath, parentFilepath);
System.out.println("shp文件路径 = " + s);
}
public static String unzipAndPrintPaths(String zipFilePath, String destDir) {
//所有的文件路径
List<String> filePaths = new ArrayList<>();
File destDirectory = new File(destDir);
if (!destDirectory.exists()) {
destDirectory.mkdirs();
}
try (ZipFile zipFile = new ZipFile(zipFilePath)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String filePath = destDir + File.separator + entry.getName();
// 确保目标文件路径的父目录存在
File entryDestination = new File(filePath);
File parentDirectory = entryDestination.getParentFile();
if (parentDirectory != null && !parentDirectory.exists()) {
parentDirectory.mkdirs();
}
if (!entry.isDirectory()) {
// 提取文件
try (InputStream is = zipFile.getInputStream(entry);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath))) {
byte[] bytesIn = new byte[4096];
int read;
while ((read = is.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
// 打印文件路径
System.out.println("Extracted: " + filePath);
filePaths.add(filePath);
} else {
// 如果是目录,则创建目录(注意:在上面的mkdirs调用中已经处理了)
// 这里也可以选择打印目录路径
System.out.println("Created directory: " + filePath);
}
}
} catch (IOException e) {
e.printStackTrace();
}
//获取特定类型的文件信息
for (String filePath : filePaths) {
if (filePath.endsWith(".shp")) {
return filePath;
}
}
return null;
}
//获取父目录
public static String getParentFilepath(String fullPath ) {
Path path = Paths.get(fullPath);
// 使用getParent()方法来获取父路径,即目录部分
Path directoryPath = path.getParent();
if (directoryPath != null) {
System.out.println("Directory path: " + directoryPath);
return directoryPath.toString();
} else {
System.out.println("No parent directory.");
return null;
}
}
}
1.2 存在的问题
如果zip里边是常见的文件,这个时候使用上边的代码是没问题的,遇到的问题,如果zip里边包含.prj的文件,就会解压报错
Exception in thread “main” java.lang.IllegalArgumentException: MALFORMED
Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED
at java.util.zip.ZipCoder.toString(ZipCoder.java:58)
at java.util.zip.ZipFile.getZipEntry(ZipFile.java:583)
at java.util.zip.ZipFile.access$900(ZipFile.java:60)
at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:539)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:514)
at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:495)
at com.yq.geotools.UnzipAndListFiles.unzipAndPrintPaths(UnzipAndListFiles.java:37)
at com.yq.geotools.UnzipAndListFiles.main(UnzipAndListFiles.java:20)
1.3 解决的办法
使用下边的解压方式2
2 解压方式二
使用org.apache.commons的解压方式
2.1 Java代码
添加的依赖
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-vfs2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-vfs2</artifactId>
<version>2.9.0</version>
</dependency>
代码
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class ZipFileExtractor {
public static List<String> unzip(String zipFilePath, String destDirectory) throws IOException {
List<String> filePaths = new ArrayList<>();
try (InputStream fis = new FileInputStream(zipFilePath);
ZipArchiveInputStream zipIn = new ZipArchiveInputStream(fis, null, true, true)) {
ArchiveEntry entry;
while ((entry = zipIn.getNextEntry()) != null) {
String filePath = destDirectory + File.separator + entry.getName();
File file = new File(filePath);
if (entry.isDirectory()) {
file.mkdirs();
} else {
// Ensure parent directory exists
File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
extractFile(zipIn, file);
filePaths.add(filePath);
}
}
}
return filePaths;
}
private static void extractFile(InputStream zipIn, File file) throws IOException {
try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file))) {
byte[] bytesIn = new byte[4096];
int read;
while ((read = zipIn.read(bytesIn)) != -1) {
bos.write(bytesIn, 0, read);
}
}
}
//获取父目录
public static String getParentFilepath(String fullPath ) {
Path path = Paths.get(fullPath);
// 使用getParent()方法来获取父路径,即目录部分
Path directoryPath = path.getParent();
if (directoryPath != null) {
System.out.println("Directory path: " + directoryPath);
return directoryPath.toString();
} else {
System.out.println("No parent directory.");
return null;
}
}
public static void main(String[] args) {
String zipFilePath = "D:\\河流.zip";
String destDirectory = getParentFilepath(zipFilePath);
System.out.println("destDirectory = " + destDirectory);
try {
List<String> filePaths = unzip(zipFilePath, destDirectory);
for (String path : filePaths) {
if (path.endsWith(".shp")) {
System.out.println("Extracted: " + path);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}