解压上传的zip文件流和文件
/**
* 解压上传的zip文件流
* @param stream
* @param outputDirectory
*/
public static String unzip(InputStream stream,String outputDirectory){
ZipInputStream in = null;
in = new ZipInputStream(stream);
ZipEntry z;
String outDir="";
String filePath="";
try {
while ((z=in.getNextEntry())!= null){
//System.out.println("unziping "+z.getName());
if (z.isDirectory()){
String name=z.getName();
name=name.substring(0,name.length()-1);
File f=new File(outputDirectory+File.separator+name);
f.mkdir();
//System.out.println("mkdir "+outputDirectory+File.separator+name);
}else{
outDir = outputDirectory+File.separator+UPLOAD_PATH;
/*File fdir = new File(outDir);
if (!fdir.exists()) {
fdir.mkdirs();
} */
makeDir(outDir);
//System.out.println("outDir "+outDir);
filePath = outDir+File.separator+z.getName();
//删除文件
FileUtil.deleteFile(filePath);
File f=new File(filePath);
if(!f.exists()){
f.createNewFile();
}
//处理后缀名
String fileName = z.getName();
String prefix=fileName.substring(fileName.lastIndexOf(".")+1);
//System.out.println(prefix);
if(!prefix.equalsIgnoreCase("zip")){
in.close();
return "00003";
}
FileOutputStream out=new FileOutputStream(f);
int b;
while ((b=in.read()) != -1){
out.write(b);
}
out.close();
}
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "00000";
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "00000";
}
if(StringUtils.isEmpty(filePath) ||
StringUtils.isEmpty(outDir)){
return "00001";
}
//处理zip文件
String outFileDir = outputDirectory;
String result = unZip(filePath,outFileDir);
return result;
}
public static String unZip(String fileName, String unZipDir) {
String outFileDir = unZipDir+File.separator+DATA_PATH;
//makeDir(outFileDir);
try {
// 先判断目标文件夹是否存在,如果不存在则新建,如果父目录不存在也新建
File f = new File(outFileDir);
if (!f.exists()) {
f.mkdirs();
}
File[] files = f.listFiles();
if(files == null)
return "00003";
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
boolean flag =FileUtil.delete(files[i].getAbsolutePath());
if (!flag) {
break;
}
}
}
BufferedOutputStream dest = null;
BufferedInputStream is = null;
ZipEntry entry;
ZipFile zipfile = new ZipFile(fileName);
//删除zip文件
FileUtil.deleteFile(fileName);
Enumeration e = zipfile.entries();
while (e.hasMoreElements()) {
entry = (ZipEntry) e.nextElement();
//System.out.println("Extracting: " + entry);
is = new BufferedInputStream(zipfile.getInputStream(entry));
int count;
byte data[] = new byte[BUFFER];
FileOutputStream fos = new FileOutputStream(outFileDir + File.separator
+ entry.getName());
//System.out.println("entry.getName(): " + entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
is.close();
}
//
} catch (Exception e) {
e.printStackTrace();
return "00003";
}
return "";
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String fileName = "e:/008 山东长鸿工程塑胶有限公司 山东长鸿工程塑胶有限公司-2015.zip";
String unZipDir = "e:/2/2";
ZipInputStream in = null;
try {
in = new ZipInputStream(new FileInputStream(zipFileName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
unzip(in, unZipDir);
}
解压上传的zip文件流和文件的更多相关文章
- PHP自动解压上传的rar文件
PHP自动解压上传的rar文件 浏览:383 发布日期:2015/07/20 分类:功能实现 关键字: php函数 php扩展 大家都知道php有个zip类可直接操作zip压缩文件,可是用户有时候 ...
- Java解压上传zip或rar文件,并解压遍历文件中的html的路径
1.本文只提供了一个功能的代码 public String addFreeMarker() throws Exception { HttpSession session = request.getSe ...
- Flask保存或解压上传的文件
import os import uuid import shutil import zipfile from flask import Flask, render_template, request ...
- 文件上传之——用SWF插件实现文件异步上传和头像截取
之前写过几篇文件上传,那些都不错.今天小编带领大家体会一种新的上传方法,及使用Flash插件实现文件上传. 使用Flash的好处就是可以解决浏览器兼容性问题.之前我写的一个快捷复制功能也是利用的Fla ...
- linux中解压.tgz, .tar.gz ,zip ,gz, .tar文件
转载:https://blog.csdn.net/fu6543210/article/details/7984578 将.tgz文件解压在当前目录: tar zxvf MY_NAME.tgz 将.ta ...
- Struts2文件上传(基于表单的文件上传)
•Commons-FileUpload组件 –Commons是Apache开放源代码组织的一个Java子项目,其中的FileUpload是用来处理HTTP文件上传的子项目 •Commons-Fil ...
- Linux解压.tar .tgz .tar.gz .tar.Z等文件
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ---------------------- ...
- uedit修改文件上传路劲,支持api文件接口
首先修改一个东西ueditor/ueditor.config.js serverUrl: URL + "php/controller.php" 原来 serverUrl: &quo ...
- 用户态与内核态 & 文件流与文件描述符 简介【转】
转自:https://www.cnblogs.com/Jimmy1988/p/7479856.html 用户态和内核态 程序代码的依赖和调用关系如下图所示: Lib:标准ASCI C函数,几乎所有的平 ...
随机推荐
- ORACLE 创建视图索引序列
/* 视图View 视图是从若干基本表和(或)其他视图构造出来的表 视图存放的都是查询语句,并没有真实的数据 虚表 作用 限制对数据的操作 复杂查询变简单 提供相同数据的不同显示 UNION ALL ...
- Lightoj 1166 - Old Sorting
Given an array containing a permutation of 1 to n, you have to find the minimum number of swaps to s ...
- AngularJS 指令实践指南(二)
这个系列教程的第一部分给出了AngularJS指令的基本概述,在文章的最后我们介绍了如何隔离一个指令的scope.第二部分将承接上一篇继续介绍.首先,我们会看到在使用隔离scope的情况下,如何从指令 ...
- c语言和oc对比
1)源文件对比 思考&实现1: 1)在C语言中,我们遇到不同后缀的文件有哪些? .c .o .out .h 2.基本语法对比 1)数据类型对比学习 2)变量的定义对比 3)流程控制语句对比 1 ...
- bzoj1499 [NOI2005]瑰丽华尔兹——单调队列优化DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1499 朴素DP方程很好想,以右移为例,就是 f[i][x][y]=max(f[i][x][y ...
- java.lang.NoSuchMethodError: org.springframework.web.context.ConfigurableWebApplicationContext.setId
运行spring报了这个错误,网上说是spring版本冲突,检查maven依赖,发现我依赖的是spring-core.3.0.5,但是spring-orm和spring-tx依赖了spring-bea ...
- JSON与JSONP(转载)
JSON是一种数据交换格式,JSONP是非官方跨域数据交互协议.一个是描述信息的格式,一个是信息传递双方约定的方法. 一.JSON是一种基于文本的数据交换方式,或者叫做数据描述格式. ...
- Xenocode Postbuild 2010 for .NET 使用
代码混淆工具 参考地址1:http://blog.csdn.net/yanpingsoft/article/details/7997212 参考地址2:http://www.cnblogs.com/w ...
- 洛谷P2082 区间覆盖(加强版)(珂朵莉树)
传送门 虽然是黄题而且还是一波离散就能解决的东西 然而珂朵莉树还是很好用 相当于一开始区间全为0,然后每一次区间赋值,问最后总权值 珂朵莉树搞一搞就好了 //minamoto #include< ...
- [App Store Connect帮助]八、维护您的 App(5)生成产品报告
您可以生成产品报告,详细介绍您所在机构中 App 目录的信息和设置,包括 App 内购买项目,以及 Game Center排行榜和成就的元数据. 首先您以不同类型请求产品报告,之后您会收到一封电子邮件 ...