📄 executefile.java
字号:
package com.huawei.icd30.agt.util.zip;
import java.io.*;
import java.util.Vector;
import java.util.zip.*;
import com.huawei.icd30.common.systemconfig.*;
import com.huawei.icd30.common.log.*;
//import com.huawei.icd30.kbs.searchengine.*;
/**
* 该类提供对文件或者文件的查找、删除、压缩、解压缩的方法
* @author ICD3.0业务 B组-外包-胡云志
* @version 1.0
*/
public class ExecuteFile
{
public Vector vc = null;
public ExecuteFile()
{
this.vc=new Vector();
}
/**
* 删除一个文件或者文件夹
* @param fileDir 要删除的文件或者文件夹的路径字符串
* @return true成功;false失败
*/
public static boolean deleteFile(String fileDir)
{
//backInfo删除操作成功以否的标志
boolean backInfo = true;
//将字符串转换为一个文件类file
File file = new File(fileDir);
//当file存在并且是目录
// SearchUtil su=SearchUtil.getInstance();
// su.RMConnect(" ",0);
if(file.exists()&&file.isDirectory())
{
//取得目录下一级节点的文件或者文件夹
String a[]=file.list();
int aLength=file.list().length;
//file不是空目录
if(file.list().length!=0)
{
for(int i=0;i<=a.length-1;i++)
{
backInfo = deleteFile(fileDir+"/"+a[i]);
//删除搜索引擎的的索引文件
if(backInfo==false)
{
// su.RMClose();
return false;
}
}
}
//file是一个空目录
else
{
backInfo = file.delete();
if(backInfo==true)
{
//删除搜索引擎中频道配置表的一个频道*******
// su.delChannelID(1);
}
}
}
//file为文件
if(file.isFile())
{
backInfo = file.delete();
if(backInfo==false)
{
// su.RMClose();
return false;
}
else
{
// su.deleteFile(file.getName());
}
}
if(file.exists())
{
backInfo = file.delete();
}
// su.RMClose();
return backInfo;
}
/**
* 将指定的压缩文件解压缩到指定的目录下面
* @param upZipFileName 需要解压缩的文件的绝对路径名
* @param zipFileName 解压缩的目的路径
* return true 解压缩成功,false 解压缩失败
*/
public boolean fileUnZip(String upZipFileName,String zipFileName)
{
boolean returnFlag = false;
try
{
// SearchUtil su = SearchUtil.getInstance();
ZipFile zf = new ZipFile(upZipFileName);
java.util.Enumeration es = zf.entries();
while(es.hasMoreElements())
{
ZipEntry ze=(ZipEntry)es.nextElement();
if(ze.isDirectory())
{
File file=new File(zipFileName,ze.getName());
//在搜索引擎的频道配置表中增加一个频道ze.getName
// su.addChannelID(ze.getName()," ");
}
else
{
InputStream in = zf.getInputStream(ze);
File file1 = new File(zipFileName,ze.getName());
File fileParent = file1.getParentFile();
fileParent.mkdirs();
//在搜索引擎的频道配置表中增加一个频道ze.getName
// su.addChannelID(ze.getName()," ");
FileOutputStream fout=null;
try
{
fout=new FileOutputStream(file1);
}
catch(Exception e)
{
e.printStackTrace();
SystemLog.error("压缩解压缩 失败 系统异常 异常信息:"+
e.getMessage());
}
int c;
while((c = in.read())!=-1)
{
fout.write(c);
}
fout.close();
in.close();
}
}
zf.close();
// su.RMClose();
returnFlag = true;
}
catch(Exception e)
{
//e.printStackTrace();
SystemLog.error("文件解压缩 系统异常 异常信息:"+e.getMessage());
}
return returnFlag;
}
/**
* 将一个文件或者文件夹压缩为一个指定的文件
* @param zippedFileName 需要压缩的文件或者的全路径
* @param zipFileName 压缩后的文件路径
* @return true 压缩成功;false 压缩失败
*/
public boolean Zip(String zippedFileName,String zipFileName)
{
boolean returnFlag=false;
try
{
File file = (new File(zippedFileName)).getParentFile();
int fileLength = file.getPath().length();
FileZip fz = FileZip.getInstance(new File(zipFileName));
//查找所有需要压缩的文件
findFileInDir(zippedFileName);
int j = vc.size()-1;
//不压缩空的文件夹
while(j>=0)
{
File file1 = (File)vc.elementAt(j);
if(file1.isDirectory())
{
vc.remove(j);
}
j--;
}
int m = vc.size();
String args[] = new String[m];
for(int i=0;i<m;i++)
{
args[i] =((File)vc.elementAt(i)).getPath().substring(fileLength);
}
if(args.length>0)
{
fz.addFiles(file,args);
}
returnFlag = true;
}
catch(Exception e)
{
e.printStackTrace();
SystemLog.error("文件压缩 系统异常 异常信息:"+e.getMessage());
}
return returnFlag;
}
/**
* <p>查找指定目录下的所有叶子节点的文件
* @param fileName 需要查找的文件或者文件夹的名称
*/
public void findFileInDir(String fileName)
{
File file = new File(fileName);
//file存在
if(file.exists())
{
//file是一个文件
if(file.isFile())
{
vc.addElement(file);
}
//file是目录
else
{
String[] a = file.list();
//在file目录下有文件
if(a.length>0)
{
for(int i=0;i<=a.length-1;i++)
{
findFileInDir(fileName+
System.getProperty("file.separator")+a[i]);
}
}
//file是一个空目录
else
{
vc.addElement(file);
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -