java如何打包zip等压缩文件

  直接调用jdk的类可以了。

 package javaio;
import java.io.*;
import java.util.zip.*;
public class ZipCompress{
 public static void main(String args[]){
  try{
   ZipCompress zip=new ZipCompress();
   FileOutputStream fileOut=new FileOutputStream("Example.zip");
   CheckedOutputStream checkOut=new CheckedOutputStream(fileOut,new CRC32());
   ZipOutputStream zipOut=new ZipOutputStream(new BufferedOutputStream(checkOut));
   zipOut.setComment("This is a java zipping test file");
   //String fileName=zip.getClass().getResource("test.txt").toString();
   String fileName="E:\\test.txt";
   System.out.println(fileName);
   System.out.println(zip.getClass());
   BufferedReader in=new BufferedReader(new FileReader(fileName));
   zipOut.putNextEntry(new ZipEntry(fileName));//建立压缩实体
   int ch;
   //当压缩过程未结束时继续读写
   while((ch=in.read())!=-1){
    zipOut.write(ch);
    System.out.println(ch);
   }
   in.close();
   zipOut.close();
  }catch(IOException e){
   System.out.println(e.toString());
  }
 }
}

你可能感兴趣的:(java)