本文实例为大家分享了java压缩解压缩工具类的具体代码,供大家参考,具体内容如下
?
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
package com.wdy.tools.utils.pressuitl;
import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.util.zip.zipentry;
import java.util.zip.zipfile;
import java.util.zip.zipinputstream;
import java.util.zip.zipoutputstream;
import org.apache.commons.logging.log;
import com.wdy.tools.utils.logutil;
/**
* 压缩/解压缩工具类(zip格式)
*
* @author wdy
* @date 2016-08-23
*/
public class pressutil {
private static final log log = logutil.getlog(pressutil. class );
public static void main(string[] args) {
// pressutil.zipmultifile("d:\\\\nwp_trans\\\\nwp_h\\\\", "d:\\\\nwp_trans\\\\nwp_h\\\\wdy.zip");
string sourcefilepath = "d:\\\\nwp_trans\\\\nwp_h\\\\" ;
string zipfilepath = "d:\\\\nwp_trans\\\\nwp_h\\\\" ;
string filename = "wdy" ;
boolean flag = pressutil.filetozip(sourcefilepath, zipfilepath, filename);
if (flag){
log.info( "文件打包成功!" );
} else {
log.info( "文件打包失败!" );
}
}
/**
* 将存放在sourcefilepath目录下的源文件,打包成filename名称的zip文件,并存放到zipfilepath路径下
* @param sourcefilepath :待压缩的文件路径
* @param zipfilepath :压缩后存放路径
* @param filename :压缩后文件的名称(不包括扩展名)
* @return
*/
@suppresswarnings ( "resource" )
public static boolean filetozip(string sourcefilepath,string zipfilepath,string filename){
boolean flag = false ;
file sourcefile = new file(sourcefilepath);
fileinputstream fis = null ;
bufferedinputstream bis = null ;
fileoutputstream fos = null ;
zipoutputstream zos = null ;
if (sourcefile.exists() == false ){
log.info( "待压缩的文件目录:" +sourcefilepath+ "不存在." );
} else {
try {
file zipfile = new file(zipfilepath + "/" + filename + ".zip" );
if (zipfile.exists()){
log.info(zipfilepath + "目录下存在名字为:" + filename + ".zip" + "打包文件." );
} else {
file[] sourcefiles = sourcefile.listfiles();
if ( null == sourcefiles || sourcefiles.length< 1 ){
log.info( "待压缩的文件目录:" + sourcefilepath + "里面不存在文件,无需压缩." );
} else {
fos = new fileoutputstream(zipfile);
zos = new zipoutputstream( new bufferedoutputstream(fos));
byte [] bufs = new byte [ 1024 * 10 ];
for ( int i= 0 ;i<sourcefiles.length;i++){
//创建zip实体,并添加进压缩包
zipentry zipentry = new zipentry(sourcefiles[i].getname());
zos.putnextentry(zipentry);
//读取待压缩的文件并写进压缩包里
fis = new fileinputstream(sourcefiles[i]);
bis = new bufferedinputstream(fis, 1024 * 10 );
int read = 0 ;
while ((read=bis.read(bufs, 0 , 1024 * 10 )) != - 1 ){
zos.write(bufs, 0 ,read);
}
}
flag = true ;
}
}
} catch (filenotfoundexception e) {
e.printstacktrace();
throw new runtimeexception(e);
} catch (ioexception e) {
e.printstacktrace();
throw new runtimeexception(e);
} finally {
//关闭流
try {
if ( null != bis) bis.close();
if ( null != zos) zos.close();
} catch (ioexception e) {
e.printstacktrace();
throw new runtimeexception(e);
}
}
}
return flag;
}
/**
* 压缩单个文件
* @param filepath 要被压缩的文件的全路径,包含文件名d:/hello.txt
* @param zippath 压缩后的全路径,包含文件名d:/hello.zip
*/
public static void ziponefile(string filepath, string zippath) {
try {
file file = new file(filepath);
file zipfile = new file(zippath);
inputstream input = new fileinputstream(file);
zipoutputstream zipout = new zipoutputstream( new fileoutputstream(zipfile));
zipout.putnextentry( new zipentry(file.getname()));
int temp = 0 ;
while ((temp = input.read()) != - 1 ) {
zipout.write(temp);
}
input.close();
zipout.close();
} catch (exception e) {
e.printstacktrace();
}
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/wdy_2099/article/details/72916282
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 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交流群
您的支持,是我们最大的动力!
热门文章
-
在ASP.NET 2.0中操作数据之十二:在GridView控件中使用TemplateField
2025-05-29 82 -
2025-05-29 18
-
2025-06-04 91
-
2025-05-29 42
-
2025-05-25 42
热门评论