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

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

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

    athrunwang

    紀(jì)元
    數(shù)據(jù)加載中……
    用org.apache.tools.zip壓縮/解壓縮zip文件
    寫(xiě)了一個(gè)用org.apache.tools.zip壓縮/解壓縮zip文件的例子,用來(lái)解決中文亂碼問(wèn)題。代碼如下:
    Java代碼  收藏代碼
    1. package org.coolyongzi;  
    2.   
    3. import java.io.File;  
    4. import java.io.FileInputStream;  
    5. import java.io.FileOutputStream;  
    6. import java.io.InputStream;  
    7. import org.apache.tools.zip.ZipEntry;  
    8. import org.apache.tools.zip.ZipFile;  
    9. import org.apache.tools.zip.ZipOutputStream;  
    10. import java.io.IOException;  
    11. import java.util.Enumeration;  
    12. import org.apache.log4j.LogManager;  
    13. import org.apache.log4j.Logger;  
    14.   
    15.   
    16. public class ZipTools {  
    17.     public static final Logger logger = LogManager.getLogger(FileTools.class);  
    18.     public ZipTools()  
    19.     {  
    20.           
    21.     }  
    22.     /* 
    23.      * @description:Compressed files or folders 
    24.      * @param compressedFilePath String,zipFileRootPath String,zipFileName String 
    25.      * @return boolean 
    26.      */  
    27.     public static boolean compressFloderChangeToZip(String compressedFilePath,String zipFileRootPath,String zipFileName)   
    28.     throws IOException  
    29.     {  
    30.         File compressedFile = new File(compressedFilePath);  
    31.         if("".equalsIgnoreCase(zipFileName))  
    32.         {  
    33.             zipFileName = StringTools.getShortFileNameFromFilePath(compressedFilePath);  
    34.         }  
    35.         if(!StringTools.conversionSpecialCharacters(zipFileRootPath).endsWith(File.separator))  
    36.         {  
    37.             zipFileRootPath = zipFileRootPath + File.separator;  
    38.         }  
    39.         ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFileRootPath + zipFileName));  
    40.         String base ="";  
    41.         logger.debug("compress [" + compressedFilePath + "] start!");  
    42.         boolean result = ZipTools.compressFloderChangeToZip(compressedFile,zipOutputStream,base);  
    43.         logger.debug("compress [" + compressedFilePath + "] end!");  
    44.         zipOutputStream.close();  
    45.         return result;  
    46.           
    47.     }  
    48.       
    49.     private static  boolean compressFloderChangeToZip(File compressedFile,ZipOutputStream zipOutputStream,String base)   
    50.     throws IOException  
    51.     {  
    52.         FileInputStream fileInputStream = null;  
    53.           
    54.         try{  
    55.             if(compressedFile.isDirectory())  
    56.             {  
    57.                 File[] childrenCompressedFileList = compressedFile.listFiles();  
    58.                 base = base.length() == 0 ? "" : base + File.separator;  
    59.                 for (int i = 0; i < childrenCompressedFileList.length; i++) {  
    60.                     ZipTools.compressFloderChangeToZip(childrenCompressedFileList[i],  
    61.                     zipOutputStream,base+childrenCompressedFileList[i].getName());  
    62.                 }  
    63.             }  
    64.             else  
    65.             {  
    66.                 if("".equalsIgnoreCase(base))  
    67.                 {  
    68.                     base = compressedFile.getName();  
    69.                 }  
    70.                 zipOutputStream.putNextEntry(new ZipEntry(base));  
    71.                 fileInputStream = new FileInputStream(compressedFile);  
    72.                 int b;  
    73.                 while((b=fileInputStream.read())!=-1)  
    74.                 {  
    75.                     zipOutputStream.write(b);  
    76.                 }  
    77.                 fileInputStream.close();  
    78.             }  
    79.             return true;  
    80.         }catch(Exception e)  
    81.         {  
    82.             e.getStackTrace();  
    83.             logger.error(e.getMessage());  
    84.             return false;  
    85.         }  
    86.     }  
    87.     /* 
    88.      * @param:zipFilePath String,releasePath String 
    89.      * @return void 
    90.      * @description:Decompress A File 
    91.      */  
    92.     @SuppressWarnings("unchecked")  
    93.     public static void decompressFile(String zipFilePath,String releasePath) throws IOException  
    94.     {  
    95.         ZipFile zipFile = new ZipFile(zipFilePath);  
    96.         Enumeration<ZipEntry> enumeration = zipFile.getEntries();  
    97.         InputStream inputStream = null;  
    98.         FileOutputStream fileOutputStream = null;  
    99.         ZipEntry zipEntry = null;  
    100.         String zipEntryNameStr ="";  
    101.         String[] zipEntryNameArray = null;  
    102.         while (enumeration.hasMoreElements()) {  
    103.             zipEntry = enumeration.nextElement();  
    104.             zipEntryNameStr = zipEntry.getName();  
    105.             zipEntryNameArray = zipEntryNameStr.split("/");  
    106.             String path = releasePath;  
    107.             File root = new File(releasePath);  
    108.             if(!root.exists())  
    109.             {  
    110.                 root.mkdir();  
    111.             }  
    112.             for (int i = 0; i < zipEntryNameArray.length; i++) {  
    113.                 if(i<zipEntryNameArray.length-1)  
    114.                 {  
    115.                     path = path + File.separator+zipEntryNameArray[i];        
    116.                     new File(StringTools.conversionSpecialCharacters(path)).mkdir();  
    117.                 }                 
    118.                 else  
    119.                 {  
    120.                     if(StringTools.conversionSpecialCharacters(zipEntryNameStr).endsWith(File.separator))  
    121.                     {  
    122.                         new File(releasePath + zipEntryNameStr).mkdir();  
    123.                     }  
    124.                     else  
    125.                     {  
    126.                         inputStream = zipFile.getInputStream(zipEntry);  
    127.                         fileOutputStream = new FileOutputStream(new File(  
    128.                                 StringTools.conversionSpecialCharacters(releasePath + zipEntryNameStr)));     
    129.                         byte[] buf = new byte[1024];  
    130.                         int len;  
    131.                         while ((len = inputStream.read(buf)) > 0)  
    132.                         {  
    133.                             fileOutputStream.write(buf, 0, len);  
    134.                         }  
    135.                         inputStream.close();  
    136.                         fileOutputStream.close();  
    137.                     }  
    138.                 }  
    139.             }  
    140.         }  
    141.         zipFile.close();  
    142.     }  
    143. }  

    junit測(cè)試類
    Java代碼  收藏代碼
    1. package org.coolyongzi.testcase;  
    2.   
    3. import java.io.IOException;  
    4.   
    5. import org.coolyongzi.ZipTools;  
    6.   
    7. import junit.framework.TestCase;  
    8.   
    9. public class ZipToolsTest extends TestCase {  
    10.   
    11.     protected void setUp() throws Exception {  
    12.         super.setUp();  
    13.     }  
    14.   
    15.     protected void tearDown() throws Exception {  
    16.         super.tearDown();  
    17.     }  
    18.   
    19.     public void testCompressFloderChangeToZip(){  
    20.         try {  
    21.             ZipTools.compressFloderChangeToZip("f:/iDocumentBanner2.gif", "f:", "test.zip");  
    22.         } catch (IOException e) {  
    23.             // TODO Auto-generated catch block  
    24.             e.printStackTrace();  
    25.         }  
    26.     }  
    27.       
    28.     public void testDecompressFile(){  
    29.         try {  
    30.             ZipTools.decompressFile("f:/java對(duì)解壓Zip格式的文件.zip","f:/test/");  
    31.         } catch (IOException e) {  
    32.             // TODO Auto-generated catch block  
    33.             e.printStackTrace();  
    34.             System.out.println(e.getMessage());  
    35.         }  
    36.     }  
    37. }

    posted on 2012-01-03 17:32 AthrunWang 閱讀(5272) 評(píng)論(0)  編輯  收藏


    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 永久免费视频网站在线观看| 农村寡妇一级毛片免费看视频| 四虎永久免费网站免费观看| 久久久久亚洲AV无码网站| 黄色网址在线免费| 国产亚洲成av片在线观看 | 国产aa免费视频| 亚洲国产成人AV网站| 免费在线观看亚洲| 一区二区视频免费观看| 97免费人妻在线视频| 亚洲视频精品在线观看| 很黄很黄的网站免费的| 亚洲日本乱码卡2卡3卡新区| 一级人做人爰a全过程免费视频| 免费夜色污私人影院在线观看| 一级毛片完整版免费播放一区| 亚洲国产专区一区| 香蕉大伊亚洲人在线观看| 成人免费视频试看120秒| 久久久久亚洲AV无码麻豆| 100部毛片免费全部播放完整| 久久综合久久综合亚洲| 免费一级毛片在线观看| 巨胸喷奶水视频www免费视频| 亚洲成人精品久久| 在线jyzzjyzz免费视频| 久久av无码专区亚洲av桃花岛| 中文字幕无码播放免费| 国产亚洲日韩在线a不卡| 亚洲无线码一区二区三区| 最近中文字幕国语免费完整 | 亚洲国产精品无码av| 午夜免费1000部| 污网站免费在线观看| 亚洲AV无码成人精品区在线观看| 亚洲人成网站在线在线观看| 91精品国产免费网站| 亚洲av无码有乱码在线观看| 在线观看免费人成视频| 无人视频免费观看免费视频|