在java中实现zip压缩文件的具体实现

有三种不错的方法:

1、jdk自带的包java.util.zip.ZipOutputStream,不足之处,文件(夹)名称带中文时,

出现乱码问题,实现代码如下:

/** 
  * 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件
  * @param sourceDir 如果是目录,eg:D:\\MyEclipse\\first\\testFile,则压缩目录下所有文件;
  *      如果是文件,eg:D:\\MyEclipse\\first\\testFile\\aa.zip,则只压缩本文件
  * @param zipFile 最后压缩的文件路径和名称,eg:D:\\MyEclipse\\first\\testFile\\aa.zip
  */
 public File doZip(String sourceDir, String zipFilePath) 
 throws IOException {
  
  File file = new File(sourceDir);
  File zipFile = new File(zipFilePath);
  ZipOutputStream zos = null;
  try {
   // 创建写出流操作
   OutputStream os = new FileOutputStream(zipFile);
   BufferedOutputStream bos = new BufferedOutputStream(os);
   zos = new ZipOutputStream(bos);
   
   String basePath = null;
   
   // 获取目录
   if(file.isDirectory()) {
    basePath = file.getPath();
   }else {
    basePath = file.getParent();
   }
   
   zipFile(file, basePath, zos);
  }finally {
   if(zos != null) {
    zos.closeEntry();
    zos.close();
   }
  }
  
  return zipFile;
 }

 /** 
  * @param source 源文件
  * @param basePath 
  * @param zos 
  */
 private void zipFile(File source, String basePath, ZipOutputStream zos) 
 throws IOException {
  File[] files = null;
  if (source.isDirectory()) {
   files = source.listFiles();
  } else {
   files = new File[1];
   files[0] = source;
  }
  
  InputStream is = null;
  String pathName;
  byte[] buf = new byte[1024];
  int length = 0;
  try{
   for(File file : files) {
    if(file.isDirectory()) {
     pathName = file.getPath().substring(basePath.length() + 1) + "/";
     zos.putNextEntry(new ZipEntry(pathName));
     zipFile(file, basePath, zos);
    }else {
     pathName = file.getPath().substring(basePath.length() + 1);
     is = new FileInputStream(file);
     BufferedInputStream bis = new BufferedInputStream(is);
     zos.putNextEntry(new ZipEntry(pathName));
     while ((length = bis.read(buf)) > 0) {
      zos.write(buf, 0, length);
     }
    }
   }
  }finally {
   if(is != null) {
    is.close();
   }
  }

 }

2、使用org.apache.tools.zip.ZipOutputStream,代码如下,

