JAVA 打ZIP 包

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

 

 

public class Zip{

   private void toZip(String fileName,String FilePath){

  

  try {

ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(fileName));

   

File file = new File(FilePath);

if(file.exists()&&file.isDirectory()){

    File[] file1 = file.listFiles();

    

    for(File file2: file1){

    String file2Name = file2.getName();

    byte[] byt = new byte[(int) file2.length()];

    FileInputStream fi = new FileInputStream(file2);

    fi.read(byt);

    zo.putNextEntry(new ZipEntry(file2Name));

    zo.write(byt);

    }

    zo.close();

}

  

  } catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

   public static void main(String[] args) {

Zip zip = new Zip();

zip.toZip("d://lick.zip","d://lick");

}

}

 

 

 

 

 

 

用ant的:

 

package zip;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

import org.apache.tools.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream;

 

 

public class ZipTool {

 

   public void toZip(String fileName,String filePath){

  File file  = new File(filePath);

  ZipOutputStream out = null;

 System.out.println(System.currentTimeMillis());

  try {

  out = new ZipOutputStream(

 new FileOutputStream (fileName) 

  );

toZip(out,file,filePath);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

out.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

   }

 

   public void toZip(ZipOutputStream out,File file,String filePath){

  

  if(file.isDirectory()){

  try {

String afterReplace = file.getPath().replace(filePath, "");  

if(!afterReplace.equals("")){

afterReplace=afterReplace.substring(1);

// System.out.println("afterReplace111"+afterReplace);

// afterReplace=afterReplace.replace(file.getName(),"");

}

System.out.println("第一个"+afterReplace+"/");

out.putNextEntry(new ZipEntry(afterReplace+"/"));

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

  for(File singleFile:file.listFiles()){

  toZip(out,singleFile,filePath);

  }

  }else{

  

 try {

//    String[] name = file.getName().split("/");

//        

//    String splitName = name[name.length-1];

//    

       String splitName = file.getPath().replace(filePath,"");

      

       if(!splitName.equals("")){

        splitName=splitName.substring(1);

//         splitName=splitName.replaceAll("","/");

       }

       System.out.println("第二个"+splitName);

out.putNextEntry(new ZipEntry(splitName));

FileInputStream fi = new FileInputStream(file);

int len;

while((len = fi.read())!= -1){

out.write(len);

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

  }

  

  

  

   }

//   public  void toZip(String fileName,String filePath) throws IOException{

//     File  file = new File(filePath);

//     ZipOutputStream op = null;

//     try {

// op = new ZipOutputStream(

//  new FileOutputStream(fileName)

// );

// } catch (FileNotFoundException e1) {

// // TODO Auto-generated catch block

// e1.printStackTrace();

// }

//     for(File singleFile:file.listFiles()){

//     if(singleFile.isDirectory()){

//     op.putNextEntry(new ZipEntry(singleFile.getName()+"/"));

//     toZip();

//     }else{

//     try {

//     int len;

// FileInputStream fi = new FileInputStream(singleFile.getPath());

//     op.putNextEntry(new ZipEntry(singleFile.getName()));

// while((len = fi.read())!= -1){

//     op.write(len);

//    }

//     } catch (FileNotFoundException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// } catch (IOException e) {

// // TODO Auto-generated catch block

// e.printStackTrace();

// } 

//     }

//     }

//     

//     op.close();

//   }

   public static void main(String[] args) throws IOException {

   ZipTool zipTool = new ZipTool();

   String fileName ="D://lick.zip";

   String filePath ="D://lick";

   zipTool.toZip(fileName, filePath);

 }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值