package util; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.zip.Adler32; import java.util.zip.CheckedOutputStream; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; /** * 文件相关操作(http://blog.csdn.net/gaoyusi4964238) * @author gaoyusi * */ public class FileHelper { /** * 将文件由指定路径拷贝到指定路径 * 如果指定路径文件不存在,则自动创建该文件 * @param src 文件的源路径 * @param dis 文件复制的目的路径 */ public void copyFile(String src, String dis) throws IOException{ File srcFile=new File(src); File disFile=new File(dis); //找不到源文件 if(!srcFile.exists()){ throw new IOException("找不到源文件!"); } try{ //create distinct file createNewFile(disFile); InputStream in=new FileInputStream(srcFile); FileOutputStream out=new FileOutputStream(disFile); byte[] buffer=new byte[1024]; int byteread=0; while((byteread=in.read(buffer))!=-1){ out.write(buffer,0,byteread); } in.close(); out.close(); }catch(Exception e){ e.printStackTrace(); throw new IOException("文件复制出错!"); } } /** * 创建新文件 * 如果文件上级文件夹不存在,则先创建文件的上级文件夹 * @param file * @throws Exception */ private boolean createNewFile(File file) throws IOException{ File parentFile=file.getParentFile(); if(!parentFile.exists()){ createDir(parentFile); } return file.createNewFile(); } /** * 根据文件路径创建该文件 * 如果路径已经创建,返回true,否则返回false * @param filePath * @throws Exception */ public boolean createNewFile(String filePath) throws IOException{ File file=new File(filePath); return createNewFile(file); } /** * 根据路径对象对应的File对象,生成该路径 * 如果路径已经创建,返回true * @param dir * @throws Exception */ public boolean createDir(File dir) throws IOException{ return dir.mkdirs(); } /** * 根据路径名称字符串生成路径 * 如果路径已经存在,返回true * @param dir * @return * @throws Exception */ public boolean createDir(String dir) throws IOException{ File file =new File(dir); return createDir(file); } /** * 根据文件名称删除文件,如果文件不存在了,返回true * @param fileName * @return * @throws Exception */ public boolean deleteFile(String fileName) throws IOException{ throw new IOException("未实现"); } /** * 根据目录路径删除目录,如果目录不存在了,返回true * @param dir * @return * @throws Exception */ public boolean deleteFolder(String dir) throws IOException{ throw new IOException("未实现"); } /** * 中文条件下文件(夹)压缩 * @throws IOException */ public void zipCompress(String src,String des) throws IOException{ ZipOutputStream out=null; try { CheckedOutputStream cusm= new CheckedOutputStream(new FileOutputStream(des),new Adler32()); out=new ZipOutputStream(new BufferedOutputStream(cusm)); fileZip(new File(src),out,""); }finally{ if(out!=null){ out.close(); } } } private void fileZip(File file, ZipOutputStream out, String base) throws IOException{ if(file.isFile()){ if(base.length()>0){ out.putNextEntry(new ZipEntry(base)); }else{ out.putNextEntry(new ZipEntry(file.getName())); } BufferedReader in=new BufferedReader( new InputStreamReader(new FileInputStream(file),"ISO8859_1")); int c; while((c=in.read())!=-1){ out.write(c); } in.close(); }else if(file.isDirectory()){ File[] subFiles=file.listFiles(); out.putNextEntry(new ZipEntry(base+File.separator)); base=base.length()!=0?base+File.separator:""; for(File subFile:subFiles){ fileZip(subFile,out,base+subFile.getName()); } } } }