java zip压缩文件中文乱码

1.解压文件
 public static Map<String,File> unzip(File zipFile) throws IOException {
Map<String,File> map = new Hashtable<String,File>();
org.apache.tools.zip.ZipFile zfile = new org.apache.tools.zip.ZipFile(zipFile,"GBK");
Enumeration<?> enumeration = zfile.getEntries();
InputStream is = null;
while (enumeration.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enumeration.nextElement();
if (!entry.isDirectory()) {
is = zfile.getInputStream(entry);
BufferedInputStream bis = new BufferedInputStream(is);
File temp = new File(Constant.FTP_LOCAL_DIR+""+CommonUtils.getToday()+"_upload/"+entry.getName());
if(!temp.getParentFile().exists()){
temp.getParentFile().mkdirs();
}
OutputStream bos = new FileOutputStream(temp);
IOUtils.copy(bis, bos);
bis.close();
bos.close();
is.close();
map.put(entry.getName(), temp);
}
}
return map;
}

2.压缩文件
private static File zip(List<File> fileList,String zipFileName) throws Exception{
File zipFile=null;
if (fileList.size() > 0) {
zipFile = new File(zipFileName);
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (File f : fileList) {
ZipEntry entry = new ZipEntry(f.getName());
zos.setEncoding("gbk");
zos.putNextEntry(entry);
CommonUtils.addFile(f, zos);
}
zos.close();
fos.close();
}
return zipFile;
}



	public static void addFile(File file, OutputStream zos) throws IOException {
InputStream in = new FileInputStream(file);
byte[] b = new byte[1024000];
int length;
while ((length = in.read(b)) > -1) {
zos.write(b, 0, length);
zos.flush();
}
in.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值