import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 java.util.zip.CRC32;
import org.apache.tools.zip.ZipFile;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.resbase.util.FileBean;
import org.resbase.util.FileSecurity;
/**
* 类名:ZipTool <p>
* 类描述:对文件进行打包或对zip包进行解包<p>
* 编写者 :luoc<p>
* 编写日期 :2005-10-3<p>
* 主要public成员变量:<p>
* 主要public方法: <p>
**/
public class ZipTool
{
private static Logger log = Logger.getLogger(ZipTool.class);
/**
* 方法名称:zip<p>
* 方法功能:对指定路径srcPath进行打包,打成zip包放入zipPath中<p>
* 参数说明:@param zipPath 打包后的结果包
* 参数说明:@param srcPath 需要打包的文件夹
* 参数说明:@throws Exception <p>
* 返回:void <p>
* 作者:luoc
* 日期:2005-10-3
**/
public static void zip(String zipPath,String srcPath)
throws Exception
{
List fileList = getSubFiles(new File(srcPath));
// 压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
zipPath));
ZipEntry ze = null;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(getAbsFileName(srcPath, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
// 设置CRC校验
ze.setCrc(0);
CRC32 crc = new CRC32();
crc.reset();
// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
InputStream is = new BufferedInputStream(new FileInputStream(f));
while ((readLen = is.read(buf, 0, 1024)) != -1) {
zos.write(buf, 0, readLen);
crc.update(buf, 0, readLen);
}
ze.setCrc(crc.getValue());
is.close();
}
zos.close();
}
public static void zipDecode(String zipPath,String srcPath)除xml外所有文件都解密
throws Exception
{
List fileList = getSubFiles(new File(srcPath));
// 压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
zipPath));
ZipEntry ze = null;
boolean isXML=false;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
isXML=false;
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(getAbsFileName(srcPath, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
// 设置CRC校验
ze.setCrc(0);
CRC32 crc = new CRC32();
crc.reset();
// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
if (FileBean.getFileExt(f.getName()).equalsIgnoreCase("xml"))
isXML=true;
InputStream is = new BufferedInputStream(new FileInputStream(f));
int j=0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
if (!isXML){
if (j==0){
buf=FileSecurity.EncodeBuff(buf);
}
}
zos.write(buf, 0, readLen);
crc.update(buf, 0, readLen);
j++;
}
ze.setCrc(crc.getValue());
is.close();
}
zos.close();
}
public static void zipDecode(String zipPath,String srcPath,String filePath)//除filePath外所有文件都不加密
throws Exception
{
List fileList = getSubFiles(new File(srcPath));
// 压缩文件名
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
zipPath));
ZipEntry ze = null;
boolean isXML=false;
byte[] buf = new byte[1024];
int readLen = 0;
for (int i = 0; i < fileList.size(); i++) {
File f = (File) fileList.get(i);
isXML=true;
// 创建一个ZipEntry,并设置Name和其它的一些属性
ze = new ZipEntry(getAbsFileName(srcPath, f));
ze.setSize(f.length());
ze.setTime(f.lastModified());
// 设置CRC校验
ze.setCrc(0);
CRC32 crc = new CRC32();
crc.reset();
// 将ZipEntry加到zos中,再写入实际的文件内容
zos.putNextEntry(ze);
if ((f.getPath()).equalsIgnoreCase(filePath))
isXML=false;
InputStream is = new BufferedInputStream(new FileInputStream(f));
int j=0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
if (!isXML){
if (j==0){
buf=FileSecurity.EncodeBuff(buf);
}
}
zos.write(buf, 0, readLen);
crc.update(buf, 0, readLen);
j++;
}
ze.setCrc(crc.getValue());
is.close();
}
zos.close();
}
/**
* 方法名称:unzip<p>
* 方法功能:对指定的zip文件进行解包<p>
* 参数说明:@param desPath 解包到文件夹
* 参数说明:@param args 要进行解包的zip文件<p>
* 返回:void <p>
* 作者:luoc
* 日期:2005-10-3
**/
public static String unzip(String desPath,String args)
{
if (!(new File(args)).exists())
{
log.debug("解压失败:文件没有找到!!");
return "解压失败:文件没有找到!!";
}
try
{
ZipFile zfile = new ZipFile(args);
Enumeration zList = zfile.getEntries();
byte[] buf = new byte[16384];
while (zList.hasMoreElements()) {
// 从ZipFile中得到一个ZipEntry
ZipEntry ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
log.debug("Dir: " + ze.getName() + " skipped..");
continue;
}
log.debug("Extracting: " + ze.getName() + "/t"
+ ze.getSize() + "/t" + ze.getCompressedSize());
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(desPath, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
log.debug("Extracted: " + ze.getName());
}
}catch (Exception e)
{
e.printStackTrace();
return "解压失败:"+e.getMessage();
}
return "";
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param baseDir
* 指定根目录
* @param absFileName
* 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
public static File getRealFileName(String baseDir, String absFileName) {
String[] dirs = absFileName.split("/");
//Regexp.split("/", absFileName);
// System.out.println(dirs.length);
File ret = new File(baseDir);
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
ret = new File(ret, dirs[i]);
}
}
if (!ret.exists()) {
ret.mkdirs();
}
ret = new File(ret, dirs[dirs.length - 1]);
return ret;
}
/**
* 给定根目录,返回另一个文件名的相对路径,用于zip文件中的路径.
*
* @param baseDir
* java.lang.String 根目录
* @param realFileName
* java.io.File 实际的文件名
* @return 相对文件名
*/
public static String getAbsFileName(String baseDir, File realFileName) {
File real = realFileName;
File base = new File(baseDir);
String ret = real.getName();
while (true) {
real = real.getParentFile();
if (real == null)
break;
if (real.equals(base))
break;
else {
ret = real.getName() + "/" + ret;
}
}
return ret;
}
/**
* 取得指定目录下的所有文件列表,包括子目录.
*
* @param baseDir
* File 指定的目录
* @return 包含java.io.File的List
*/
public static List getSubFiles(File baseDir) {
List ret = new ArrayList();
// File base=new File(baseDir);
if (baseDir.isDirectory()){
File[] tmp = baseDir.listFiles();
for (int i = 0; i < tmp.length; i++) {
if (tmp[i].isFile()) {
ret.add(tmp[i]);
}
if (tmp[i].isDirectory()) {
ret.addAll(getSubFiles(tmp[i]));
}
}
}else{
ret.add(baseDir);
}
return ret;
}
public static void main(String[] args) {
try {
zip("c://李海青.zip", "c://李海青.xls");
} catch (Exception e) {
} // end try
}
}