java zip字符编码,java 文件压缩解压编码问题

该代码示例展示了如何使用Apache Commons Compress库处理ZIP压缩文件的解压,尤其针对可能出现的乱码问题。通过创建ZipArchiveInputStream读取ZIP文件,遍历每个条目,创建目录或文件,并确保文件名正确解码。同时,为了解决文件名冲突,将解压后的文件名替换为UUID加上原始扩展名,保证了文件的唯一性。
摘要由CSDN通过智能技术生成

zip压缩文件解压乱码问题,请使用commons.compress 的jar处理

样例代码如下:

public static List> getUnZip(File srcFile, File destDir) throws IOException {

int BUFFER_SIZE = 2048;

ZipArchiveInputStream is = null;

List> lst_unzip = new ArrayList>();

try {

is = new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(srcFile), BUFFER_SIZE));

ZipArchiveEntry entry = null;

while ((entry = is.getNextZipEntry()) != null) {

if (entry.isDirectory()) {

File directory = new File(destDir, entry.getName());

directory.mkdirs();

} else {

Map unfile = new HashMap();

unfile.put("src", "");

unfile.put("yname", "");

String subffix = entry.getName();

String fname = destDir + subffix;

OutputStream os = null;

try {

unfile.put("yname", entry.getName());

os = new BufferedOutputStream(new FileOutputStream(new File(fname)), BUFFER_SIZE);

IOUtils.copy(is, os);

} finally {

IOUtils.closeQuietly(os);

}

subffix = fname;

if (subffix.indexOf(".") > 0) {

subffix = subffix.substring(subffix.lastIndexOf(".") + 1);

}

String nfname = destDir + UUID.randomUUID().toString() + "."+ subffix;

File f = new File(fname);

File toFile = new File(nfname);

FileUtils.copyFile(f,toFile);

if(f.exists())

{

f.delete();

}

unfile.put("src", toFile.getName());

unfile.put("yname", entry.getName());

lst_unzip.add(unfile);

}

}

}finally{

IOUtils.closeQuietly(is);

}

return lst_unzip;

}

public static List> getUnZip(String srcFile, String destDir) throws IOException {

File zip = new File(srcFile);

if (!zip.exists()) {

return new ArrayList>();

}

File dir = new File(destDir);

if ((!dir.exists()) || (!dir.isDirectory())) {

dir.delete();

dir.mkdirs();

}

if (!destDir.endsWith("//") && !destDir.endsWith("/")) {

destDir += "//";

}

return getUnZip(new File(srcFile), new File(destDir));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值