欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现多个文件压缩成压缩包

程序员文章站 2024-02-17 20:35:04
本文实例为大家分享了java实现多个文件压缩的具体代码,供大家参考,具体内容如下 需要用到的ant.jar package util; import ja...

本文实例为大家分享了java实现多个文件压缩的具体代码,供大家参考,具体内容如下

需要用到的ant.jar

package util;

import java.io.bufferedinputstream;
import java.io.bufferedoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.inputstream;
import java.io.outputstream;
import java.util.arraylist;
import java.util.enumeration;
import java.util.list;
import java.util.zip.zipentry;
import java.util.zip.zipfile;
import java.util.zip.zipoutputstream;


public class ziptools { 

  public static final string zip_filename = ""; //需要解压缩的文件名 
  public static final string zip_dir = "";  //需要压缩的文件夹 
  public static final string un_zip_dir = "";  //要解压的文件目录 
  public static final int buffer = 1024 ;  //缓存大小 

public static void zipfile(string basedir,string filename) throws exception{ 
    list filelist=getsubfiles(new file(basedir)); 
    zipoutputstream zos=new zipoutputstream(new fileoutputstream(filename)); 
    zipentry ze=null; 
    byte[] buf=new byte[buffer]; 
    int readlen=0; 
    for(int i = 0; i <filelist.size(); i++) { 
      file f=(file)filelist.get(i); 
      ze=new zipentry(getabsfilename(basedir, f)); 
      ze.setsize(f.length()); 
      ze.settime(f.lastmodified());   
      zos.putnextentry(ze); 
      inputstream is=new bufferedinputstream(new fileinputstream(f)); 
      while ((readlen=is.read(buf, 0, buffer))!=-1) { 
        zos.write(buf, 0, readlen); 
      } 
      is.close(); 
    } 
    zos.close(); 
  } 


private static string getabsfilename(string basedir, file realfilename){ 
    file real=realfilename; 
    file base=new file(basedir); 
    string ret=real.getname(); 
    while (true) { 
      real=real.getparentfile(); 
      if(real==null)  
        break; 
      if(real.equals(base))  
        break; 
      else 
        ret=real.getname()+"/"+ret; 
    } 
    return ret; 
  } 

private static list getsubfiles(file basedir){ 
    list ret=new arraylist(); 
    file[] tmp=basedir.listfiles(); 
    for (int i = 0; i <tmp.length; i++) { 
      if(tmp[i].isfile()) 
        ret.add(tmp[i]); 
      if(tmp[i].isdirectory()) 
        ret.addall(getsubfiles(tmp[i])); 
    } 
    return ret; 
  } 

public static void upzipfile() throws exception{ 
    zipfile zfile=new zipfile(zip_filename); 
    enumeration zlist=zfile.entries(); 
    zipentry ze=null; 
    byte[] buf=new byte[1024]; 
    while(zlist.hasmoreelements()){ 
      ze=(zipentry)zlist.nextelement();     
      if(ze.isdirectory()){ 
        file f=new file(zip_dir+ze.getname()); 
        f.mkdir(); 
        continue; 
      } 
      outputstream os=new bufferedoutputstream(new fileoutputstream(getrealfilename(zip_dir, ze.getname()))); 
      inputstream is=new bufferedinputstream(zfile.getinputstream(ze)); 
      int readlen=0; 
      while ((readlen=is.read(buf, 0, 1024))!=-1) { 
        os.write(buf, 0, readlen); 
      } 
      is.close(); 
      os.close();  
    } 
    zfile.close(); 
  } 

public static file getrealfilename(string basedir, string absfilename){ 
    string[] dirs=absfilename.split("/"); 
    file ret=new file(basedir); 
    if(dirs.length>1){ 
      for (int i = 0; i < dirs.length-1;i++) { 
        ret=new file(ret, dirs[i]); 
      } 
      if(!ret.exists()) 
        ret.mkdirs(); 
      ret=new file(ret, dirs[dirs.length-1]); 
      return ret; 
    } 
    return ret; 
  } 

public static void deletedirfile(string path){
   file file=new file(path);
   if(file.isdirectory()){    //如果是目录,先递归删除
     string[] list=file.list();
     for(int i=0;i<list.length;i++){
     deletedirfile(path+"\\"+list[i]); //先删除目录下的文件
     }
   }    
   file.delete();
 }


public static string newfolder(string dir){
 java.io.file myfilepath=new java.io.file(dir);
 if(myfilepath.isdirectory()){}else{
  myfilepath.mkdirs();
 }
 return dir;
 }

public static string getfilenames(string path){  

    file file = new file(path);      // get file list where the path has
    file[] array = file.listfiles();     // 获得文件列表 
    string pdfnames="";

    for(int i=0;i<array.length;i++){  
      if(array[i].isfile()){  
        if(array[i].getname().endswith(".pdf")){  //获得pdf文件名称
         pdfnames+=array[i].getname().substring(0,array[i].getname().length()-4)+",";
        }
      } 
    }
    if(pdfnames.length()>0){
     pdfnames.substring(0,pdfnames.length()-1);
    }
    return pdfnames;
  }

public static void copyfile(string oldpath, string newpath) {
    try {
      int bytesum = 0;
      int byteread = 0;
      file oldfile = new file(oldpath);
      if (oldfile.exists()) {     //文件存在时
        inputstream instream = new fileinputstream(oldpath); //读入原文件
        fileoutputstream fs = new fileoutputstream(newpath);
        byte[] buffer = new byte[1444];
        while ( (byteread = instream.read(buffer)) != -1) {
          bytesum += byteread;    //字节数文件大小
          fs.write(buffer, 0, byteread);
        }
        instream.close();
      }
    }
    catch (exception e) {
      system.out.println("copy file error!");
      e.printstacktrace();
    }
  }

public static boolean fileexist(string filenames,string pdfname){
 boolean flag=false;
 if(!"".equals(filenames)){
  string[] namearr=filenames.split(",");
  for(int i=0;i<namearr.length;i++){
  if(pdfname.equals(namearr[i])){    //如果文件名相同则执行拷贝操作(拷贝到zip目录准备打包)
   flag=true;
   break;
  }
  }
 }
 return flag;
 }

public static void deletefileanddir(string path){
  file file = new file(path);  
     file[] array = file.listfiles(); 
     for(int i=0;i<array.length;i++){  
       if(array[i].isfile()){
         array[i].delete();
       }else if(array[i].isdirectory()){  
       deletedirfile(array[i].getpath()); 
       }  
     }  
 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。