package com.zip;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 解决压缩包中文乱码问题
* 如果出现找不到类的情况,请导入 项目lib目录下的ant.jar包
*
* @author JingYuWang
*
*/
public class CDGGzip {
public void gzip(String zipFile,List<File> files,String fileComment) {
byte[] buf = new byte[1024];
ZipOutputStream out = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipFile));
out.setEncoding("gbk");
out.setComment(fileComment);
for (File file : files) {
FileInputStream in = new FileInputStream(file);
out.putNextEntry(new ZipEntry(file.getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(out!= null)out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
String zipName = "d:\\wjy01.zip";//压缩包的位置
List<File> file = new ArrayList<File>(10);
file.add(new File("d:\\My Documents\\文档\\流水账\\moneylogger_201107.xls"));//要压缩的文件01
file.add(new File("d:\\My Documents\\文档\\流水账\\moneylogger_201108.xls"));//要压缩的文件02
file.add(new File("d:\\My Documents\\文档\\流水账\\moneylogger_201109.xls"));//要压缩的文件03
file.add(new File("d:\\My Documents\\文档\\流水账\\moneylogger_201110.xls"));//要压缩的文件04
try {
CDGGzip cdggzip = new CDGGzip();
cdggzip.gzip(zipName, file, "完美解决中文乱码问题");
} catch (Exception e) {
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
ZIP.rar (6个子文件)
zip
bin
com
zip
CDGGzip.class 3KB
.settings
org.eclipse.jdt.core.prefs 629B
src
com
zip
CDGGzip.java 2KB
.project 379B
.classpath 351B
lib
ant.jar 1.84MB
共 6 条
- 1
资源评论
- qq6461030332013-01-29还是有缺陷 出现了乱码
- q158611758152013-01-06还可以,有一些用
- 晨solo2013-12-05还是有缺陷 出现了乱码
- callmelyn2012-10-12不错,谢谢分享,解决了我解压zip的问题
- apple89898562014-02-11非常有用的资料文件
jingyu.wang
- 粉丝: 6
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功