注意:JDK7支持设置编码设置编码格式 zipFile,zipInputStream,zipOutputStream都增加了编码格式,如果是jdk1.6需要其他的包辅助
下面为自带jdk压缩文件夹代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
public void dozip(String srcfile, String zipfile) throws IOException {
String temp = "";
File src = new File(srcfile);
File zipFile=new File(zipfile);
//判断要压缩的文件存不存在
if (!src.exists()) {
System.err.println("要压缩的文件不存在!");
System.exit(1);
}
//如果说压缩路径不存在,则创建
if (!zipFile.getParentFile().exists()) {
zipFile.getParentFile().mkdirs();
// System.out.println("创建ok");
}
// 封装压缩的路径
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(zipfile));
//这里可以加入校验
//CheckedOutputStream cos = new CheckedOutputStream(bos,new CRC32());
//还可以设置压缩格式,默认UTF-8
Charset charset = Charset.forName("GBK");
ZipOutputStream zos = new ZipOutputStream(bos,charset);
zip(src, zos, temp);
//关闭流
zos.flush();
zos.close();
System.out.println("压缩完成!");
System.out.println("压缩文件的位置是:" + zipfile);
// System.out.println("检验和:"+cos.getChecksum().getValue());
}
private void zip(File file, ZipOutputStream zos, String temp)
throws IOException {
// 如果不加"/"将会作为文件处理,空文件夹不需要读写操作
if (file.isDirectory()) {
String str = temp + file.getName() + "/";
zos.putNextEntry(new ZipEntry(str));
File[] files = file.listFiles();
for (File file2 : files) {
zip(file2, zos, str);
}
} else {
// System.out.println("当前文件的父路径:"+temp);
ZipFile(file, zos, temp);
}
}
private void ZipFile(File srcfile, ZipOutputStream zos, String temp)
throws IOException {
// 默认的等级压缩-1
// zos.setLevel(xxx);
// 封装待压缩文件
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
srcfile));
zos.putNextEntry(new ZipEntry(temp + srcfile.getName()));
byte buf[] = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1) {
zos.write(buf, 0, len);
}
//按标准需要关闭当前条目,不写也行
zos.closeEntry();
bis.close();
}
|
下面为解压:
这里先说一下好压的解压规则:
如果自定义了其他文件夹xxx,则先创建xxx,再放入解压后的文件夹
2.好压压缩的时候,是采用GBK格式的,所以在解压的时候,为了统一,采用GBK解压另外再说一下WINRAR,因为RAR压缩是申请了专利(商业软件),所以RAR压缩算法是不公开的,但是解压算法是有的,其压缩默认也是GBK格式的;
经过测试,发现,不管压缩的时候采用UTF-8还是GBK,解压的时候用GBK都可以正确解压!(具体原因还不清楚)
本java程序是直接解压到文件夹的,默认解压到与压缩文件同路径
如果解压编码有问题,则报错:java.lang.IllegalArgumentException: MALFORMED
如果压缩文件有密码:则报错:java.util.zip.ZipException: encrypted ZIP entry not supporte
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
//方法1:
public void unZip(String zipfile) throws IOException {
//检查是否是zip文件,并判断文件是否存在
checkFileName(zipfile);
long startTime = System.currentTimeMillis();
File zfile=new File(zipfile);
//获取待解压文件的父路径
String Parent=zfile.getParent()+"/";
FileInputStream fis=new FileInputStream(zfile);
Charset charset = Charset.forName("GBK");//默认UTF-8
// CheckedInputStream cis = new CheckedInputStream(fis,new CRC32());
ZipInputStream zis = new ZipInputStream(fis,charset);// 输入源zip路径
ZipEntry entry=null;
BufferedOutputStream bos=null;
while ((entry=zis.getNextEntry())!=null) {
if (entry.isDirectory()) {
File filePath=new File(Parent+entry.getName());
//如果目录不存在,则创建
if (!filePath.exists()) {
filePath.mkdirs();
}
}else{
FileOutputStream fos=new FileOutputStream(Parent+entry.getName());
bos=new BufferedOutputStream(fos);
byte buf[] = new byte[1024];
int len;
while ((len = zis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
zis.closeEntry();
//关闭的时候会刷新
bos.close();
}
}
zis.close();
long endTime = System.currentTimeMillis();
System.out.println("解压完成!所需时间为:"+(endTime-startTime)+"ms");
// System.out.println("校验和:"+cis.getChecksum().getValue());
}
private void checkFileName(String name) {
//文件是否存在
if (!new File(name).exists()) {
System.err.println("要解压的文件不存在!");
System.exit(1);
}
// 判断是否是zip文件
int index = name.lastIndexOf(".");
String str=name.substring(index+1);
if (!"zip".equalsIgnoreCase(str)) {
System.err.println("不是zip文件,无法解压!");
System.exit(1);
}
}
|
方法2:
利用zipFile解压,方法跟ZipInputStream类似,都是连续取到Entry,然后再用Entry判断,听说zipFile内建了缓冲流,所以对于同一个文件解压多次效率比ZipInputStream高些
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
public void dozip(String zipfile) throws IOException {
checkFileName(zipfile);
long startTime = System.currentTimeMillis();
// 获取待解压文件的父路径
File zfile = new File(zipfile);
String Parent = zfile.getParent() + "/";
// 设置,默认是UTF-8
Charset charset = Charset.forName("GBK");
ZipFile zip = new ZipFile(zipfile, charset);
ZipEntry entry = null;
//封装解压后的路径
BufferedOutputStream bos=null;
//封装待解压文件路径
BufferedInputStream bis=null;
Enumeration<ZipEntry> enums = (Enumeration<ZipEntry>) zip.entries();
while (enums.hasMoreElements()) {
entry = enums.nextElement();
if (entry.isDirectory()) {
File filePath = new File(Parent + entry.getName());
// 如果目录不存在,则创建
if (!filePath.exists()) {
filePath.mkdirs();
}
}else{
bos=new BufferedOutputStream(new FileOutputStream(Parent + entry.getName()));
//获取条目流
bis =new BufferedInputStream(zip.getInputStream(entry));
byte buf[] = new byte[1024];
int len;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
bos.close();
}
}
bis.close();
zip.close();
System.out.println("解压后的路径是:"+Parent);
long endTime = System.currentTimeMillis();
System.out.println("解压成功,所需时间为:"+(endTime-startTime)+"ms");
}
private void checkFileName(String name) {
// 文件是否存在
if (!new File(name).exists()) {
System.err.println("要解压的文件不存在!");
System.exit(1);
}
// 判断是否是zip文件
int index = name.lastIndexOf(".");
String str = name.substring(index + 1);
if (!"zip".equalsIgnoreCase(str)) {
System.err.println("不是zip文件,无法解压!");
System.exit(1);
}
}
|
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/u011165335/article/details/50496619
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-05 79
-
2025-05-29 72
-
2025-05-25 67
-
2025-05-27 50
-
2025-05-29 113
热门评论

