本文实例为大家分享了 java 实现多个 文件压缩 的具体代码,供大家参考,具体内容如下
需要用到的ant.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
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()); } } }
} |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/NFA_YY/article/details/73613406
查看更多关于java实现多个文件压缩成压缩包的详细内容...