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();
}
}
查看全文
相关阅读:
dedecms织梦自定义表单发送到邮箱-用163邮箱发送邮件
DedeCMS实现自定义表单提交后发送指定QQ邮箱法
什么是授权码,它又是如何设置?
telnet配置和telnet用法
linux利用sendmail发送邮件的方法
SSH会话连接超时问题
dede 提交表单 发送邮件
a 标签中 写页面刷新代码
织梦dede在首页调用留言本
PHP通过Thrift操作Hbase
原文地址:https://www.cnblogs.com/pricks/p/1600051.html
最新文章
CPU的段寄存器
mmap内存映射
linux 内存地址空间管理 mm_struct
mm_struct简要解析
线程属性总结 线程的api属性
线程栈溢出与线程属性
进程管理—进程描述符(task_struct)
进程—内存描述符(mm_struct)
ABI and compiler
ABI and ISA
热门文章
存储器的分类
缓存机制与局部性原理
局部性原理与高速缓存
共享内存简介
IA32的三种地址
内存映射机制
Linux内存寻址之分页机制
Linux内存寻址之分段机制
内存映射
dedecms后台系统基本参数标题
Copyright © 2011-2022 走看看