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基础入门-arraylist存储开销
java基础入门-iterator迭代器与集合下标的使用
java基础入门-泛型(1)-为什么需要使用泛型?
vue路由懒加载
js防抖和节流
vue 生命周期函数详解
createElement 函数
vue中Runtime-Compiler和Runtime-only的区别
箭头函数以及this指向问题
webpackES6语法转ES5语法
原文地址:https://www.cnblogs.com/pricks/p/1600054.html
最新文章
前端总结资料库
十分钟玩转 jQuery、实例大全
H5 以及 CSS3
Delphi 单元
【转】实现Ribbon风格的窗体
Delphi的打开文件对话框-TOpenDialog
Delphi数据类型转换
Delphi的保存文件对话框-TsaveDialog
Delphi中 弹出框的用法
cxgrid 非编辑状态下复制当前列的值 真折腾人
热门文章
delphi XML简单处理
sql数据库中如何根据身份证号判断性别
delphi 获取网卡信息(支持多网卡)
数据结构与算法-函数的渐近增长
java基础入门-建立简单的ServerSocket
数据结构与算法-为什么要使用算法
java基础入门-对象流与序列化
java基础入门-ZipOutputStream打包下载
数据机构与算法-数据结构的一些基本概念
java基础入门-多线程同步浅析-以银行转账为例子
Copyright © 2011-2022 走看看