http://www.chinajavaworld.net/doc/wangyou/lcj/beansoftZip.zip

解决方案 »

  1.   

    我有[email protected]给我发邮件好了
      

  2.   

    以下这个程式简单示範压缩的方式: 
    <%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*,java.util.zip.*"%>
    <%
       FileOutputStream fos =new FileOutputStream(request.getRealPath("/")+"myzip.zip");
       //先指定压缩档的位置及档名,建立一个FileOutputStream
       
       ZipOutputStream zos=new ZipOutputStream(fos);
       //建立ZipOutputStream并将fos传入
       
       zos.setLevel(9);
       //设定压缩率,可选0-9
       
       String rootstr="c:\\test";
       //设定要压缩的资料夹
       
       File[] myfiles=(new File(rootstr)).listFiles();
       ZipEntry ze=null;
       //每个档案要压缩,都要透过ZipEntry来处理   FileInputStream fis=null;
       byte[] ch = new byte[256];
       for(int i=0;i<myfiles.length;i++)
       {
          if (myfiles[i].isFile())
          {
             ze=new ZipEntry(myfiles[i].getName());
             //以上是只以档案的名字当Entry,也可以自己再加上额外的路径
             //例如 ze=new ZipEntry("test\\"+myfiles[i].getName());
             //如此压缩档内的每个档案都会加test这个路径         fis=new FileInputStream(myfiles[i]);
             zos.putNextEntry(ze);
             //将ZipEntry透过ZipOutputStream的putNextEntry的方式送进去处理         int len;
             while((len=fis.read(ch))!=-1)
               zos.write(ch,0,len);
             //开始将原始档案读进ZipOutputStream         fis.close();
             zos.closeEntry();
          }
       }
       zos.close();
       fos.close();
    %>
    <script language="javascript">
    alert("档案已压缩完毕!");
    location.replace("myzip.zip");
    </script>
      

  3.   

    在压缩与解压缩(1)只压缩了某个资料夹下的档案,对於子资料夹内的档案却没有处理;若要将子资料夹内的所有档案都要压缩,则需要用到递迴抓出所有的档案! 
    <%@ page contentType="text/html;charset=MS950" import="java.util.*,java.io.*,java.util.zip.*"%>
    <%!
      private ArrayList AllFiles=new ArrayList();
      private void searchFiles(String dirstr)
      {
         File tempdir=new File(dirstr);
         if (tempdir.exists())
         {
           if (tempdir.isDirectory())
           {
             File[] tempfiles=tempdir.listFiles();
             for(int i=0;i<tempfiles.length;i++)
             {
                if (tempfiles[i].isDirectory())
                    searchFiles(tempfiles[i].getPath());
                else
                    AllFiles.add(tempfiles[i]);
             }
           }
           else
           {
              AllFiles.add(tempdir);
           }
         }
      }
    %>
    <%
       FileOutputStream fos =new FileOutputStream(request.getRealPath("/")+"myzip.zip");
       //先指定压缩档的位置及档名,建立一个FileOutputStream
       ZipOutputStream zos=new ZipOutputStream(fos);
       //建立ZipOutputStream并将fos传入
       zos.setLevel(9);
       //设定压缩率,可选0-9
       String rootstr="e:\\five";
       //指定要压缩的路径或档案
       searchFiles(rootstr);
       //利用递迴搜寻该路径下所有的File
       Object[] myobject=AllFiles.toArray();
       //取得该路径下的所有档案
       ZipEntry ze=null;
       FileInputStream fis=null;
       byte[] ch = new byte[256];
       //开始逐一压缩档案
       for (int i=0;i<myobject.length;i++)
       {
         File myfile=(File)myobject[i];
         if (myfile.isFile())
         {
           ze=new ZipEntry(myfile.getPath().substring((rootstr+myfile.separator).length()));
           ze.setTime(myfile.lastModified());
           //设定压缩档的时间,如果不设定,会以目前时间当预设值
           fis=new FileInputStream(myfile);
           zos.putNextEntry(ze);
           int len;
           while((len=fis.read(ch))!=-1)
              zos.write(ch,0,len);
           //开始将原始档案读进ZipOutputStream
           fis.close();
           zos.closeEntry();
           out.println(myfile.getName()+"已压缩完毕!<br>");
         }
       }
       zos.close();
       fos.close();
    %>
    <script language="javascript">
    alert("档案已压缩完毕!");
    location.replace("myzip.zip");
    </script>
    以上两个就过了。你看看吧!
      

  4.   

    还有需要注意的是目前用java.util.zip包用于压缩解压,你的压缩包中不能出现中文目录或是中文文件名!否则无法解压!不知谁有解决方案!
      

  5.   

    这是我把一个String放到zip中的一段程序,
    ZipEntry zipentry = new ZipEntry("cor.csv"); //设置压缩文件的名字
    Hashtable FileParm = new Hashtable();
    String filepath = "e:\\";
    String filename = "cor" + dpt_sale_id + date + ".zip";
    FileParm.put("FILEPATH", filepath);
    FileParm.put("FILENAME", filename);
    File f = new File(filepath + filename);
    byte b[] = ss.getBytes();
    ZipOutputStream zipouts =
    new ZipOutputStream(new FileOutputStream(f));
    zipouts.putNextEntry(zipentry);
    DataOutputStream os = new DataOutputStream(zipouts);
    ByteArrayInputStream bais = new ByteArrayInputStream(b);
    BufferedInputStream bis = new BufferedInputStream(bais);
    BufferedOutputStream bos = new BufferedOutputStream(os);
    byte[] data = new byte[1];
    while (bis.read(data, 0, 1) != -1) {
    bos.write(data);
    }
    bos.flush();
    bos.close();