package com.util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
/**
* 解压缩文件
*
*/
public class Zip extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 5142373267185568029L;
/**
* 压缩文件为zip格式
*
* @paramzipFile 压缩文件后的文件名
* @paramsrcPathName 待压缩文件
* @throws IOException
* 文件不存在异常
*/
public static void zipFile(String srcPathName) throws IOException {
File file = new File(srcPathName);
if (!file.exists())
throw new RuntimeException(srcPathName + "not exist!");
//System.out.println(srcPathName);
FileOutputStream fileOutputStream = new FileOutputStream(srcPathName+".zip");
CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
ZipOutputStream out = new ZipOutputStream(cos);
String basedir = "";
compress(file, out, basedir);
out.flush();
out.close();
}
private static void compress(File file, ZipOutputStream out, String basedir) throws IOException {
if (file.isDirectory()) {
compressDirectory(file, out, basedir);
} else {
compressFile(file, out, basedir);
}
}
private static void compressDirectory(File dir, ZipOutputStream out, String basedir) throws IOException {
if (!dir.exists())
return;
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
compress(files[i], out, basedir + dir.getName() + "/");
}
}
private static void compressFile(File file, ZipOutputStream out, String basedir) throws IOException {
if (!file.exists()) {
return;
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(basedir + file.getName());
out.putNextEntry(entry);
int len = 0;
byte data[] = new byte[1024];
while ((len = bis.read(data)) != -1) {
out.write(data, 0, len);
}
bis.close();
}
private static void createDir(String path) {
File dir = new File(path);
if (dir.exists() == false)
dir.mkdir();
}
private static String getSuffixName(String name) {
return name.substring(0, name.lastIndexOf("."));
}
/**
* 解压缩文件
* @throws IOException
* 文件读写异常
*/
public static void unZip(String zipFilePath) throws IOException {
File file = new File(zipFilePath+".zip");
ZipFile zipFile = new ZipFile(file);
File unzipFile = new File(zipFilePath);
if (unzipFile.exists())
unzipFile.delete();
unzipFile.mkdirs();
Enumeration zipEnum = zipFile.entries();
InputStream input = null;
OutputStream output = null;
ZipEntry entry = null;
while (zipEnum.hasMoreElements()) {
entry = (ZipEntry) zipEnum.nextElement();
String entryName = new String(entry.getName());
String names[] = entryName.split("\\/");
int length = names.length;
String path = unzipFile.getAbsolutePath();
for (int v = 0; v < length; v++) {
if (v < length - 1) {
path += "/" + names[v] + "/";
createDir(path);
} else {
if (entryName.endsWith("/"))
createDir(unzipFile.getAbsolutePath() + "/" + entryName);
else {
input = zipFile.getInputStream(entry);
output = new FileOutputStream(new File(unzipFile.getAbsolutePath() + "/" + entryName));
byte[] buffer = new byte[1024 * 8];
int readLen = 0;
while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1)
output.write(buffer, 0, readLen);
input.close();
output.flush();
output.close();
}
}
}
}
}
public static void main(String[] args) {
Zip zip=new Zip();
//String path=zip.getClassPath();//System.getProperty("user.dir");
try {
String path=zip.findServerPath();
//System.out.println(path);
//压缩
//zip.zipFile(path+"testzip");//路径+文件名
// 解压
//zip.unZip(path+"testzip");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 获取服务器端的webapps路径
* @return
*/
public String findServerPath() throws Exception{
String path=this.getServletContext().getRealPath("/");
return path;
}
/**
* 得到本类物理路径所在文件夹
* @return
*/
private String getClassPath()throws Exception{
String strClassName = getClass().getName();
String strPackageName = "";
if(getClass().getPackage() != null) {
strPackageName = getClass().getPackage().getName();
}
String strClassFileName = "";
if(!"".equals(strPackageName)){
strClassFileName = strClassName.substring(strPackageName.length() + 1,strClassName.length());
}
else {
strClassFileName = strClassName;
}
URL url = null;
url = getClass().getResource(strClassFileName + ".class");
String strURL = url.toString();
//String middleString = System.getProperty("file.separator"); // 取得操作系统路径分割符
strURL = strURL.substring(strURL.indexOf( "/" ) + 1,strURL.lastIndexOf( "/" ));
return strURL;
}
}