解压上传的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);

}

posted on 2015-09-14 17:01  浅潇烟  阅读(1214)  评论(0编辑  收藏  举报

导航