zoukankan
html css js c++ java
Java解压缩Zip 文件
package
com.test;
import
java.io.BufferedInputStream;
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.InputStream;
import
java.util.Enumeration;
import
java.util.zip.ZipEntry;
import
java.util.zip.ZipFile;
import
java.util.zip.ZipOutputStream;
public
class
TestZip
{
/** */
/**
* zip压缩功能测试. 将d:\\temp\\zipout目录下的所有文件连同子目录压缩到d:\\temp\\out.zip.
*
*
@throws
Exception
*/
@SuppressWarnings(
"
unchecked
"
)
public
void
CreateZip(String zipUrl, String zipName)
throws
Exception
{
//
压缩baseDir下所有文件,包括子目录
String baseDir
=
zipUrl;
List fileList
=
getSubFiles(
new
File(baseDir));
//
压缩文件名
ZipOutputStream zos
=
new
ZipOutputStream(
new
FileOutputStream(zipName));
ZipEntry ze
=
null
;
byte
[] buf
=
new
byte
[
1024
];
int
readLen
=
0
;
for
(
int
i
=
0
; i
<
fileList.size(); i
++
)
{
File f
=
(File) fileList.get(i);
System.out.print(
"
Adding:
"
+
f.getPath()
+
f.getName());
//
创建一个ZipEntry,并设置Name和其它的一些属性
ze
=
new
ZipEntry(getAbsFileName(baseDir, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
//
将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
InputStream is
=
new
BufferedInputStream(
new
FileInputStream(f));
while
((readLen
=
is.read(buf,
0
,
1024
))
!=
-
1
)
{
zos.write(buf,
0
, readLen);
}
is.close();
System.out.println(
"
done
"
);
}
zos.close();
}
/** */
/**
* 取得指定目录下的所有文件列表,包括子目录.
*
*
@param
baseDir
* File 指定的目录
*
@return
包含java.io.File的List
*/
@SuppressWarnings(
"
unchecked
"
)
private
List getSubFiles(File baseDir)
{
List ret
=
new
ArrayList();
//
File base=new File(baseDir);
File[] tmp
=
baseDir.listFiles();
for
(
int
i
=
0
; i
<
tmp.length; i
++
)
{
if
(tmp[i].isFile())
{
ret.add(tmp[i]);
}
if
(tmp[i].isDirectory())
{
ret.addAll(getSubFiles(tmp[i]));
}
}
return
ret;
}
/** */
/**
* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.
*
*
@param
baseDir
* java.lang.String 根目录
*
@param
realFileName
* java.io.File 实际的文件名
*
@return
相对文件名
*/
private
String getAbsFileName(String baseDir, File realFileName)
{
File real
=
realFileName;
File base
=
new
File(baseDir);
String ret
=
real.getName();
while
(
true
)
{
real
=
real.getParentFile();
if
(real
==
null
)
break
;
if
(real.equals(base))
break
;
else
{
ret
=
real.getName()
+
"
/
"
+
ret;
}
}
return
ret;
}
public
static
void
main(String[] args)
{
TestZip.zipFile();
//
TestZip.unzip(url, dest);
}
/** */
/**
* 解压缩zip文件
*/
@SuppressWarnings(
"
unchecked
"
)
public
static
void
unzip(String zipFile, String dest)
throws
Exception
{
//
String ctxUrl = ServletActionContext.getRequest().getRealPath("/upload");
//
String url = ctxUrl + "\\" + fileName;
//
String dest = ctxUrl;
//
zipFile = url;
ZipFile zip
=
new
ZipFile(zipFile);
Enumeration
<
ZipEntry
>
en
=
(Enumeration)zip.entries();
ZipEntry entry
=
null
;
byte
[] buffer
=
new
byte
[
1024
];
int
length
=
-
1
;
InputStream input
=
null
;
BufferedOutputStream bos
=
null
;
File file
=
null
;
while
(en.hasMoreElements())
{
entry
=
(ZipEntry) en.nextElement();
if
(entry.isDirectory())
{
file
=
new
File(dest, entry.getName());
if
(
!
file.exists())
{
file.mkdir();
}
continue
;
}
input
=
zip.getInputStream(entry);
file
=
new
File(dest, 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();
}
}
查看全文
相关阅读:
delphi reintroduce作用
终止TTask.Run启动的线程
ChinaCock界面控件介绍-TCCYearMonthSelector
EChart 后台生成图片思路
Maven的仓库和settings.xml配置文件
JDK13 安装处理
jdk API8、9英文、中文在线文档及CHM下载:
springmvc 学习基础
Spring 框架——利用HandlerExceptionResolver实现全局异常捕获
ApplicationListener详解
原文地址:https://www.cnblogs.com/pricks/p/1600051.html
最新文章
为知笔记发布博客到博客园
好用的油猴脚本
QQ邮箱:日程管理,多平台同步
字体识别
python合成语音
提取pdf文档表格
读取ppt
向word文档里增加标题,分页,图片
读取word段落和文字块
在word中读取表格和写入表格
热门文章
绘制五星红旗
openpyxl对excel实现字母与数字之间的转换
python中openpyxl修改excel中字体及读取字体
10.3.1 iOS启动画面横屏是怎么回事?
解决Detected problems with API compatibility...
android 8.0 intent安装apk失败屏幕闪过
ChinaCock让Android App应用不锁屏
Delphi 10.3.1拍照遇到的问题
Delphi 10.3.1 TNetHttpClient在多线程中存在的问题及解决方法。
Delphi 10.3.1来了
Copyright © 2011-2022 走看看