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();
}
}
查看全文
相关阅读:
java里如何实现对数组中的元素反转[4, 1, 8, 7, 3, 8, 2]变成 [2, 8, 3, 7, 8, 1, 4]
牛客网Java刷题知识点之插入排序(直接插入排序和希尔排序)、选择排序(直接选择排序和堆排序)、冒泡排序、快速排序、归并排序和基数排序(博主推荐)
[转]ASP.NET Web API对OData的支持
[转]Work With Odata in Web API: Create Your First Odata Service
[转]如何在 .Net Framework 4.0 项目上使用 OData?
[转]Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)
[转]使用WCF 4.0 构建 REST Service
[转]构建基于WCF Restful Service的服务
[转]asp.net5中使用NLog进行日志记录
[转]浅谈 .NET Framework 与 .NET Core 的区别与联系
原文地址:https://www.cnblogs.com/pricks/p/1600054.html
最新文章
Android 在已有工程中实现微信图片压缩
Android仿微信高效压缩图片(libjpeg)
上周热点回顾(10.10-10.16)团队
又被微信封,见识腾讯的威风团队
[故障公告]受阿里云部分ECS服务器故障影响,目前无法上传图片与文件团队
领略微信的威风:“已停止访问该网页”团队
上周热点回顾(10.3-10.9)团队
.NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息团队
上周热点回顾(9.26-10.2)团队
.NET跨平台之旅:将QPS 100左右的ASP.NET Core站点部署到Linux服务器上团队
热门文章
上周热点回顾(9.19-9.25)团队
上周热点回顾(9.12-9.18)团队
牛客网Java刷题知识点之什么是单例模式?解决了什么问题?饿汉式单例(开发时常用)、懒汉式单例(面试时常用)、单例设计模式的内存图解
牛客网Java刷题知识点之代码块(局部代码快、构造代码块、静态代码块)
牛客网Java刷题知识点之父类中的私有内容,子类是否具备? 子类不可直接,但可间接访问父类中的私有内容?
牛客网Java刷题知识点之构造函数可以调用一般函数,但是一般函数不可以直接调用构造函数
牛客网Java刷题知识点之构造函数与set方法、与类名同名的一般方法、构造函数中有return语句
牛客网Java刷题知识点之构造函数是什么、一般函数和构造函数什么区别呢、构造函数的重载、构造函数的内存图解
牛客网Java刷题知识点之基本数据类型参数传递和引用数据类型参数传递图解
牛客网Java刷题知识点之匿名对象
Copyright © 2011-2022 走看看