我的问题是:我现在可以将新的文本文件压缩到一个指定的压缩文件,但压缩文件中以前的文件就没有了,怎么回事呢?
比如有个压缩文件为  file.zip,里边由a.txt和b.txt两个文件。
现在将c.txt压缩到里边,但压缩完成后,file.zip中只有c.txt文件了,怎么回事呢?
         File file = new File(fileName);
        
         if(!file.exists()){
         try {
         file.createNewFile();
} catch (IOException e) {
log.error("文件创建错误" + e.getMessage());
e.printStackTrace();
}
         }
            
             FileOutputStream fout = new FileOutputStream(file);
             ZipOutputStream zout = new ZipOutputStream(fout);
            
             DateFormat df=DateFormat.getDateTimeInstance();
             String fdate = df.format(new java.util.Date());
            
            
            
             zout.setMethod(ZipOutputStream.DEFLATED);
            
             zout.putNextEntry(new ZipEntry("111"));
             //测试
             byte[] b = new byte[3];
             b = new String("123").getBytes();
             byte[] v = new byte[2];
             v = new String("\r\n").getBytes();
             zout.write(b,0,b.length);
             zout.write(v,0,v.length);
             zout.write(b,0,b.length);
             zout.flush();
             zout.close();
             fout.close();

解决方案 »

  1.   

    找个中间产物吧.
    import java.util.Enumeration;
    import java.util.zip.*;
    import java.io.*;public class ZIO { /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
    new ZIO().add("C:/a.zip", "C:/a.txt");
    }
    public void add(String src,String addFile) throws Exception
    {
    ZipFile zf=new ZipFile(src);
    File tempf;
    Enumeration e=zf.entries();
    ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(tempf=File.createTempFile("tmp","zip")));
    while(e.hasMoreElements())
    {
    ZipEntry ze=(ZipEntry) e.nextElement();
    zos.putNextEntry(ze);
    int temp;
    InputStream ist=zf.getInputStream(ze);
    while((temp=ist.read())!=-1) zos.write(temp);
    ist.close();
    zos.closeEntry();
    }
    zf.close();
    zos.putNextEntry(new ZipEntry(new File(addFile).getName()));
    FileInputStream ff=new FileInputStream(addFile);
    int r=0;
    while((r=ff.read())!=-1)
    {
    zos.write(r);
    }
    zos.closeEntry();
    zos.close();
    new File(src).delete();
    tempf.renameTo(new File(src));
    tempf.delete();
    }}
      

  2.   

    其实这并不是很好的办法.因为我用winrar试过,如果是在zip里加入一个文件比较快的.当然删除其中的一个文件它也用了中间文件的,不然没有办法.
      

  3.   

    哈哈,我把问题解决了啦!其实肯定是要中间文件的.但是不用这么麻烦,前面的只要复制一下就OK了,请看代码:
    import java.util.Enumeration;
    import java.util.zip.*;
    import java.io.*;public class ZIP2 { /**
     * @param args
     */
    public static void main(String[] args) throws Exception
    {
    new ZIP2().add("C:/a.zip", "C:/a.txt");
    }
    public void add(String src,String addFile) throws Exception
    {
    FileInputStream fis=new FileInputStream(src);
    File temp=File.createTempFile("tmp", "zip");
    ZipOutputStream zos=new ZipOutputStream(new FileOutputStream(temp));
    int t;
    while((t=fis.read())!=-1)
    {
    zos.write(t);
    }
    zos.putNextEntry(new ZipEntry(new File(addFile).getName()));
    FileInputStream fis2=new FileInputStream(addFile);
    while((t=fis2.read())!=-1)
    {
    zos.write(t);
    }
    zos.closeEntry();
    zos.close();
    fis.close();
    fis2.close();
    new File(src).delete();
    temp.renameTo(new File(src));
    }}
      

  4.   

    Just try it!!!!FileOutputStream fout = new FileOutputStream(file);============>FileOutputStream fout = new FileOutputStream(file, true);
      

  5.   

    你好,你的方法,我试了,第一段代码是好用的,我试验已经没有问题了,但第二段代码是不行的,因为在zos.write(t);之前是一定要zos.putNextEntry的,不然要出错的。