package com.cms21.mobilecrm.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import com.cms21.mobilecrm.common.context.ApplicationProperties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class NewZipUtils {
private static final String zipFilterExtension = AppPropertyUtils
.getProperty(ApplicationProperties.UNZIP_FILE_FILTER);
public static void main(String[] args) {
// 把 E 盘正则表达式文件夹下的所有文件压缩到 E 盘 stu 目录下,压缩后的文件名保存为 正则表达式 .zip
// zip ("E://Java//test", "E://Java//压缩文件.zip");
// 把 E 盘 stu 目录下的正则表达式 .zip 压缩文件内的所有文件解压到 E 盘 stu 目录下面
// /unZip ( "E://Java//测试html.zip" , "E://Java" );
}
/**
*
* 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件 create date:2009- 6- 9
*
* author:Administrator
*
*
*
* @param sourceDir
*
* E:// 我的备份
*
* @param zipFile
*
* 格式: E://stu //zipFile.zip 注意:加入 zipFile 我们传入的字符串值是
*
* : "E://stu //" 或者 "E://stu "
*
* 如果 E 盘已经存在 stu 这个文件夹的话,那么就会出现 java.io.FileNotFoundException:
* E:/stu
*
* ( 拒绝访问。 ) 这个异常,所以要注意正确传参调用本函数哦
*
*
*/
public static void zip(String sourceDir, String zipFile) {
OutputStream os;
try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("压缩成功");
}
/**
*
*
*
* create date:2009- 6- 9 author:Administrator
*
*
*
* @param source
*
* @param basePath
*
* @param zos
*
* @throws IOException
*/
private static void zipFile(File source, String basePath,
ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
String pathName;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1)
+ "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
is.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** 针对于邮件模板的解压处理开始 **/
/**
*
* 解压 zip 文件,注意不能解压 rar 文件哦,只能解压 zip 文件 解压 rar 文件 会出现 java.io.IOException:
* Negative
*
* seek offset 异常 create date:2009- 6- 9 author:Administrator
*
*
*
* @param zipfile
*
* zip 文件,注意要是正宗的 zip 文件哦,不能是把 rar 的直接改为 zip 这样会出现
* java.io.IOException:
*
* Negative seek offset 异常
*
* @param destDir
*
* @throws IOException
*/
public static List<String> unZip(String zipfile, String destDir,
String projectPath) {
List<File> htmlFile = new ArrayList<File>();
List<String> htmlList = new ArrayList<String>();
File dstDiretory = new File(destDir);
if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
byte b[] = new byte[9028];
int length;
ZipFile zipFile = null;
File file = null;
try {
file = new File(zipfile);
zipFile = new ZipFile(file, "gb2312");
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + File.separator
+ zipEntry.getName());
if (zipEntry.isDirectory()) {
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
if (!isSuffix(zipEntry.getName()))
continue;
loadFile.mkdirs();
} else {
if (!isSuffix(zipEntry.getName()))
continue;
if (!loadFile.getParentFile().exists()) {
if (!isSuffix(zipEntry.getName()))
continue;
loadFile.getParentFile().mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(
loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
fileOutputStream.write(b, 0, length);
fileOutputStream.close();
inputStream.close();
}
if (isHTML(zipEntry.getName())) {
htmlList.add(dealHtml(loadFile, projectPath));
htmlFile.add(loadFile);
}
}
System.out.println(" 文件解压成功 ");
} catch (Exception e) {
file.delete();
file.getParentFile().delete();
e.printStackTrace();
} finally {
try {
zipFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
deleteHtmlFile(htmlFile);
return htmlList;
}
/** 针对于邮件模板的解压处理结束 **/
// 对html文件进行重写,目的在于将里边的img中属性为src的进行修改
public static String dealHtml(File loadFile, String projectPath) {
String lastPath = loadFile.getParent().substring(
projectPath.length() + 8);
String url = (AppPropertyUtils
.getProperty(ApplicationProperties.IMG_PATH_DOMAIN)
+ AppPropertyUtils.getProperty("img_path") + lastPath).replace(
"\\", "/"); // 得到服务器域名,并和解压文件目录叠加
String htmlOld = "";
try {
htmlOld = FileUtils.readFileToString(loadFile, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
String htmlNew = HtmlUtils.replaceImg(htmlOld, url);
return htmlNew;
}
// 删除解压后,并以处理过的html或htm文件
public static void deleteHtmlFile(List<File> loadFile) {
for (File f : loadFile) {
f.delete();
}
}
// 判断解压出来的文件是否为图片
public static boolean isPics(String filename) {
boolean flag = false;
String FileName = filename.toLowerCase();
if (FileName.endsWith(".jpg") || FileName.endsWith(".gif")
|| FileName.endsWith(".bmp") || FileName.endsWith(".png"))
flag = true;
return flag;
}
// 判断解压出来的文件是否为HTML或htm
public static boolean isHTML(String filename) {
return FilenameUtils.isExtension(filename.toLowerCase(), new String[] {
"html", "htm" });
}
// 判断解压出来的文件是否为配置文件规定的后缀
public static boolean isSuffix(String filename) {
String fileExtension[] = zipFilterExtension.split(",");
return FilenameUtils.isExtension(filename.toLowerCase(), fileExtension);
}
/** 公用解压处理方法开始 **/
/**
*
* 解压 zip 文件,注意不能解压 rar 文件哦,只能解压 zip 文件 解压 rar 文件 会出现 java.io.IOException:
* Negative
*
* seek offset 异常 create date:2009- 6- 9 author:Administrator
*
*
*
* @param zipfile
*
* zip 文件,注意要是正宗的 zip 文件哦,不能是把 rar 的直接改为 zip 这样会出现
* java.io.IOException:
*
* Negative seek offset 异常
*
* @param destDir
*
* @param extensionFilter
* 用于解压特定的文件,为null是解压所有文件
*
* @throws IOException
*/
public static List<File> publicUnZip(String zipfile, String destDir,
String[] extensionFilter) {
List<File> fileList = new ArrayList<File>();
File dstDiretory = new File(destDir);
if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
byte b[] = new byte[9028];
int length;
ZipFile zipFile = null;
File file = null;
try {
file = new File(zipfile);
zipFile = new ZipFile(file, "gb2312");
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + File.separator
+ zipEntry.getName());
if (zipEntry.isDirectory()) {
if (!isFileExtension(zipEntry.getName(), extensionFilter))
continue;
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
loadFile.mkdirs();
} else {
if (!isFileExtension(zipEntry.getName(), extensionFilter))
continue;
if (!loadFile.getParentFile().exists()) {
if (!isFileExtension(zipEntry.getName(),
extensionFilter))
continue;
loadFile.getParentFile().mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(
loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
fileOutputStream.write(b, 0, length);
fileOutputStream.close();
inputStream.close();
}
fileList.add(loadFile);
}
System.out.println(" 文件解压成功 ");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
zipFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return fileList;
}
// 判断解压出来的文件是否是特定的后缀
public static boolean isFileExtension(String filename,
String[] extensionFilter) {
return FilenameUtils.isExtension(filename.toLowerCase(),
extensionFilter);
}
/** 公用解压处理方法结束 **/
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
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.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import com.cms21.mobilecrm.common.context.ApplicationProperties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
public class NewZipUtils {
private static final String zipFilterExtension = AppPropertyUtils
.getProperty(ApplicationProperties.UNZIP_FILE_FILTER);
public static void main(String[] args) {
// 把 E 盘正则表达式文件夹下的所有文件压缩到 E 盘 stu 目录下,压缩后的文件名保存为 正则表达式 .zip
// zip ("E://Java//test", "E://Java//压缩文件.zip");
// 把 E 盘 stu 目录下的正则表达式 .zip 压缩文件内的所有文件解压到 E 盘 stu 目录下面
// /unZip ( "E://Java//测试html.zip" , "E://Java" );
}
/**
*
* 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件 create date:2009- 6- 9
*
* author:Administrator
*
*
*
* @param sourceDir
*
* E:// 我的备份
*
* @param zipFile
*
* 格式: E://stu //zipFile.zip 注意:加入 zipFile 我们传入的字符串值是
*
* : "E://stu //" 或者 "E://stu "
*
* 如果 E 盘已经存在 stu 这个文件夹的话,那么就会出现 java.io.FileNotFoundException:
* E:/stu
*
* ( 拒绝访问。 ) 这个异常,所以要注意正确传参调用本函数哦
*
*
*/
public static void zip(String sourceDir, String zipFile) {
OutputStream os;
try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile(file, basePath, zos);
zos.closeEntry();
zos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("压缩成功");
}
/**
*
*
*
* create date:2009- 6- 9 author:Administrator
*
*
*
* @param source
*
* @param basePath
*
* @param zos
*
* @throws IOException
*/
private static void zipFile(File source, String basePath,
ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
String pathName;
byte[] buf = new byte[1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1)
+ "/";
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
}
is.close();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** 针对于邮件模板的解压处理开始 **/
/**
*
* 解压 zip 文件,注意不能解压 rar 文件哦,只能解压 zip 文件 解压 rar 文件 会出现 java.io.IOException:
* Negative
*
* seek offset 异常 create date:2009- 6- 9 author:Administrator
*
*
*
* @param zipfile
*
* zip 文件,注意要是正宗的 zip 文件哦,不能是把 rar 的直接改为 zip 这样会出现
* java.io.IOException:
*
* Negative seek offset 异常
*
* @param destDir
*
* @throws IOException
*/
public static List<String> unZip(String zipfile, String destDir,
String projectPath) {
List<File> htmlFile = new ArrayList<File>();
List<String> htmlList = new ArrayList<String>();
File dstDiretory = new File(destDir);
if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
byte b[] = new byte[9028];
int length;
ZipFile zipFile = null;
File file = null;
try {
file = new File(zipfile);
zipFile = new ZipFile(file, "gb2312");
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + File.separator
+ zipEntry.getName());
if (zipEntry.isDirectory()) {
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
if (!isSuffix(zipEntry.getName()))
continue;
loadFile.mkdirs();
} else {
if (!isSuffix(zipEntry.getName()))
continue;
if (!loadFile.getParentFile().exists()) {
if (!isSuffix(zipEntry.getName()))
continue;
loadFile.getParentFile().mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(
loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
fileOutputStream.write(b, 0, length);
fileOutputStream.close();
inputStream.close();
}
if (isHTML(zipEntry.getName())) {
htmlList.add(dealHtml(loadFile, projectPath));
htmlFile.add(loadFile);
}
}
System.out.println(" 文件解压成功 ");
} catch (Exception e) {
file.delete();
file.getParentFile().delete();
e.printStackTrace();
} finally {
try {
zipFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
deleteHtmlFile(htmlFile);
return htmlList;
}
/** 针对于邮件模板的解压处理结束 **/
// 对html文件进行重写,目的在于将里边的img中属性为src的进行修改
public static String dealHtml(File loadFile, String projectPath) {
String lastPath = loadFile.getParent().substring(
projectPath.length() + 8);
String url = (AppPropertyUtils
.getProperty(ApplicationProperties.IMG_PATH_DOMAIN)
+ AppPropertyUtils.getProperty("img_path") + lastPath).replace(
"\\", "/"); // 得到服务器域名,并和解压文件目录叠加
String htmlOld = "";
try {
htmlOld = FileUtils.readFileToString(loadFile, "utf-8");
} catch (IOException e) {
e.printStackTrace();
}
String htmlNew = HtmlUtils.replaceImg(htmlOld, url);
return htmlNew;
}
// 删除解压后,并以处理过的html或htm文件
public static void deleteHtmlFile(List<File> loadFile) {
for (File f : loadFile) {
f.delete();
}
}
// 判断解压出来的文件是否为图片
public static boolean isPics(String filename) {
boolean flag = false;
String FileName = filename.toLowerCase();
if (FileName.endsWith(".jpg") || FileName.endsWith(".gif")
|| FileName.endsWith(".bmp") || FileName.endsWith(".png"))
flag = true;
return flag;
}
// 判断解压出来的文件是否为HTML或htm
public static boolean isHTML(String filename) {
return FilenameUtils.isExtension(filename.toLowerCase(), new String[] {
"html", "htm" });
}
// 判断解压出来的文件是否为配置文件规定的后缀
public static boolean isSuffix(String filename) {
String fileExtension[] = zipFilterExtension.split(",");
return FilenameUtils.isExtension(filename.toLowerCase(), fileExtension);
}
/** 公用解压处理方法开始 **/
/**
*
* 解压 zip 文件,注意不能解压 rar 文件哦,只能解压 zip 文件 解压 rar 文件 会出现 java.io.IOException:
* Negative
*
* seek offset 异常 create date:2009- 6- 9 author:Administrator
*
*
*
* @param zipfile
*
* zip 文件,注意要是正宗的 zip 文件哦,不能是把 rar 的直接改为 zip 这样会出现
* java.io.IOException:
*
* Negative seek offset 异常
*
* @param destDir
*
* @param extensionFilter
* 用于解压特定的文件,为null是解压所有文件
*
* @throws IOException
*/
public static List<File> publicUnZip(String zipfile, String destDir,
String[] extensionFilter) {
List<File> fileList = new ArrayList<File>();
File dstDiretory = new File(destDir);
if (!dstDiretory.exists()) { // 目标目录不存在时,创建该文件夹
dstDiretory.mkdirs();
}
byte b[] = new byte[9028];
int length;
ZipFile zipFile = null;
File file = null;
try {
file = new File(zipfile);
zipFile = new ZipFile(file, "gb2312");
Enumeration enumeration = zipFile.getEntries();
ZipEntry zipEntry = null;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
File loadFile = new File(destDir + File.separator
+ zipEntry.getName());
if (zipEntry.isDirectory()) {
if (!isFileExtension(zipEntry.getName(), extensionFilter))
continue;
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
loadFile.mkdirs();
} else {
if (!isFileExtension(zipEntry.getName(), extensionFilter))
continue;
if (!loadFile.getParentFile().exists()) {
if (!isFileExtension(zipEntry.getName(),
extensionFilter))
continue;
loadFile.getParentFile().mkdirs();
}
FileOutputStream fileOutputStream = new FileOutputStream(
loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
fileOutputStream.write(b, 0, length);
fileOutputStream.close();
inputStream.close();
}
fileList.add(loadFile);
}
System.out.println(" 文件解压成功 ");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
zipFile.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return fileList;
}
// 判断解压出来的文件是否是特定的后缀
public static boolean isFileExtension(String filename,
String[] extensionFilter) {
return FilenameUtils.isExtension(filename.toLowerCase(),
extensionFilter);
}
/** 公用解压处理方法结束 **/
}