最好的办法是在你的代码中调用ant来完成,具体方法如下:
以下是一个示例,这个示例,将f:studyTest1目录打成f:studymytest.jar包。package mytest;import org.apache.tools.ant.*;import org.apache.tools.ant.taskdefs.*;import org.apache.tools.ant.types.*;import java.io.File;public class TestJar {public TestJar() {}public static void main(String[] args) {TestWar testWar1 = new TestWar();testWar1.jar();}public static void jar(){final class Jarer extends Jar {public Jarer() {project = new Project();project.init();taskType = "jar";taskName = "jar";target = new Target();}}Jarer jar = new Jarer();jar.setDestFile(new File("f:\Study\mytest.jar"));jar.setBasedir(new File("f:\Study\Test1"));jar.execute();}}在这个示例中,应用了Ant任务中的Jar类,从这个类继承了一个新的类Jarer,Jarer类的功能就是创建Project和Target对象。在以后的使用过程中,可以直接创建Jarer类的对象,然后设置Jarer的参数,最后调用Jarer的execute()方法。附注:以上示例程序的编译、运行,仅需要将Ant的ant.jar包增加到classpath路径中。

解决方案 »

  1.   

    我自己写了一个
    现在有个问题,如果文件名是中文了,压缩后文件名就成乱码了,如何解决?package test;import java.io.*;
    import java.util.zip.*;;public class TendZip 
    {
    final int BUFFERSIZE = 2048;
    byte buf[] = new byte[BUFFERSIZE];

    FileInputStream filein = null;
    BufferedInputStream bufin = null;

    FileOutputStream fileout = null;
    BufferedOutputStream bufout = null;
    ZipOutputStream zipout = null;

    public TendZip() {
    super();
    // TODO Auto-generated constructor stub
    }

    public boolean zip(String srcpath, String deszip)
    {
    boolean result = true;
    try{
    fileout = new FileOutputStream(deszip);
    bufout = new BufferedOutputStream(fileout);
    zipout = new ZipOutputStream(bufout);

    File file = new File(srcpath);
    ZipEntry entry = null;
    if(!file.isDirectory()){//file
    entry = new ZipEntry(file.getName());
    result = zipfile(file, entry);
    }else{//folder
    result = zipfolder(file, null);
    }

    zipout.close();
    }catch(Exception e){
    e.printStackTrace();
    result = false;
    }finally{

    }
    return result;
    }//zip

    boolean zipfile(File file, ZipEntry zipentry)
    {
    boolean result = true;
    try{
    filein = new FileInputStream(file);
    bufin = new BufferedInputStream(filein);

    zipout.putNextEntry(zipentry);
    int count = 0;
    while((count = bufin.read(buf, 0, BUFFERSIZE)) != -1){
    zipout.write(buf, 0, count);
    }

    bufin.close();
    }catch(Exception e){
    e.printStackTrace();
    result = false;
    }finally{

    }
    return result;
    }//zipfile /*****
     * 
     * @param folder the folder would be zipped
     * @param fatherzipfolder the folder hierarchy in the zip, null if it's root folder
     * @return
     */
    boolean zipfolder(File folder, String fatherzipfolder)
    {
    boolean result = true;
    try{
    ZipEntry entry = null;

    //add the folder to zip
    String zipfolder = null;
    if(fatherzipfolder == null){
    zipfolder = folder.getName() + "/";
    }else{
    zipfolder = fatherzipfolder + folder.getName() + "/";
    }
    entry = new ZipEntry(zipfolder);
    zipout.putNextEntry(entry);

    //recursive zip the folder
    File filelist[] = folder.listFiles();
    for(int i = 0; i < filelist.length; i++){
    if(!filelist[i].isDirectory()){
    String zipfile = zipfolder + filelist[i].getName();
    entry = new ZipEntry(zipfile);
    zipfile(filelist[i], entry);
    }else{
    if(!zipfolder(filelist[i], zipfolder))
    return false;
    }
    }
    }catch(Exception e){
    e.printStackTrace();
    result = false;
    }finally{

    }
    return result;
    }//zipfolder

    }
      

  2.   

    中文问题,不要用sun的zip包,用apache的org.apache.tools.zip;
    ant.jar里有org.apache.tools.zip这个包。
      

  3.   

    用java.util.zip压缩中文是不会出现乱码的
    你用它压缩,再用它解压
    里面的内容是正常的:-)
      

  4.   

    sgdb(神天月晓) ( ) 
    你的方法可以,谢谢
    一会儿结贴