解压,输入一个压缩包文件,解压到path下filename前10位为文件夹的指定目录,返回url ,文件名里的 姓名 身份证号该怎么写java
时间: 2024-02-01 19:14:20 浏览: 17
可以使用Java自带的ZipInputStream类来实现解压缩,具体代码如下:
```java
import java.io.*;
import java.util.zip.*;
public class UnzipFile {
public static String unzip(String zipFilePath, String outputPath) throws IOException {
String folderName = "";
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath));
ZipEntry entry;
byte[] buffer = new byte[4096];
while ((entry = zis.getNextEntry()) != null) {
String fileName = entry.getName();
if (fileName.length() >= 10) {
folderName = fileName.substring(0, 10);
fileName = fileName.substring(10);
}
File outputFile = new File(outputPath + File.separator + folderName + File.separator + fileName);
if (entry.isDirectory()) {
outputFile.mkdirs();
} else {
FileOutputStream fos = new FileOutputStream(outputFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
}
zis.close();
return outputPath + File.separator + folderName;
}
}
```
其中,zipFilePath为需要解压的压缩包文件路径,outputPath为解压到的目标文件夹路径,函数返回值为解压后的文件夹路径。
至于文件名里的姓名和身份证号,可以将其作为文件名的一部分来命名,比如:
```java
String fileName = "张三_310101199001012345.doc";
```
这样就将姓名和身份证号与文件名关联起来了。