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);  //压缩一个文件夹

  }

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。