zoukankan
html css js c++ java
java zip递归压缩解压代码
ZIP压缩类
import
java.io.BufferedInputStream;
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
java.util.zip.ZipEntry;
import
java.util.zip.ZipOutputStream;
public
class
ZipCompress
{
/** */
/**
*/
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
throws
IOException
{
compress(
"
D:/tomcat-5.5.20
"
,
"
d:/test/testZip.zip
"
);
}
/** */
/**
*/
/** */
/**
* 递归压缩文件
*
@param
source 源路径,可以是文件,也可以目录
*
@param
destinct 目标路径,压缩文件名
*
@throws
IOException
*/
private
static
void
compress(String source,String destinct)
throws
IOException
{
List fileList
=
loadFilename(
new
File(source));
ZipOutputStream zos
=
new
ZipOutputStream(
new
FileOutputStream(
new
File(destinct)));
byte
[] buffere
=
new
byte
[
8192
];
int
length;
BufferedInputStream bis;
for
(
int
i
=
0
;i
<
fileList.size();i
++
)
{
File file
=
(File) fileList.get(i);
zos.putNextEntry(
new
ZipEntry(getEntryName(source,file)));
bis
=
new
BufferedInputStream(
new
FileInputStream(file));
while
(
true
)
{
length
=
bis.read(buffere);
if
(length
==-
1
)
break
;
zos.write(buffere,
0
,length);
}
bis.close();
zos.closeEntry();
}
zos.close();
}
/** */
/**
*/
/** */
/**
* 递归获得该文件下所有文件名(不包括目录名)
*
@param
file
*
@return
*/
private
static
List loadFilename(File file)
{
List filenameList
=
new
ArrayList();
if
(file.isFile())
{
filenameList.add(file);
}
if
(file.isDirectory())
{
for
(File f:file.listFiles())
{
filenameList.addAll(loadFilename(f));
}
}
return
filenameList;
}
/** */
/**
*/
/** */
/**
* 获得zip entry 字符串
*
@param
base
*
@param
file
*
@return
*/
private
static
String getEntryName(String base,File file)
{
File baseFile
=
new
File(base);
String filename
=
file.getPath();
//
int index=filename.lastIndexOf(baseFile.getName());
if
(baseFile.getParentFile().getParentFile()
==
null
)
return
filename.substring(baseFile.getParent().length());
return
filename.substring(baseFile.getParent().length()
+
1
);
}
}
ZIP解压类
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.InputStream;
import
java.util.Enumeration;
import
java.util.zip.ZipEntry;
import
java.util.zip.ZipFile;
public
class
ZipDecompression
{
/** */
/**
*/
/** */
/**
*
@param
args
*
@throws
IOException
*/
public
static
void
main(String[] args)
throws
IOException
{
decompression(
"
d:/test/testZip.zip
"
,
"
d:/test/un
"
);
}
/** */
/**
*/
/** */
/**
* 解压ZIP文件
*
@param
zipFile 要解压的ZIP文件路径
*
@param
destination 解压到哪里
*
@throws
IOException
*/
public
static
void
decompression(String zipFile,String destination)
throws
IOException
{
ZipFile zip
=
new
ZipFile(zipFile);
Enumeration en
=
zip.entries();
ZipEntry entry
=
null
;
byte
[] buffer
=
new
byte
[
8192
];
int
length
=-
1
;
InputStream input
=
null
;
BufferedOutputStream bos
=
null
;
File file
=
null
;
while
(en.hasMoreElements())
{
entry
=
(ZipEntry)en.nextElement();
if
(entry.isDirectory())
{
System.out.println(
"
directory
"
);
continue
;
}
input
=
zip.getInputStream(entry);
file
=
new
File(destination,entry.getName());
if
(
!
file.getParentFile().exists())
{
file.getParentFile().mkdirs();
}
bos
=
new
BufferedOutputStream(
new
FileOutputStream(file));
while
(
true
)
{
length
=
input.read(buffer);
if
(length
==-
1
)
break
;
bos.write(buffer,
0
,length);
}
bos.close();
input.close();
}
zip.close();
}
}
查看全文
相关阅读:
如何利用 JConsole观察分析Java程序的运行,进行排错调优
【解决】网站运行一段时间后就无法访问,重启Tomcat才能恢复
不允许一个用户使用一个以上用户名与一个服务器或共享
SVN升级到1.8后 Upgrade working copy
Windows Server 2012 R2 创建AD域
JTA 深度历险
svn merge error must be ancestrally related to,trunk merge branch报错
OutputStream-InputStream-FileOutputStream-FileInputStream-BufferedOutputStream-BufferedInputStream-四种复制方式-单层文件夹复制
SVN提交时响应很慢,我是这样解决的。
docker学习6-docker-compose容器集群编排
原文地址:https://www.cnblogs.com/pricks/p/1600054.html
最新文章
虚拟机桥接模式不能上网
成为优秀程序员的101条建议(1)
前端在阿里开始主导业务了?
推动前段发展的底层原因
2019第30周日
放下应该,让感受流动
不要把自己当成纯粹的执行者
面试中的两大法则
Java如何正确的将数值转化为ArrayList?
查理芒格思维模型清单
热门文章
如何让FireFox/chrome新打开的标签页在后台打开,而不是立即跳转过去
更改mac系统语言及其软件
Intelli系列代理部分报错:You have JVM property https.proxyHost set..
Intelli公司IDE快捷键设置
vim去除行显示;vim全部复制命令
git别名;git配置使用shell函数;git别名使用shell函数;git获取当前分支;git alias
vim的翻页、跳转到某一行功能
kafka-python的gevent模式和kafka的兼容性
django错误:Connection reset by peer
SVN合并时报错:Merge tracking not allowed with missing subtrees; try restoring these items
Copyright © 2011-2022 走看看