Zip文件解析(包含中文目录)

2 篇文章 0 订阅

在解析zip文件时,由于默认版本不支持中文目录,导致解析时遇到中文文件目录会出现乱码:

从网上找到一些资料,自己写了个样例。有兴趣的可以看看。(需要修改原有的zip包,添加构造方法,支持gbk编码)

具体修改class为 ZipinputStream和ZipOutputStream(代码见附件中)

见下方压缩与解析代码:

 

package ziphandle;

 

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.LinkedList;

import java.util.List;

import java.util.Map;

import java.util.Set;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

 

import org.apache.commons.lang.ArrayUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.transaction.annotation.Transactional;

 

 

import zip.ZipEntry;

import zip.ZipInputStream;

import zip.ZipOutputStream;

 

@Transactional

@Component

public class FileHandle {

 

public static void zip(File outZipPath, File zipFileRoot) {

try {

// filePath.

OutputStream os = new FileOutputStream(outZipPath);

BufferedOutputStream bos = new BufferedOutputStream(os);

ZipOutputStream zip = new ZipOutputStream(bos, "GBK");

zip(zip, zipFileRoot, "");

zip.flush();

zip.closeEntry();

zip.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

e.printStackTrace();

}

}

 

/*

* 写入文件目录与文件内容

*/

private static void zip(ZipOutputStream out, File f, String base) throws Exception {

System.out.println("Zipping  " + f.getName());

if (f.isDirectory()) {

File[] fl = f.listFiles();

// base = new String(base.getBytes(), "GBK");

out.putNextEntry(new ZipEntry(base + "/"));

base = base.length() == 0 ? "" : base + "/";

for (int i = 0; i < fl.length; i++) {

zip(out, fl[i], base + fl[i].getName());

}

} else if (!f.getName().endsWith(".zip")) {

// base = new String(base.getBytes(), "GBK");

out.putNextEntry(new ZipEntry(base));

BufferedInputStream in = new BufferedInputStream(new FileInputStream(f));

byte[] bt = new byte[1024];

int b;

while ((b = in.read(bt)) != -1) {

out.write(bt, 0, b);

}

in.close();

}

}

 

@Transactional

public void unzip(String zipFileName) throws Exception {

ZipInputStream in = null;

try {

in = new ZipInputStream(new FileInputStream(zipFileName), "gbk");

ZipEntry z;

int totalCount = 0;

while ((z = in.getNextEntry()) != null) {

if (z.isDirectory()) {

String name = z.getName();

if (StringUtils.isNotBlank(name)) {

System.out.println("目录:" + name);

int index = name.lastIndexOf("/");

String currentName = null;

String parentName = null;

if (index != -1) {

currentName = name.substring(index + 1, name.length());

parentName = name.substring(0, index);

} else {

currentName = name;

}

 

currentName = currentName.substring(0, name.length() - 1); //去掉斜线

System.out.println("输出目录名称:"+currentName);

}

} else {

String name = z.getName();

System.out.println("文件名:" + name);

if (StringUtils.isNotBlank(name)) {

String currentName = null;

String parentName = null;

int index = name.lastIndexOf("/");

if (index != -1) {

currentName = name.substring(index + 1, name.length());

parentName = name.substring(0, index);

} else {

currentName = name;

}

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String b;

int row = 0;

while ((b = br.readLine()) != null) {

totalCount++;

row++;

System.out.println("读出文件内容:"+b);  //打出读取的内容

 

}

System.out.println("读取文件 " + currentName + " 结束");

}

}

}

} catch (Exception e) {

throw e;

} finally {

in.close();

File tempFile = new File(zipFileName);

tempFile.delete();

tempFile.deleteOnExit();

}

 

}

 

public static void main(String[] args) {

try {

// OutputStream os = new FileOutputStream(new File("D:/中文new.zip"));

// BufferedOutputStream bos = new BufferedOutputStream(os);

// ZipOutputStream zip = new ZipOutputStream(bos, "GBK");

// zip(zip, new File("D://中文"), "");

// zip.flush();

// zip.closeEntry();

// zip.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}


需要用到的修改后的zip包,见附件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值