压缩目录的代码,供参考:import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;public class FileZip
{
    static final char SEPARATOR = File.separatorChar;
    private int preffixLen = 0;    public void compress(OutputStream out, File file) throws IOException
    {
        preffixLen = file.getParentFile().getAbsolutePath().length() + 1;
        ZipOutputStream zos = new ZipOutputStream(out);
        addFile(zos, file);
        zos.close();
    }    private void addFile(ZipOutputStream zos, File file) throws IOException
    {
        String entryName = entryName(file);
        if (entryName.equals("") || entryName.equals("."))
            return;        System.out.println("adding " + entryName);
        ZipEntry entry = new ZipEntry(entryName);
        entry.setTime(file.lastModified());
        boolean isDirectory = file.isDirectory();
        if (isDirectory)
        {
            entry.setMethod(0);
            entry.setSize(0L);
            entry.setCrc(0L);
        }
        else
            entry.setSize(file.length());        zos.putNextEntry(entry);
        if (isDirectory)
        {
            File[] files = file.listFiles();
            for(int i=0; i<files.length; i++)
                addFile(zos, files[i]);
        }
        else
        {
            byte buf[] = new byte[1024];
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            int bytes;
            while ((bytes = bis.read(buf, 0, buf.length)) != -1)
                zos.write(buf, 0, bytes);
            bis.close();
        }
        zos.closeEntry();
    }    private String entryName(File file)
    {
        String entryName = file.getPath();
        if (file.isDirectory())
            entryName = entryName.endsWith(File.separator) ? entryName : entryName + File.separator;
        entryName = entryName.replace(File.separatorChar, '/').substring(preffixLen);
        return entryName;
    }    public static void main(String args[])
    {
        try
        {
            FileOutputStream fos = new FileOutputStream("test.zip");
            FileZip zip = new FileZip();
            zip.compress(new BufferedOutputStream(fos), new File("d:\\test\\jsp")); //compress folder d:\test\jsp
            fos.close();
        }
        catch (Throwable e)
        {
            e.printStackTrace();
        }
    }
}

解决方案 »

  1.   

    谢谢cbhyk的帮助,但解压以后不是独立的压缩前的文件,而是在一个s的文件夹目录下?
    该如何修改?
      

  2.   

    上面的代码即可压缩目录,也可压缩文件。如果你要压缩的是多个文件,把它们放到一个名为s主目录,然后压缩那个目录即可。如果要压缩的是一个文件,可用如下代码:private static void testZipFile() throws IOException
    {
        InputStream is = new BufferedInputStream(new FileInputStream("test.dat"), 4096); //source file;
        OutputStream os = new BufferedOutputStream(new FileOutputStream("test.zip"), 4096); //dest file;
        ZipOutputStream out = new ZipOutputStream(fos);
        ZipEntry entry = new ZipEntry("s/test.dat");
        out.putNextEntry(entry);
        byte[] buf = new byte[4096];
        int bytes = 0;
        while((bytes = is.read(buf)) != -1)
            out.write(buf, 0, bytes);
        is.close();
        out.close();
    }
      

  3.   

    或者修改entryName方法,在返回前加一个“s/”:
    private String entryName(File file)
    {
        String entryName = file.getPath();
        if (file.isDirectory())
            entryName = entryName.endsWith(File.separator) ? entryName : entryName + File.separator;
        entryName = entryName.replace(File.separatorChar, '/').substring(preffixLen);
        return "s/" + entryName;
    }
      

  4.   

    多谢cbhyk(),可能是我表达上的问题,现在我遇见的问题是,利用java API压缩一个文件,由于java自带字符集跟操作系统不同,用winzip等工具解压以后中文名存在乱码的问题,所以我现在自己写了一个解压程序来解压,但是我的压缩程序压完以后解开是带着服务器端的路径的,这个问题我的解压程序又不能解决.
    我刚才的问题的意思是,是否能够将文件压缩后不放在一个目录底下而是直接,而是直接象用第三方工具解压那样把文件直接解到当前目录?
      

  5.   

    比如我要压缩的文件是a.doc ,b.doc,现在解压出来都是放在在文件d:/unzip/s/下的,而我想直接把解压文件放到d:/unzip/下,不知道行不行?
      

  6.   

    这样压缩出来就没目录:private static void testZipFiles() throws IOException
    {
        String[] files = new String[]{"d:\\a.doc", "d:\\test\\b.doc"};//待压缩文件,在任何目录都可以    OutputStream os = new BufferedOutputStream(new FileOutputStream("test.zip"), 4096); //压缩后的文件
        ZipOutputStream out = new ZipOutputStream(os);
        byte[] buf = new byte[4096];
        for(int i=0; i<files.length; i++)
        {
            InputStream is = new BufferedInputStream(new FileInputStream(files[i]), 4096);
            int index = files[i].lastIndexOf(File.separatorChar);
            ZipEntry entry = new ZipEntry(index==-1 ? files[i] : files[i].substring(index+1));
            out.putNextEntry(entry);
            int bytes = 0;
            while((bytes = is.read(buf)) != -1)
                out.write(buf, 0, bytes);
            is.close();
        }
        out.close();
    }