码迷,mamicode.com
首页 > 编程语言 > 详细

Java压缩文件

时间:2014-09-24 19:25:07      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
 
public class ll {  
    static final int BUFFER = 8192;  
    private File zipFile;  
    public ll(String pathName) {  
        zipFile = new File(pathName);  
    }  
 
/**
  * 压缩文件
  * @param srcPathName
  */
public void compress(String srcPathName) {
  File file = new File(srcPathName);
  if (!file.exists())
       throw new RuntimeException(srcPathName + "不存在!");
  try {
   FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
   CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream,new CRC32());
   ZipOutputStream out = new ZipOutputStream(cos);
   out.setEncoding("gbk");
   String basedir = "";
   compress(file, out, basedir);
   out.close();
  } catch (Exception e) {
      throw new RuntimeException(e);
  }
}
 
     private void compress(File file, ZipOutputStream out, String basedir) {  
        /* 判断是目录还是文件 */ 
        if (file.isDirectory()) {  
            this.compressDirectory(file, out, basedir);  
        } else {  
            this.compressFile(file, out, basedir);  
        }  
     }  
 
    /** 压缩一个目录 */ 
    private void compressDirectory(File dir, ZipOutputStream out, String basedir) {  
    if (!dir.exists())  
        return;  
    File[] files = dir.listFiles();  
    System.out.println(files.length+"--");
    for (int i = 0; i <files.length; i++) {
        compress(files[i], out, basedir + dir.getName() + "/");
    }  
    }

    /** 压缩一个文件 */
private void compressFile(File file, ZipOutputStream out, String basedir) {
  if (!file.exists()) {
     return;
  }
  try {
   BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
   ZipEntry entry = new ZipEntry(basedir + file.getName());
   out.putNextEntry(entry);
   int count;
   byte data[] = new byte[BUFFER];
   while ((count = bis.read(data, 0, BUFFER)) != -1) {
       out.write(data, 0, count);
   }
   bis.close();
  } catch (Exception e) {
      throw new RuntimeException(e);
  }
}

  private static  String getName(String filename){
  if ((filename != null) && (filename.length() > 0)) {
  int q=filename.lastIndexOf("\\");
  System.out.println(q);
  if(filename.contains(".")){
  int dot = filename.lastIndexOf(‘.‘);
  if ((dot > -1) && (dot < (filename.length() - 1))&&q>-1&&(q < (filename.length() - 1))) {
      return filename.substring(q+1,dot);
  }
  }else{
  if (q>-1&&(q < (filename.length() - 1))) {
      return filename.substring(q + 1);
  }
  }
  }
 
return filename;

}
public static void main(String[] args) {
File f1=new File("C:\\Users\\Administrator\\Desktop\\s\\测试");
File f2=new File("C:\\Users\\Administrator\\Desktop\\s" );
   String fl1=f1.getPath();
   String fl2=f2.getPath();
   String name=getName(fl1);
       ll zc = new ll(fl2+"\\"+name+".zip");
       zc.compress(fl1);  //压缩一个文件夹

  }

Java压缩文件

标签:java

原文地址:http://blog.csdn.net/u012406790/article/details/39525703

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!