<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(260) 評論(0)  編輯  收藏

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


    網站導航:
     

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

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    PL/SQL存儲過程與函數

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲AV无码不卡在线播放| 免费黄网站在线观看| 老司机亚洲精品影院无码| 四虎影视永久免费观看地址| 日韩在线播放全免费| 国产激情免费视频在线观看| 免费一级毛suv好看的国产网站| 99久久婷婷国产综合亚洲| 亚洲视频在线免费观看| 国产精品亚洲美女久久久| 日本牲交大片免费观看| 一二三四在线播放免费观看中文版视频 | 亚洲精品NV久久久久久久久久| 免费无码精品黄AV电影| 曰批全过程免费视频播放网站| 国产午夜精品免费一区二区三区| 免费人成动漫在线播放r18| 亚洲av日韩综合一区久热| 亚洲入口无毒网址你懂的| 亚洲国产成人精品无码一区二区| 五月天网站亚洲小说| 亚洲日韩中文字幕在线播放| 亚洲JIZZJIZZ中国少妇中文| 国产jizzjizz视频全部免费| 女人让男人免费桶爽30分钟| 免费H网站在线观看的| 4399好看日本在线电影免费| 182tv免费观看在线视频| 最近2019年免费中文字幕高清| 日本免费人成视频在线观看| 亚洲精品免费观看| 91福利视频免费| 2019中文字幕免费电影在线播放| 99re在线这里只有精品免费| 中文字幕免费视频一| h视频在线观看免费网站| 男女做羞羞的事视频免费观看无遮挡 | 九月婷婷亚洲综合在线| 亚洲成AV人在线观看网址| 亚洲国产成人精品91久久久| 亚洲免费在线观看|