1. import java.io.BufferedInputStream; 
  2. import java.io.BufferedOutputStream; 
  3. import java.io.BufferedReader; 
  4. import java.io.File; 
  5. import java.io.FileInputStream; 
  6. import java.io.FileOutputStream; 
  7. import java.io.IOException; 
  8. import java.io.InputStream; 
  9. import java.io.InputStreamReader; 
  10. import java.util.Enumeration; 
  11.  
  12. import org.apache.log4j.Logger; 
  13. import org.apache.tools.zip.ZipEntry; 
  14. import org.apache.tools.zip.ZipFile; 
  15. import org.apache.tools.zip.ZipOutputStream; 
  16.  
  17. import com.captech.lang.ErrorType; 
  18. import com.captech.ws.rs.WebApplicationException; 
  19.  
  20. public class CommonUtils { 
  21.      
  22.     protected static Logger logger = Logger.getLogger(CommonUtils.class); 
  23.      
  24.     public static long getStringIpToLong(String ip) { 
  25.         ip = ip.trim(); 
  26.         String[] ips = ip.split("[.]"); 
  27.         long num = 16777216L * Long.parseLong(ips[0]) + 65536L 
  28.                 * Long.parseLong(ips[1]) + 256 * Long.parseLong(ips[2]) 
  29.                 + Long.parseLong(ips[3]); 
  30.         return num; 
  31.     } 
  32.      
  33.     public static String longToIP(long longIp) {    
  34.         StringBuffer sb = new StringBuffer("");    
  35.         sb.append(String.valueOf((longIp >>> 24)));    
  36.         sb.append(".");    
  37.         sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16));    
  38.         sb.append(".");    
  39.         sb.append(String.valueOf((longIp & 0x0000FFFF) >>> 8));    
  40.         sb.append(".");    
  41.         sb.append(String.valueOf((longIp & 0x000000FF)));    
  42.         return sb.toString();    
  43.     } 
  44.      
  45.     public static File zipFiles (String filePath, String zipname){ 
  46.         FileOutputStream fos=null
  47.         ZipOutputStream zipOut=null
  48.         File fileList = new File(filePath); 
  49.         if(fileList.exists() && fileList.isDirectory()){ 
  50.             File[] files=fileList.listFiles(); 
  51.             try { 
  52.                  File out = new File(zipname); 
  53.                  if(!out.exists()){ 
  54.                       out.createNewFile(); 
  55.                  } 
  56.                  fos = new FileOutputStream(out);//创建文件输出流(低级流) 
  57.                  zipOut = new ZipOutputStream(fos);//创建zip文件输出流 
  58.                  zipOut.setEncoding("gbk"); 
  59.                  int i = 0
  60.                  for (i = 0; i < files.length; i++) { 
  61.                       writeFile(files[i], zipOut, ""); 
  62.                  } 
  63.                  zipOut.close(); 
  64.                  return out; 
  65.              }catch(IOException e){ 
  66.                  throw createAndLogException(ErrorType.FILE_WRITE_ERROR, e); 
  67.              } 
  68.         }else
  69.                  throw createAndLogException(ErrorType.FILE_OPEN_ERROR, new Exception("file Not exist")); 
  70.         } 
  71.     } 
  72.      
  73.     public static void writeFile(File file, ZipOutputStream zipOut, String base){ 
  74.         try { 
  75.             if(file.isDirectory()){ 
  76.                 File[] files = file.listFiles(); 
  77.                 base = base + file.getName() + File.separator; 
  78.                 zipOut.putNextEntry(new ZipEntry(base)); 
  79.                 for (int i = 0; i < files.length; i++) { 
  80.                     writeFile(files[i], zipOut, base); 
  81.                 } 
  82.             }else
  83.                 base = base + file.getName(); 
  84.                 BufferedReader in=new BufferedReader(new InputStreamReader(new FileInputStream(file),"ISO8859_1"));   
  85.                 ZipEntry ze = new ZipEntry(base); 
  86.                 zipOut.putNextEntry(ze); 
  87.                 int c = 0
  88.                 while ((c = in.read()) != -1) { 
  89.                     zipOut.write(c); 
  90.                 } 
  91.                 in.close(); 
  92.             } 
  93.         } catch(Exception e){ 
  94.              throw createAndLogException(ErrorType.FILE_WRITE_ERROR, e); 
  95.         } 
  96.     } 
  97.    
  98.   //删除文件夹的接口 
  99.     public static boolean deleteDirectory(File file){ 
  100.         if( file.exists() ) {    
  101.               File[] files = file.listFiles();    
  102.               for(int i=0; i<files.length; i++) {    
  103.                  if(files[i].isDirectory()) {    
  104.                    deleteDirectory(files[i]);    
  105.                  }    
  106.                  else {    
  107.                    files[i].delete();    
  108.                  }    
  109.            }    
  110.         }    
  111.         return( file.delete()); 
  112.     } 
  113.    
  114.   //复制文件的接口 
  115.     public  static void copyFile(File sourceFile,File targetFile) {  
  116.       // 新建文件输入流并对它进行缓冲  
  117.       try { 
  118.             if(!targetFile.getParentFile().exists()){ 
  119.                 targetFile.getParentFile().mkdir(); 
  120.             } 
  121.             if(!targetFile.exists()){ 
  122.                 targetFile.createNewFile(); 
  123.             } 
  124.                 FileInputStream input = new FileInputStream(sourceFile);  
  125.                 BufferedInputStream inBuff=new BufferedInputStream(input);  
  126.      
  127.                 // 新建文件输出流并对它进行缓冲  
  128.                 FileOutputStream output = new FileOutputStream(targetFile);  
  129.                 BufferedOutputStream outBuff=new BufferedOutputStream(output);  
  130.                   
  131.                 // 缓冲数组  
  132.                 byte[] b = new byte[1024 * 5];  
  133.                 int len;  
  134.                 while ((len =inBuff.read(b)) != -1) {  
  135.                     outBuff.write(b, 0, len);  
  136.                 }  
  137.                 // 刷新此缓冲的输出流  
  138.                 outBuff.flush();  
  139.                   
  140.                 //关闭流  
  141.                 inBuff.close();  
  142.                 outBuff.close();  
  143.                 output.close();  
  144.                 input.close(); 
  145.             } catch (Exception e) { 
  146.                 throw createAndLogException(ErrorType.FILE_WRITE_ERROR, e); 
  147.         }  
  148.     } 
  149.      
  150.     @SuppressWarnings("unchecked"
  151.     public static void unzipFile(String unzipfile, String outputDirectory){ 
  152.         try { 
  153.             ZipFile zipFile = new ZipFile(unzipfile,"GBK"); 
  154.             ZipEntry entry; 
  155.             // 创建文件夹 
  156.             Enumeration<ZipEntry> e = zipFile.getEntries(); 
  157.             while (e.hasMoreElements()) { 
  158.                 entry = e.nextElement(); 
  159.                 String name = entry.getName(); 
  160.                 if (!name.endsWith(File.separator)) { 
  161.                     File f = new File(outputDirectory + File.separator + entry.getName()); 
  162.                     if (!f.getParentFile().exists()) { 
  163.                         f.getParentFile().mkdirs(); 
  164.                     } 
  165.                     InputStream in = zipFile.getInputStream(entry); 
  166.                     FileOutputStream out = new FileOutputStream(f); 
  167.                     int c; 
  168.                     byte[] by = new byte[1024]; 
  169.                     while ((c = in.read(by)) != -1) { 
  170.                         out.write(by, 0, c); 
  171.                     } 
  172.                     out.close(); 
  173.                     in.close(); 
  174.                 } 
  175.             } 
  176.             zipFile.close(); 
  177.         } catch (IOException e) { 
  178.             createAndLogException(ErrorType.FILE_OPEN_ERROR, new Throwable("unzip file error")); 
  179.         } 
  180.     } 

用的apache 的ant工具类解决中文问题


maven依赖的配置:

<dependency> <groupId>org.apache.ant</groupId> <artifactId>ant</artifactId> <version>1.7.0</version> </dependency>