/** * Copyright 1996-2014 FoxBPM ORG. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * @author kenshin */ package org.foxbpm.engine.impl.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.foxbpm.engine.exception.FoxBPMException; /** * 文件操作工具类 * * @author Administrator * */ public class FileUtil { public static void unZip(String zipfile, String destDir) { byte b[] = new byte[1024]; OutputStream outputStream = null; InputStream inputStream = null; ZipFile zipFile = null; int length; try { DeleteFolder(destDir); zipFile = new ZipFile(zipfile); Enumeration<? extends ZipEntry> enumeration = zipFile.entries(); ZipEntry zipEntry = null; while (enumeration.hasMoreElements()) { zipEntry = enumeration.nextElement(); File loadFile = new File(new StringBuffer(destDir).append(File.separator) .append(zipEntry.getName()).toString()); if (zipEntry.isDirectory()) { loadFile.mkdirs(); } else { File parentFile = loadFile.getParentFile(); if (!parentFile.exists()) { parentFile.mkdirs(); } outputStream = new FileOutputStream(loadFile); inputStream = zipFile.getInputStream(zipEntry); while ((length = inputStream.read(b)) > 0) { outputStream.write(b, 0, length); } if (outputStream != null) { outputStream.close(); } } } } catch (IOException e) { throw new FoxBPMException("解压文件:" + zipfile + "失败!", e); } finally { try { if (outputStream != null) { outputStream.close(); } if (inputStream != null) { inputStream.close(); } if (zipFile != null) { zipFile.close(); } } catch (Exception e2) { throw new FoxBPMException("关闭流失败!", e2); } } } /** * 根据路径删除指定的目录或文件,无论存在与否 * * @param sPath * 要删除的目录或文件 * @return 删除成功返回 true,否则返回 false。 */ public static boolean DeleteFolder(String sPath) { File file = new File(sPath); // 判断目录或文件是否存在 if (!file.exists()) { // 不存在返回 false return true; } else { // 判断是否为文件 if (file.isFile()) { // 为文件时调用删除文件方法 return deleteFile(sPath); } else { // 为目录时调用删除目录方法 return deleteDirectory(sPath); } } } /** * 删除单个文件 * * @param sPath * 被删除文件的文件名 * @return 单个文件删除成功返回true,否则返回false */ public static boolean deleteFile(String sPath) { boolean flag = false; File file = new File(sPath); // 路径为文件且不为空则进行删除 if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag; } /** * 删除目录(文件夹)以及目录下的文件 * * @param sPath * 被删除目录的文件路径 * @return 目录删除成功返回true,否则返回false */ public static boolean deleteDirectory(String sPath) { // 如果sPath不以文件分隔符结尾,自动添加文件分隔符 if (!sPath.endsWith(File.separator)) { sPath = sPath + File.separator; } File dirFile = new File(sPath); // 如果dir对应的文件不存在,或者不是一个目录,则退出 if (!dirFile.exists() || !dirFile.isDirectory()) { return false; } boolean flag = true; // 删除文件夹下的所有文件(包括子目录) File[] files = dirFile.listFiles(); for (int i = 0; i < files.length; i++) { // 删除子文件 if (files[i].isFile()) { flag = deleteFile(files[i].getAbsolutePath()); if (!flag) break; } // 删除子目录 else { flag = deleteDirectory(files[i].getAbsolutePath()); if (!flag) break; } } if (!flag) return false; // 删除当前目录 if (dirFile.delete()) { return true; } else { return false; } } }