package com.file.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.log4j.Logger;
/**
* 文件处理公共类
* @author guchunchao
*
*/
public class FileUtil {
private static Logger logger = Logger.getLogger(FileUtil.class);
/**
* 通配符匹配
* @param pattern 通配符模式
* @param str 待匹配的字符串
* @return 匹配成功则返回true,否则返回false
*/
public static boolean wildcardMatch(String pattern, String str) {
int patternLength = pattern.length();
int strLength = str.length();
int strIndex = 0;
char ch;
for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {
ch = pattern.charAt(patternIndex);
if (ch == '*') {
//通配符星号*表示可以匹配任意多个字符
while (strIndex < strLength) {
if (wildcardMatch(pattern.substring(patternIndex + 1), str.substring(strIndex))) {
return true;
}
strIndex++;
}
} else if (ch == '?') {
//通配符问号?表示匹配任意一个字符
strIndex++;
if (strIndex > strLength) {
//表示str中已经没有字符匹配?了。
return false;
}
} else {
if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {
return false;
}
strIndex++;
}
}
return (strIndex == strLength);
}
/**
* @param basePath 查询路径
* @param pattern 匹配模板
* @return
*/
public static List<File> getFiles(String basePath , String pattern) {
List<File> fileList = new ArrayList<File>();
File baseDir = new File(basePath);
if(baseDir.isDirectory()) {
File[] listArray = baseDir.listFiles();
for(File file : listArray) {
if(wildcardMatch(pattern,file.getName())) {
logger.info("loop local cache file's name is [ " + file.getName() + " ] ");
fileList.add(file);
}
}
}
return fileList;
}
/**
* 删除单个文件
* @param file
*/
public static void deleteFile(File file) {
if (file != null && file.exists() && file.isFile())
file.delete();
}
/**
* 删除文件集合
* @param files
*/
public static void deleteFile(List<File> files) {
if(files != null && files.size() > 0)
for(File file : files)
deleteFile(file);
}
/**
* 封装zip文件
* @param excelFileList
* @param zipfile
*/
private static void zipFiles(List<File> excelFileList, File zipfile) {
byte[] buf = new byte[1024];
try {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// out.setEncoding("gbk");// 防止文件名乱码
for (int i = 0; i < excelFileList.size(); i++) {
FileInputStream in = new FileInputStream(excelFileList.get(i));
out.putNextEntry(new ZipEntry(excelFileList.get(i).getName()));
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
out.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
/**
* 封装zip文件
* @param excelFileList
* @param zipfile
*/
public static void zipFiles(List<File> excelFileList, File zipfile,OutputStream zipOut) {
try {
zipFiles(excelFileList, zipfile);
FileInputStream inStream = new FileInputStream(zipfile);
byte[] buf = new byte[1024];
int readLength;
while (((readLength = inStream.read(buf)) != -1)) {
zipOut.write(buf, 0, readLength);
}
inStream.close();
zipOut.flush();
zipOut.close();
} catch (IOException e) {
logger.info("将zip压缩文件IO流赋予outstream时出错");
logger.error(e.getMessage(), e);
}
}
}