Java代码:
  1. package net.szh.zip;   
  2.   
  3. import java.io.BufferedInputStream;   
  4. import java.io.File;   
  5. import java.io.FileInputStream;   
  6. import java.io.FileOutputStream;   
  7. import java.util.zip.CRC32;   
  8. import java.util.zip.CheckedOutputStream;   
  9.   
  10. import org.apache.tools.zip.ZipEntry;   
  11. import org.apache.tools.zip.ZipOutputStream;   
  12.   
  13. public class ZipCompressor {   
  14.     static final int BUFFER = 8192;   
  15.   
  16.     private File zipFile;   
  17.   
  18.     public ZipCompressor(String pathName) {   
  19.         zipFile = new File(pathName);   
  20.     }   
  21.   
  22.     public void compress(String srcPathName) {   
  23.         File file = new File(srcPathName);   
  24.         if (!file.exists())   
  25.             throw new RuntimeException(srcPathName + "不存在!");   
  26.         try {   
  27.             FileOutputStream fileOutputStream = new FileOutputStream(zipFile);   
  28.             CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,   
  29.                     new CRC32());   
  30.             ZipOutputStream out = new ZipOutputStream(cos);   
  31.             String basedir = "";   
  32.             compress(file, out, basedir);   
  33.             out.close();   
  34.         } catch (Exception e) {   
  35.             throw new RuntimeException(e);   
  36.         }   
  37.     }   
  38.   
  39.     private void compress(File file, ZipOutputStream out, String basedir) {   
  40.         /* 判断是目录还是文件 */  
  41.         if (file.isDirectory()) {   
  42.             System.out.println("压缩:" + basedir + file.getName());   
  43.             this.compressDirectory(file, out, basedir);   
  44.         } else {   
  45.             System.out.println("压缩:" + basedir + file.getName());   
  46.             this.compressFile(file, out, basedir);   
  47.         }   
  48.     }   
  49.   
  50.     /** 压缩一个目录 */  
  51.     private void compressDirectory(File dir, ZipOutputStream out, String basedir) {   
  52.         if (!dir.exists())   
  53.             return;   
  54.   
  55.         File[] files = dir.listFiles();   
  56.         for (int i = 0; i < files.length; i++) {   
  57.             /* 递归 */  
  58.             compress(files[i], out, basedir + dir.getName() + "/");   
  59.         }   
  60.     }   
  61.   
  62.     /** 压缩一个文件 */  
  63.     private void compressFile(File file, ZipOutputStream out, String basedir) {   
  64.         if (!file.exists()) {   
  65.             return;   
  66.         }   
  67.         try {   
  68.             BufferedInputStream bis = new BufferedInputStream(   
  69.                     new FileInputStream(file));   
  70.             ZipEntry entry = new ZipEntry(basedir + file.getName());   
  71.             out.putNextEntry(entry);   
  72.             int count;   
  73.             byte data[] = new byte[BUFFER];   
  74.             while ((count = bis.read(data, 0, BUFFER)) != -1) {   
  75.                 out.write(data, 0, count);   
  76.             }   
  77.             bis.close();   
  78.         } catch (Exception e) {   
  79.             throw new RuntimeException(e);   
  80.         }   
  81.     }   

 

3、 可以用ant中的org.apache.tools.ant.taskdefs.Zip来实现,更加简单。 
Java代码
  1. package net.szh.zip;   
  2.   
  3. import java.io.File;   
  4.   
  5. import org.apache.tools.ant.Project;   
  6. import org.apache.tools.ant.taskdefs.Zip;   
  7. import org.apache.tools.ant.types.FileSet;   
  8.   
  9. public class ZipCompressorByAnt {   
  10.   
  11.     private File zipFile;   
  12.   
  13.     public ZipCompressorByAnt(String pathName) {   
  14.         zipFile = new File(pathName);   
  15.     }   
  16.        
  17.     public void compress(String srcPathName) {   
  18.         File srcdir = new File(srcPathName);   
  19.         if (!srcdir.exists())   
  20.             throw new RuntimeException(srcPathName + "不存在!");   
  21.            
  22.         Project prj = new Project();   
  23.         Zip zip = new Zip();   
  24.         zip.setProject(prj);   
  25.         zip.setDestFile(zipFile);   
  26.         FileSet fileSet = new FileSet();   
  27.         fileSet.setProject(prj);   
  28.         fileSet.setDir(srcdir);   
  29.         //fileSet.setIncludes("**/*.java"); 包括哪些文件或文件夹 eg:zip.setIncludes("*.java");   
  30.         //fileSet.setExcludes(...); 排除哪些文件或文件夹   
  31.         zip.addFileset(fileSet);   
  32.            
  33.         zip.execute();   
  34.     }   
  35. }  
测试一下 
Java代码
  1. package net.szh.zip;   
  2.   
  3. public class TestZip {   
  4.     public static void main(String[] args) {   
  5.         ZipCompressor zc = new  ZipCompressor("E:\\szhzip.zip");   
  6.         zc.compress("E:\\test");   
  7.            
  8.         ZipCompressorByAnt zca = new ZipCompressorByAnt("E:\\szhzipant.zip");   
  9.         zca.compress("E:\\test");   
  10.     }   
  11. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值