posts - 33,  comments - 17,  trackbacks - 0
            1// 一個壓縮解壓縮文件和目錄的完整程序.  
            2// 版權沒有, 隨便使用. 
            3package cross.pauliuyou.tool.io; 
            4
            5import java.io.*
            6import java.util.*
            7import java.util.zip.*
            8
            9
           10public class ZipUtil 
           11
           12public static int bufsize = 8192
           13private String zipBase; 
           14
           15private int maxFileNameLength; 
           16private long totalLength; 
           17private long bytesHandled; 
           18
           19private String getSpace(int num) 
           20
           21String space = ""
           22for (int i = 0; i < num; i++
           23
           24space += " "
           25}
           
           26return space; 
           27}
           
           28private void getLength(File path) 
           29
           30if (path.isFile()) 
           31
           32totalLength += path.length(); 
           33if (path.getPath().length() > maxFileNameLength) 
           34
           35maxFileNameLength = path.getPath().length(); 
           36}
           
           37}
           
           38else 
           39
           40File [] fs = path.listFiles(); 
           41for (int i = 0; i < fs.length; i++
           42
           43getLength(fs[i]); 
           44}
           
           45}
           
           46}
           
           47
           48private String lengthString(long fileLength) 
           49
           50long newValue = 0
           51String size = null
           52if (fileLength < 1024
           53size = fileLength + " B"
           54}
           else if (fileLength < (1024 * 1024)) 
           55newValue = fileLength / 1024
           56size = newValue + " KB"
           57}
           else if (fileLength < (1024 * 1024 * 1024)) 
           58newValue = fileLength / (1024 * 1024); 
           59size = newValue + " MB"
           60}
           else 
           61newValue = fileLength / (1024 * 1024 * 1024); 
           62size = newValue + " GB"
           63}
           
           64return size; 
           65}
           
           66public void zip(String path) throws Exception 
           67
           68zip(path,null); 
           69}
           
           70public void zip(String path,String dest) throws Exception 
           71
           72File f = new File(path); 
           73if (!f.exists()) 
           74
           75System.out.println("File : " + path + " Not Exists!"); 
           76return
           77}
           
           78getLength(f); 
           79
           80System.out.println("total " + lengthString(totalLength) + " to zip"); 
           81String path2 = ""
           82for (int i = 0; i < path.length(); i++
           83
           84char c = path.charAt(i); 
           85if (c == '\\'
           86
           87= '/'
           88}
           
           89path2 += c; 
           90}
           
           91path = path2; 
           92
           93zipBase = path.substring(path.lastIndexOf("/"== -1 ? 0 : path.lastIndexOf("/"+ 1); 
           94if (dest == null
           95
           96if (f.isFile()) 
           97
           98if (dest == null
           99
          100zipBase = "./"
          101dest = path.substring(0,(path.lastIndexOf("."== -1 ? path.length() : path.lastIndexOf("."))) + ".zip"
          102}
           
          103}
           
          104else 
          105
          106path2 = path.substring(0,path.lastIndexOf("/"== -1 ? path.length() : path.lastIndexOf("/")); 
          107if (path.equals(path2)) 
          108
          109dest = "."
          110}
           
          111dest = path2 + "/" + zipBase.substring(0,(zipBase.lastIndexOf("."== -1 ? zipBase.length() : zipBase.lastIndexOf("."))) + ".zip"
          112}
           
          113}
           
          114ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(dest)); 
          115zipFile(f,zipOut); 
          116zipOut.close(); 
          117System.out.print("\r100%\t"); 
          118System.out.println(path + " has been compressed to zip file : " + dest + getSpace(40)); 
          119}
           
          120
          121public void zipFile(File f,ZipOutputStream zipOut) throws Exception 
          122
          123if (f.isDirectory()) 
          124
          125File [] fs = f.listFiles(); 
          126for (int i = 0; i < fs.length; i++
          127
          128zipFile(fs[i],zipOut); 
          129}
           
          130}
           
          131else 
          132
          133String path = f.getPath(); 
          134FileInputStream fileIn = new FileInputStream(path); 
          135String entryName = path; 
          136int basePos = path.indexOf(zipBase); 
          137if (basePos > 0
          138
          139entryName = path.substring(basePos); 
          140}
           
          141ZipEntry theEntry = new ZipEntry(entryName); 
          142theEntry.setSize(f.length()); 
          143
          144zipOut.putNextEntry(theEntry); 
          145
          146byte[] buffer = new byte[bufsize]; 
          147int bytesRead = fileIn.read(buffer,0,bufsize); 
          148bytesHandled += bytesRead; 
          149
          150while (bytesRead > 0
          151
          152zipOut.write(buffer,0,bytesRead); 
          153bytesRead = fileIn.read(buffer,0,bufsize); 
          154bytesHandled += bytesRead; 
          155int percent = (int)(((double)bytesHandled / totalLength) * 100 ); 
          156System.out.print("\r" + percent + "%  "); 
          157if (totalLength > 1024 * 1024 * 80
          158
          159int j; 
          160for (j = 0; j < percent % 4 + 1; j++
          161
          162System.out.print(">"); 
          163
          164}
           
          165for (int x = 0; x < 4 - j; x++
          166
          167System.out.print(" "); 
          168}
           
          169}
           
          170System.out.print("  Compress File " + path + getSpace(maxFileNameLength - path.length())); 
          171}
           
          172fileIn.close(); 
          173zipOut.closeEntry(); 
          174}
           
          175}
           
          176
          177public void unzip(String zipFileName) throws Exception 
          178
          179unzip(zipFileName,null); 
          180}
           
          181
          182public void unzip(String zipFileName,String dest) throws Exception 
          183
          184File f = new File(zipFileName); 
          185if (!f.exists()) 
          186
          187System.out.println("Zip File : " + zipFileName + " Not Exists!"); 
          188return
          189}
           
          190
          191byte[] buffer = new byte[bufsize]; 
          192
          193ZipFile zf = new ZipFile(zipFileName); 
          194
          195ZipEntry theEntry = null
          196
          197Enumeration enumer = zf.entries(); 
          198while (enumer.hasMoreElements()) 
          199
          200theEntry = (ZipEntry)enumer.nextElement(); 
          201totalLength += theEntry.getSize(); 
          202if (theEntry.getName().length() > maxFileNameLength) 
          203
          204maxFileNameLength = theEntry.getName().length(); 
          205}
           
          206}
           
          207
          208System.out.println("Zip File " + lengthString(f.length()) + " and Total " + lengthString(totalLength) + " Data to unzip"); 
          209
          210enumer = zf.entries(); 
          211while (enumer.hasMoreElements()) 
          212
          213theEntry = (ZipEntry)enumer.nextElement(); 
          214String entryName = theEntry.getName(); 
          215String entryName2 = ""
          216for (int i = 0; i < entryName.length(); i++
          217
          218char c = entryName.charAt(i); 
          219if (c == '\\'
          220
          221= '/'
          222}
           
          223entryName2 += c; 
          224}
           
          225entryName = entryName2; 
          226zipBase = "."
          227if (dest != null
          228
          229if (dest.endsWith("/")) 
          230
          231dest = dest.substring(0,dest.length() - 1); 
          232}
           
          233zipBase = dest; 
          234}
           
          235String tmpFolder = zipBase; 
          236
          237int begin = 0
          238int end = 0
          239while (true
          240
          241end = entryName.indexOf("/",begin); 
          242if (end == -1
          243
          244break
          245}
           
          246tmpFolder += "/" + entryName.substring(begin,end); 
          247begin = end + 1
          248= new File(tmpFolder); 
          249if (!f.exists()) 
          250
          251f.mkdir(); 
          252}
           
          253}
           
          254OutputStream os = new FileOutputStream(zipBase + "/" + entryName); 
          255InputStream is = zf.getInputStream(theEntry); 
          256int bytesRead = is.read(buffer,0,bufsize); 
          257bytesHandled += bytesRead; 
          258while (bytesRead > 0
          259
          260os.write(buffer,0,bytesRead); 
          261bytesRead = is.read(buffer,0,bufsize); 
          262bytesHandled += bytesRead; 
          263System.out.print("\r"); 
          264int percent = (int)(((double)bytesHandled / totalLength) * 100 ); 
          265System.out.print("\r" + percent + "%  "); 
          266if (totalLength > 1024 * 1024 * 80
          267
          268int j; 
          269for (j = 0; j < percent % 4 + 1; j++
          270
          271System.out.print(">"); 
          272
          273}
           
          274for (int x = 0; x < 4 - j; x++
          275
          276System.out.print(" "); 
          277}
           
          278}
           
          279
          280System.out.print("  Unzip File " + entryName + getSpace(maxFileNameLength - entryName.length())); 
          281}
           
          282is.close(); 
          283os.close(); 
          284}
           
          285
          286System.out.println("\r100%  Zip File : " + zipFileName + " has been unCompressed to " + dest + "/ !" + getSpace(40)); 
          287}
           
          288
          289public static void main(String []args) throws Exception 
          290
          291if (args.length < 2
          292
          293System.out.println("usage : java cross.pauliuyou.tool.io.ZipUtil -zip[-unzip] filename"); 
          294return
          295}
           
          296ZipUtil zip = new ZipUtil(); 
          297String dest = null
          298if (args[0].equals("-zip")) 
          299
          300if (args.length > 2
          301
          302dest = args[2]; 
          303}
           
          304zip.zip(args[1],dest); 
          305}
           
          306if (args[0].equals("-unzip")) 
          307
          308if (args.length > 2
          309
          310dest = args[2]; 
          311}
           
          312zip.unzip(args[1],dest); 
          313}
           
          314}
           
          315}
          posted on 2008-07-23 17:57 scea2009 閱讀(265) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2008年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類

          隨筆檔案

          PL/SQL存儲過程與函數

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 水富县| 余姚市| 安义县| 包头市| 东安县| 阳高县| 长汀县| 鄂托克旗| 搜索| 台中市| 托里县| 玛纳斯县| 镇赉县| 琼结县| 永昌县| 雅安市| 南雄市| 百色市| 肇州县| 凌源市| 新营市| 自贡市| 无棣县| 玉屏| 平乐县| 徐水县| 赫章县| 卢龙县| 定南县| 牟定县| 蓝山县| 山阴县| 肃宁县| 阆中市| 曲阜市| 晋江市| 聂荣县| 滦平县| 锡林郭勒盟| 汉中市| 大渡口区|