/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */package filestudy;import java.util.zip.*;
import java.io.*;/**
 *
 * @author zhanglongfei
 */
public class DecZip {
    public void DoIt(){
        try
        {
            ZipInputStream in = new ZipInputStream(new FileInputStream("D:/tools.zip"));
            ZipEntry entry = in.getNextEntry();
            while( ((entry = in.getNextEntry()) != null) && !entry.isDirectory() ){
                File file = new File(entry.getName());
                if (!file.exists()){
                    file.createNewFile();
                }
                in.closeEntry();
                System.out.println( entry.getName() + "解压成功!");
            }
            in.close();
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }
}这样的话解压的时候不能自动创建目录, 而且创建的文件并不是从压缩文件中读取的,而是新创建一个文件.
如何才能达到我要的效果呢? 

解决方案 »

  1.   

    要用输出流往文件系统里写,而不是只创建一个文件名。这个是我以前用的
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class Unzip {
    public static boolean unzipfs(InputStream urlin,String savepath){
      
      if(urlin==null){
      return false;
      }else{
      try{
      BufferedInputStream in = new BufferedInputStream(urlin);   
              ZipInputStream zin = new ZipInputStream(in);   
              ZipEntry e = null;
                         
              while((e=zin.getNextEntry())!= null){
                    File absolute = new File(new File(savepath),e.getName());   //创建该条目的绝对路径
             if(e.isDirectory()){                          //如果读取的条目是目录,则创建该目录
                           if(!absolute.exists()){
             absolute.mkdir();
                }
             }else{                                        //不是目录则
             unzipfile(zin,absolute);              //调用这个函数往文件系统里写数据
             }
              }   
              zin.close();
              return true;
      }catch(Exception e){
    return false;
      }
      }
    }

    private static void unzipfile(ZipInputStream zin,File s)throws IOException {    //数据输出的函数
             FileOutputStream out = new FileOutputStream(s);   
             byte[] b = new byte[1024];   
             int len = 0;   
             while((len=zin.read(b))!=-1){   
                 out.write(b,0,len);   
             }   
             out.close();   
    }
    }大概意思就是这样了。
      

  2.   


    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;public class Zip {
    //定义缓冲区大小
    private  final static int BUFFER = 1024;
    public Zip(){

    }
    public void upZip(String zipPath,String unZipPath)
    {
    try
    {
    //获取压缩文件 *.zip 格式
    ZipInputStream in = new ZipInputStream(new FileInputStream(zipPath));
    ZipEntry entry = null;
    while((entry = in.getNextEntry()) != null)
    {
    String entryName = entry.getName();
    if(entry.isDirectory())
    {
    File file = new File(unZipPath+entryName);
    file.mkdirs();
    System.out.println("系统提示信息:" + "创建"+entryName+"文件夹成功");
    }
    else
    {
    FileOutputStream out = new FileOutputStream(unZipPath+entryName);
    byte[] buf = new byte[BUFFER];
    int len;
    while((len = in.read(buf)) > 0)
    {
    out.write(buf,0,len);
    }
        out.close();
        in.closeEntry();
    }
    }
    }catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) {
    Zip z = new Zip();
    z.upZip("f:/woshizip.zip", "f:");
    }
    }以前写的
      

  3.   

    问题解决了 我添加压缩的时候压缩文件里面的目录是错的 
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */package filestudy;import java.util.zip.*;
    import java.io.*;
    /**
     *
     * @author zhanglongfei
     */
    public class ZipStream {
        public void DoSomething(String zipFileName,File inputFile){
            try
            {
                ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
                zip(out,inputFile,"");
                System.out.println("压缩中...");
                out.close();
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
        private void zip(ZipOutputStream out,File f,String base)throws Exception{
            if (f.isDirectory()){
                File[] fl = f.listFiles();
                out.putNextEntry(new ZipEntry(base + "/"));
                base = base.length() == 0 ? "" : base + "/";
                for (int i = 0; i < fl.length; i ++){
                    zip(out, fl[i],base + fl[i]);
                }
            }
            else
            {
                out.putNextEntry(new ZipEntry(base));
                FileInputStream in = new FileInputStream(f);
                int b;
                System.out.println(base);
                while((b = in.read()) != -1){
                    out.write(b);
                }
                in.close();
            }
        }
    }能压缩成功, 但是解压的时候出错. 可以仍然可以解压成功 哪的问题呢?
      

  4.   

    你3楼的是压缩的代码啊,建议压缩完了用winrar解压缩看看,如果没问题,就证明是解压代码的错误。
    不妨把解压代码贴出来看看。
      

  5.   

    我直接在windows下解压的也出错