初学JAVA,看了thinking in java这本书上的生成ZIP文件的例子后,自己试了试,代码如下:
(Dirs是个string[],中间包含很多要压缩的文件的全路径名,
如"C:\book\1.txt"这类文件名的集合。)
      try {
      FileOutputStream f =
        new FileOutputStream("test.zip");
      CheckedOutputStream csum =
        new CheckedOutputStream(
          f, new Adler32());
      ZipOutputStream out =
        new ZipOutputStream(
          new BufferedOutputStream(csum));
      out.setComment("A test of Java Zipping");
      for(int i = 0; i < Dirs.length; i++) {
        System.out.println(
          "Writing file " + Dirs[i]);
        BufferedReader in =
          new BufferedReader(
            new FileReader(Dirs[i]));
        out.putNextEntry(new ZipEntry(Dirs[i]));
        int c;
        while((c = in.read()) != -1)
          out.write(c);
        in.close();
      }
      out.close();
    } catch(Exception e2) {
      e2.printStackTrace();
    }
但是生成的test.zip是一个0字节的zip包。
不知道哪里错了,请大侠指点。

解决方案 »

  1.   

    ZipOutputStream zout = null;
            FileInputStream  fin = null;
            try {
                zout = new ZipOutputStream(new FileOutputStream(new File("d:\\sgr.zip")));
                fin = new FileInputStream("d:\\carlson001.jpg");
                byte[] bb = new byte[4096];
                int i = 0;
                zout.putNextEntry(new ZipEntry("carlson001.jpg"));
                while ((i = fin.read(bb)) != -1) {
                    zout.setLevel(9);
                    zout.write(bb,0,i);
                }
            }
            catch(Exception ex) {
            }
            finally {
                try {
                    zout.close();
                    fin.close();
                }
                catch(Exception ex1) {
                }
            }
      

  2.   

    谢谢Anubis(为朋友两肋插刀,为MM插朋友两刀!!) !
    我检查了一下大小写问题,应该是正确的。
      

  3.   

    我眼睛花了!不知道了!!应该是小问题!
    给你一个我的例子你看看!
    public void makeZIP(String s_zipPath,
                            String s_newZipFile,
                            String s_fileName,
                            String s_filePath,
                            String status,
                            String yearmonth)
                            throws FileNotFoundException,IOException
        {
            try
            {
                File f_compressFile=new File(s_filePath,s_fileName);
                if (f_compressFile.isFile())
                {
                    FileInputStream s_fileIn=new FileInputStream(f_compressFile);
                    DataInputStream in = new DataInputStream(s_fileIn);
                    outZip.putNextEntry(new ZipEntry(s_fileName));                int c;
                    while((c = in.read()) != -1)
                       outZip.write(c);
                    in.close();
                    s_fileIn.close();
                    outZip.closeEntry();
                    java.io.File ZipfileDir = new java.io.File(s_filePath+"\\"+s_fileName);
                    if (ZipfileDir.exists())
                    {
                        ZipfileDir.delete();
                    }
                    if (status=="end")
                    {
                      outZip.close();                  this.addNormalMessage("Download/"+
                          this.getSessionParam().getString(
                          CMN_APP_ColConstant.MANNO)+"_"+yearmonth+".zip");                }
                   }
                else if (f_compressFile.isDirectory())
                {
                    File f_zip=new File(s_zipPath,s_newZipFile);
                    FileOutputStream f_zipFile = new FileOutputStream(f_zip);
                    outZip = new ZipOutputStream(new DataOutputStream(f_zipFile));
                    String[] s_fileList=f_compressFile.list();
                    String s_newDirectory=f_compressFile.getAbsolutePath();
                    String nowStatus=null;
                    for (int i=0;i<s_fileList.length ;i++ )
                    {
                      if (i==s_fileList.length-1)
                      {
                        nowStatus="end";
                      }
                      makeZIP(s_zipPath,s_newZipFile,s_fileList[i],
                          s_newDirectory,nowStatus,yearmonth);
                    }
                }
                //
            }
            catch (Exception e)
            {
              e.printStackTrace();
            }
        }
      

  4.   

    在我这没问题阿,看看你的Dirs长度是不是为0阿。
    我的程序:
    import java.util.zip.*;
    import java.io.*;
    public class TestZip {    public static void main(String [] args) {
        
        String [] Dirs =  args;
        try {
          FileOutputStream f =
            new FileOutputStream("test.zip");
          CheckedOutputStream csum =
            new CheckedOutputStream(
              f, new Adler32());
          ZipOutputStream out =
            new ZipOutputStream(
              new BufferedOutputStream(csum));
          out.setComment("A test of Java Zipping");
          for(int i = 0; i < Dirs.length; i++) {
            System.out.println(
              "Writing file " + Dirs[i]);
            BufferedReader in =
              new BufferedReader(
                new FileReader(Dirs[i]));
            out.putNextEntry(new ZipEntry(Dirs[i]));
            int c;
            while((c = in.read()) != -1)
              out.write(c);
            in.close();
          }
          out.close();
        } catch(Exception e2) {
          e2.printStackTrace();
        }    }
    }
    C:\> java TestZip TestZip.java
    用winzip检查,完全正确
      

  5.   

    我打包时,中文的文件名和文件中的中文变成了乱码了,这该怎么办?是不是哪个属性的设置?
    用的是 farawayzheng_necas(遥远) ( )的方法.
      

  6.   

    转码:1、文件名用new String(fileName.getByte("ISO8859-1"),"GBK");
    2.ZipOutputStream设置编码格式
    我只用了第一种