文件名称:java工具类:文件操作工具类.java
文件大小:9KB
文件格式:JAVA
更新时间:2011-11-30 03:25:00
文件操作工具类.java
public class FileUtil { protected static Logger log = LoggerFactory.getLogger(FileUtil.class); /** * 压缩文件 * @param inputFileName 要压缩的文件或文件夹路径,例如:c:\\a.txt,c:\\a\ * @param outputFileName 输出zip文件的路径,例如:c:\\a.zip */ public static void zip(String inputFileName, String outputFileName) throws Exception { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName)); zip(out, new File(inputFileName), ""); log.debug("压缩完成!"); out.closeEntry(); out.close(); } /** * 压缩文件 * @param out org.apache.tools.zip.ZipOutputStream * @param file 待压缩的文件 * @param base 压缩的根目录 */ private static void zip(ZipOutputStream out, File file, String base) throws Exception { if (file.isDirectory()) { File[] fl = file.listFiles(); base = base.length() == 0 ? "" : base + File.separator; for (int i = 0; i < fl.length; i++) { zip(out, fl[i], base + fl[i].getName()); } } else { out.putNextEntry(new ZipEntry(base)); log.debug("添加压缩文件:" + base); FileInputStream in = new FileInputStream(file); int b; while ((b = in.read()) != -1) { out.write(b); } in.close(); 下载完整。。。