java解析嵌套zip_java读取zip压缩包文件及文件内容

这篇博客介绍了如何使用Java进行ZIP压缩包的操作,包括读取ZIP文件中的文件名列表、读取文件内容、保存上传的ZIP文件、获取ZIP文件中的文件个数以及直接解压ZIP内部文件到指定路径,代码示例详细展示了实现过程。

1.读取zip中文件名,返回文件名列表

//读取zip文件内的文件,返回文件名称列表

public static List readZipFileName(String path){

List 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 readZipFile(String path){

List list = new ArrayList<>();

List> 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 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();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值