这个程序是用来压缩文件的。
这个程序的MAIN函数没读懂,求解释,
print()函数读懂了,
我看着用户文档边查边看,还是有些看不懂。不能理解MAIN()函数的整体的流程。所以发问。
我想问两个问题:
1。压缩文件的源地址在哪里设置?目的地址在哪里设置?(两个地址)
2。这个程序可不可以用于压缩文件夹?(因为我想文件夹里有很多文件。压缩就像打包一样),然后再给压缩后的文件命名。
package tom;
import java.io.*;
import java.util.zip.*;class ToZip {
   public static void main(String args[]) throws IOException {
     byte b[] = new byte[512];
     ZipOutputStream zout = new ZipOutputStream(System.out);
     for(int i = 0; i < args.length; i ++) {
       InputStream in = new FileInputStream(args[i]);//这句是什么意思?是不是在运行时还要自己输入?输入什么东西?打个比方(如:java ToZip yang.txt?是这样的吗?)。
       ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/'));//这句是什么意思?
       zout.putNextEntry(e);
       int len=0;
       while((len=in.read(b)) != -1) {
         zout.write(b,0,len);
         }
       zout.closeEntry();
       print(e);
       }
     zout.close();
     }
     
   public static void print(ZipEntry e){
     PrintStream err = System.err;
     err.print("added " + e.getName());
     if (e.getMethod() == ZipEntry.DEFLATED) {
       long size = e.getSize();
       if (size > 0) {
         long csize = e.getCompressedSize();
         long ratio = ((size-csize)*100) / size;
         err.println(" (deflated " + ratio + "%)");
         }
       else {
         err.println(" (deflated 0%)");
         }
       }
     else {
       err.println(" (stored 0%)");
       }
     }
   }
然后就是,我只有45分了,都给你。请你帮个忙。

解决方案 »

  1.   

    ZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/'));//这句是什么意思?
     你看 单词的意思就可以知道 是吧 文件中的分隔符 替换成 /第一句你说的正确 去看下FileInputStream的API
      

  2.   


    InputStream in = new FileInputStream(args[i]);//这句是什么意思?是不是在运行时还要自己输入?输入什么东西?打个比方(如:java ToZip yang.txt?是这样的吗?)。是这样的  不过这里是args[i]表明你可以输入好几个参数,比如java ToZip yang.txt 1.txt w.txtZipEntry e = new ZipEntry(args[i].replace(File.separatorChar,'/'));//这句是什么意思?把每个文件的系统分隔符替换为/
    比如一个路径D:\Program Files\eclipse经过这句后变为D:/Program Files/eclipse
      

  3.   

    试了一下,“是这样的 不过这里是args[i]表明你可以输入好几个参数,比如java ToZip yang.txt 1.txt w.txt”
    不过出错了,
    下面是我从DOS界面复制出来的东西。看下,有什么问题。
    D:\cc\Bean\bin\tom>java ToZip    yant.txt
    Exception in thread "main" java.lang.NoClassDefFoundError: ToZip (wrong name: tom/ToZip)
      

  4.   

    然后,里面的ZipOutputStream zout = new ZipOutputStream(System.out);是什么意思??
    我怎么去设置压缩文件的源地址和目的地址?