java导入zip文件的功能怎么做,现有的资料都是来自同一份模板,只给出了主代码,其他的配置都没有详说,求一个详细的教程
1.读取zip中文件名,返回文件名列表
//读取zip文件内的文件,返回文件名称列表
public static List<String> readZipFileName(String path){
List<String> list = new ArrayList<>();
try {
ZipFile zipFile = new ZipFile(path);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
list.add(entries.nextElement().getName());
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
2.读取zip中文件的内容
//读取zip文件内的文件,返回文件内容列表
public static List<String> readZipFile(String path){
List<String> list = new ArrayList<>();
List<List<String>> ddlList=null;
try {
ZipFile zipFile = new ZipFile(path);
InputStream in = new BufferedInputStream(new FileInputStream(path));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
ddlList=new ArrayList<>();
if (ze.isDirectory()) {
} else {
System.err.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(ze),Charset.forName("gbk")));
String line;
while ((line = br.readLine()) != null) {
String[] index = line.split(",");
List<String> indexList = Arrays.asList(index);
ddlList.add(indexList);
}
br.close();
}
}
//处理ddlList,此时ddlList为每个文件的内容,while每循环一次则读取一个文件
}
zin.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
//此处返回无用,懒得修改了
return list;
}
3 上传zip文件
//保存zip文件
public String saveZipFile(MultipartFile file, String path) {
String resultPath = "";
ZipInputStream zipInputStream = null;
FileOutputStream fs = null;
try {
resultPath = "D:" + path+"/" + file.getOriginalFilename();
File zipFile = new File(resultPath);
if (!zipFile.exists()) {
new File(zipFile.getParent()).mkdirs();
zipFile.createNewFile();
}
zipInputStream = new ZipInputStream(file.getInputStream(), Charset.forName("GBK"));
BufferedInputStream stream = new BufferedInputStream(file.getInputStream());
fs = new FileOutputStream(zipFile);
byte[] buffer = new byte[1024];
int i = -1;
while ((i = stream.read(buffer)) != -1) {
fs.write(buffer, 0, i);
fs.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
zipInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return resultPath;
}
4 获取zip中文件个数
public int getZipFileCount(String zipFilePath) {
ZipFile zf = null;
int count = 0;
try {
zf = new ZipFile(zipFilePath);
count = zf.size(); //返回zip文件中的条目数
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return count;
}
5 解压缩zip(直接解压内部文件,去掉父级目录)
/**
* 解压zip压缩包
* @param path 要解压的文件路径
* @param name 要解压的文件名
* @param resultPath 解压后文件存放路径
*/
public static final void unzip(String path,String name,String resultPath,String code){
try {
ZipFile zipFile = new ZipFile(path+"/"+name);
InputStream in = new BufferedInputStream(new FileInputStream(path+"/"+name));
ZipInputStream zin = new ZipInputStream(in);
ZipEntry ze;
while ((ze = zin.getNextEntry()) != null) {
System.err.println("file - " + ze.getName() + " : "+ ze.getSize() + " bytes");
//创建文件
String newTextPath = resultPath+"/"+ze.getName();
long size = ze.getSize();
if (size > 0) {
BufferedReader br = new BufferedReader(new InputStreamReader(zipFile.getInputStream(ze), Charset.forName(code)));
String line;
while ((line = br.readLine()) != null) {
FileUtil.appendString(line+"\r\n", newTextPath, "UTF-8");
}
br.close();
}
}
zin.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}