import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
public class Zip {
public static void main(String args[]) throws IOException {
String sourceFilePath = "F:\\fund_bill_20161120.zip"; //zip文件存放位置
String saveRootDirectory = "F://test/"; //解压后存放位置
File file = new File(sourceFilePath);
Zip.zipFileRead(file, saveRootDirectory);
}
/**
*
* @Description: TODO(读取Zip信息,获得zip中所有的目录文件信息)
* @param设定文件
* @return void 返回类型
* @throws
*/
public static List<String> zipFileRead(File file, String saveRootDirectory) {
List<String> list = new ArrayList<String>();
try {
// 获得zip信息
ZipFile zipFile = new ZipFile(file,"GBK");
@SuppressWarnings("unchecked")
Enumeration<ZipArchiveEntry> enu = (Enumeration<ZipArchiveEntry>) zipFile.getEntries();
while (enu.hasMoreElements()) {
ZipArchiveEntry zipElement = (ZipArchiveEntry) enu.nextElement();
InputStream read = zipFile.getInputStream(zipElement);
String fileName = zipElement.getName();
System.out.println(fileName);
list.add(fileName);
if (fileName != null && fileName.indexOf(".") != -1) {// 是否为文件
unZipFile(zipElement, read, saveRootDirectory);
}
}
return list;
} catch (Exception e) {
e.printStackTrace();
}
return list;
} /**
*
* @Description: TODO(找到文件并读取解压到指定目录)
* @param 设定文件
* @return void 返回类型
* @throws
*/
public static void unZipFile(ZipArchiveEntry ze, InputStream read, String saveRootDirectory) throws FileNotFoundException, IOException {
// 如果只读取图片,自行判断就OK.
String fileName = ze.getName(); // 判断文件是否符合要求或者是指定的某一类型
// if (fileName.equals("WebRoot/WEB-INF/web.xml")) {
// 指定要解压出来的文件格式(这些格式可抽取放置在集合或String数组通过参数传递进来,方法更通用)
File file = new File(saveRootDirectory + fileName);
if (!file.exists()) {
File rootDirectoryFile = new File(file.getParent());
// 创建目录
if (!rootDirectoryFile.exists()) {
boolean ifSuccess = rootDirectoryFile.mkdirs();
if (ifSuccess) {
System.out.println("文件夹创建成功!");
} else {
System.out.println("文件创建失败!");
}
}
// 创建文件
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
// 写入文件
BufferedOutputStream write = new BufferedOutputStream( new FileOutputStream(file));
int cha = 0;
while ((cha = read.read()) != -1) {
write.write(cha);
}
// 要注意IO流关闭的先后顺序
write.flush();
write.close();
read.close();
// }
// }
}
}

最新文章

  1. Sql Server系列:Transact-SQL变量
  2. python学习之路——基础篇(3)模块(续)
  3. ANDROID调用webservice带soapheader验证
  4. 你知道哪些linux命令,能把文件上传到远程linux服务器
  5. 项目、SVN clean的一些事
  6. Nested Loop,Sort Merge Join,Hash Join
  7. django中使用json.dumps处理数据时,在前台遇到字符转义的问题
  8. Android 显示YUV编码格式
  9. Java中Timer和TimerTask的使用
  10. webpack - tree shaking
  11. json 帮助工具
  12. python 发送附件至邮箱
  13. js判断对象为空 JSON.stringify(obj)
  14. MyBatis《2》
  15. Java深入 - MyBatis的经常用法
  16. freemarker处理空值
  17. 【Python】排列组合itertools &amp; 集合set
  18. (NO.00001)iOS游戏SpeedBoy Lite成形记(十九)
  19. Linux-误删apt-get以及把aptitude换回
  20. python中的__metaclass__

热门文章

  1. django源码阅读
  2. [转]Libev源码分析 -- 整体设计
  3. Definition of:payload
  4. C#.Net获取Mac等PC信息
  5. NuGet的本地服务器安装与Package的发布
  6. IOS7学习之路九(ios7自定义UIAlertView)
  7. IOS学习之路十(仿人人滑动菜单Slide-out Sidebar Menu)
  8. mongodb学习(翻译1)
  9. touch事件分发
  10. 搜索广告与广告网络Demand技术-搜索广告