本人想设计一个压缩软件,像winrar一样,在压缩和解压缩的同时显示进度条.为保证解压缩过程与进度条同步,我使用ProgressMonitorInputStream自动弹进度条,但运行结果却没有进度条显示.望各位swing高手帮忙看一下:
    public void unzip1(final File zipfile) throws IOException{
      new Thread() {
        public void run() {
          try {
            System.out.println("Releasing file " + zipfile.getName());
            FileInputStream fi = new FileInputStream(zipfile);
            CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
            ZipInputStream in2 = new ZipInputStream(new BufferedInputStream(csumi));
            //BufferedReader br=new BufferedReader(new InputStreamReader(in2));//把in2包装成字节缓冲流
            ProgressMonitorInputStream pmi = new ProgressMonitorInputStream(new JFrame(),
                "vfvfvf", in2);
            ProgressMonitor pm = pmi.getProgressMonitor();
            pm.setMillisToDecideToPopup(0);
            pm.setMillisToPopup(0);
  
            ZipEntry ze;
            while ( (ze = in2.getNextEntry()) != null) {
              System.out.println("Reading file " + ze);
              File file = new File(zipfile.getParent() + "\\" + ze.getName());
              FileOutputStream fo = new FileOutputStream(file);
              //FileWriter fo=new FileWriter(file);
              //可用br.read()或br.readLine()两种方法
              int x;
              while ( (x = pmi.read()) != -1) {
                //System.out.write(x);
                System.out.println(x);
                fo.write(x);
                try {
                  Thread.sleep(3000);
                }
                catch (InterruptedException e) {}              }
              fo.close(); //不关闭则会刷新已写入信息            }
            System.out.println("Checksum:" + csumi.getChecksum().getValue());
            in2.close();
            pmi.close();          }catch(Exception e){e.printStackTrace();}
        }
      }.start();    }

解决方案 »

  1.   

    包装流的顺序错了FileInputStream fi = new FileInputStream(zipfile);
    ProgressMonitorInputStream pmi = new ProgressMonitorInputStream(new JFrame(), "vfvfvf", fi);
    CheckedInputStream csumi = new CheckedInputStream(pmi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(new BufferedInputStream(csumi));
    ProgressMonitor pm = pmi.getProgressMonitor();