情瘦
路人甲
路人甲
  • 注册日期2004-02-23
  • 发帖数234
  • QQ
  • 铜币682枚
  • 威望0点
  • 贡献值0点
  • 银元0个
阅读:2258回复:1

[分享]利用java来压缩文件夹/文件

楼主#
更多 发布于:2007-04-04 13:57
import   java.io.*;     <BR>//import   java.util.zip.*;  <a>\\JDK</A>自带的zip包不支持中文压缩<BR>import  org.apache.tools.zip.*;   //ant开发的zip包,支持中文压缩<BR>    <BR>    <BR>  public   class   ZipFunc   {     <BR>   private   static   ZipOutputStream   zos;<BR>       <BR>   public   static   void   main(String[]   args)   <BR>   {     <BR>    ZipFunc zip = new ZipFunc();<BR>       <BR>    String souPath = "E:\\download\\问题.txt";<BR>    String desPath = "E:\\download";     <BR>      <BR>   try   {     <BR>     zip.makeZip(souPath,desPath);     <BR>    }catch   (Exception   e)   {     <BR>     System.err.println(e);     <BR>    }     <BR>   }     <BR>       <BR>   public boolean makeZip(String souPath,String desPath) throws IOException, FileNotFoundException     <BR>   {     <BR>    <BR>    <BR>    File file = new File(souPath);<BR>    if(!file.exists()){<BR>     return false;<BR>    }<BR>     <BR>    String folderPath="";<BR>    String[] splt = file.toString().split("<A>\\\\</A>");<BR>    folderPath=desPath + "\\" +splt[splt.length-1];  //获得最后一级路径的名称,作为zip文件名称<BR>    System.out.println(folderPath);<BR>    if (file.isFile())<BR>    {<BR>     folderPath=folderPath.substring(0,(folderPath.length()-4));<BR>    }<BR>    <BR>    try<BR>    {<BR>     zos = new ZipOutputStream(new FileOutputStream(folderPath + ".zip"));<BR>    } catch(Exception e) {<BR>     System.out.println(e.toString());<BR>     return false;<BR>    }<BR>    boolean mSuccess=false;          <BR>    if(recurseFiles(file))<BR>    {<BR>     mSuccess=true;<BR>    }        <BR>    zos.close();<BR>    return mSuccess;     <BR>   }     <BR>        <BR>   public boolean recurseFiles(File file) throws IOException, FileNotFoundException     <BR>   {     <BR>    boolean success=false;<BR>    if   (file.isDirectory()) {     <BR>        String[]   fileNames   =   file.list();     <BR>     if (fileNames != null) {     <BR>      for (int i=0; i<fileNames.length; i++)   <BR>      {     <BR>       recurseFiles(new File(file, fileNames));<BR>            <BR>      }     <BR>     }<BR>     success=true;     <BR>    }        <BR>    else   {     <BR>     byte[] buf = new byte[1024];     <BR>     int len=0;     <BR>     String fileName="";<BR>     String[] splt = file.toString().split("<A>\\\\</A>");  //获得不带路径的文件名<BR>     fileName=splt[splt.length-1];<BR>     //System.out.println(folderPath);<BR>     try{     <BR>      ZipEntry zipEntry = new ZipEntry(fileName);     <BR>         <BR>      FileInputStream fin = new FileInputStream(file);     <BR>      BufferedInputStream in = new BufferedInputStream(fin);    <BR>      zos.putNextEntry(zipEntry);         <BR>      while ((len = in.read(buf)) >= 0) {     <BR>       zos.write(buf, 0, len);     <BR>      } <BR>      in.close();<BR>      success=true;<BR>     }catch (Exception e)  {<BR>      System.out.println(e.toString());<BR>      success=false;<BR>      <BR>     }  <BR>          <BR>     zos.closeEntry(); <BR>         <BR>    }<BR>    System.out.println(success);<BR>    return success;     <BR>   }<BR>   <BR>     <BR>  }     <BR>
喜欢0 评分0
whmwxhanshan123
路人甲
路人甲
  • 注册日期2006-06-17
  • 发帖数3108
  • QQ
  • 铜币6445枚
  • 威望0点
  • 贡献值0点
  • 银元0个
1楼#
发布于:2007-04-05 21:02
<img src="images/post/smile/dvbbs/em02.gif" />
举报 回复(0) 喜欢(0)     评分
游客

返回顶部