免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Java壓縮技術(shù)(二) ZIP壓縮——Java原生實(shí)現(xiàn)
去年整理了一篇ZLib算法Java實(shí)現(xiàn)(Java壓縮技術(shù)(一) ZLib),一直惦記卻沒時(shí)間補(bǔ)充。今天得空,整理一下ZIP的java原生實(shí)現(xiàn)。
看了幾篇zip壓縮算法的帖子,講的算是比較細(xì)致了,但就是沒有對(duì)應(yīng)的解壓縮實(shí)現(xiàn),太惜敗了!
我就喜歡沒事做總結(jié),稍作整理,將其收納!


相關(guān)鏈接:
Java壓縮技術(shù)(一) ZLib
Java壓縮技術(shù)(二) ZIP壓縮——Java原生實(shí)現(xiàn)
Java壓縮技術(shù)(三) ZIP解壓縮——Java原生實(shí)現(xiàn)
Java壓縮技術(shù)(四) GZIP——Java原生實(shí)現(xiàn)
Java壓縮技術(shù)(五) GZIP相關(guān)——瀏覽器解析
Java壓縮技術(shù)(六) BZIP2——Commons實(shí)現(xiàn)
Java壓縮技術(shù)(七) TAR——Commons實(shí)現(xiàn)

查過相關(guān)資料后才知道,ZIP應(yīng)該算作歸檔類的壓縮算法,每一門學(xué)科都可深可淺!


閑言少敘,先說ZIP壓縮。
zip壓縮需要通過ZipOutputStream 執(zhí)行write方法將壓縮數(shù)據(jù)寫到指定輸出流中。
注意,這里應(yīng)先使用CheckedOutputStream 指定文件校驗(yàn)算法。(通常使用CRC32算法)。代碼如下所示:
Java代碼
 
  1. CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(destPath), new CRC32());   
  2. ZipOutputStream zos = new ZipOutputStream(cos);  

接下來,需要將待壓縮文件以ZipEntry的方式追加到壓縮文件中,如下所示:
Java代碼
 
  1.  /**  
  2.  * 壓縮包內(nèi)文件名定義  
  3.  *   
  4.  * <pre>  
  5.  * 如果有多級(jí)目錄,那么這里就需要給出包含目錄的文件名  
  6.  * 如果用WinRAR打開壓縮包,中文名將顯示為亂碼  
  7.  * </pre>  
  8.  */  
  9. ZipEntry entry = new ZipEntry(dir   file.getName());   
  10.   
  11. zos.putNextEntry(entry);  

ZipEntry就是壓縮包中的每一個(gè)實(shí)體!
完成上述準(zhǔn)備后,就可以執(zhí)行壓縮操作了。實(shí)際上,就是執(zhí)行ZipOutputStream類的write方法,如下所示:
Java代碼
 
  1. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(   
  2.         file));   
  3.   
  4. int count;   
  5. byte data[] = new byte[BUFFER];   
  6. while ((count = bis.read(data, 0, BUFFER)) != -1) {   
  7.     zos.write(data, 0, count);   
  8. }   
  9. bis.close();  

當(dāng)然,如果待添加的壓縮項(xiàng)是一個(gè)目錄。那么,需要通過遞歸的方式指定最終的壓縮項(xiàng)。
如果要添加一個(gè)空目錄,注意使用符號(hào)"/"(String PATH="/";)作為添加項(xiàng)名字結(jié)尾符!

遞歸構(gòu)建目錄壓縮,代碼如下:
Java代碼
 
  1. /**  
  2.  * 壓縮  
  3.  *   
  4.  * @param srcFile  
  5.  *            源路徑  
  6.  * @param zos  
  7.  *            ZipOutputStream  
  8.  * @param basePath  
  9.  *            壓縮包內(nèi)相對(duì)路徑  
  10.  * @throws Exception  
  11.  */  
  12. private static void compress(File srcFile, ZipOutputStream zos,   
  13.         String basePath) throws Exception {   
  14.     if (srcFile.isDirectory()) {   
  15.         compressDir(srcFile, zos, basePath);   
  16.     } else {   
  17.         compressFile(srcFile, zos, basePath);   
  18.     }   
  19. }   
  20.   
  21. /**  
  22.  * 壓縮目錄  
  23.  *   
  24.  * @param dir  
  25.  * @param zos  
  26.  * @param basePath  
  27.  * @throws Exception  
  28.  */  
  29. private static void compressDir(File dir, ZipOutputStream zos,   
  30.         String basePath) throws Exception {   
  31.   
  32.     File[] files = dir.listFiles();   
  33.   
  34.     // 構(gòu)建空目錄   
  35.     if (files.length < 1) {   
  36.         ZipEntry entry = new ZipEntry(basePath   dir.getName()   PATH);   
  37.   
  38.         zos.putNextEntry(entry);   
  39.         zos.closeEntry();   
  40.     }   
  41.   
  42.     for (File file : files) {   
  43.         // 遞歸壓縮   
  44.         compress(file, zos, basePath   dir.getName()   PATH);   
  45.     }   
  46. }  

x是一個(gè)空目錄,用WinRAR打開后,可以看到這個(gè)目錄下還有一個(gè)空文件名文件!



來個(gè)完整的壓縮實(shí)現(xiàn),代碼如下所示:
Java代碼
 
  1. /**  
  2.  * 2010-4-12  
  3.  */  
  4. package org.zlex.commons.io;   
  5.   
  6. import java.io.BufferedInputStream;   
  7. import java.io.BufferedOutputStream;   
  8. import java.io.File;   
  9. import java.io.FileInputStream;   
  10. import java.io.FileOutputStream;   
  11. import java.util.zip.CRC32;   
  12. import java.util.zip.CheckedInputStream;   
  13. import java.util.zip.CheckedOutputStream;   
  14. import java.util.zip.ZipEntry;   
  15. import java.util.zip.ZipInputStream;   
  16. import java.util.zip.ZipOutputStream;   
  17.   
  18. /**  
  19.  * ZIP壓縮工具  
  20.  *   
  21.  * @author  <a href="mailto:zlex.dongliang@gmail.com">梁棟</a>     
  22.  * @since 1.0  
  23.  */  
  24. public class ZipUtils {   
  25.   
  26.     public static final String EXT = ".zip";   
  27.     private static final String BASE_DIR = "";   
  28.   
  29.     // 符號(hào)"/"用來作為目錄標(biāo)識(shí)判斷符   
  30.     private static final String PATH = "/";   
  31.     private static final int BUFFER = 1024;   
  32.   
  33.     /**  
  34.      * 壓縮  
  35.      *   
  36.      * @param srcFile  
  37.      * @throws Exception  
  38.      */  
  39.     public static void compress(File srcFile) throws Exception {   
  40.         String name = srcFile.getName();   
  41.         String basePath = srcFile.getParent();   
  42.         String destPath = basePath   name   EXT;   
  43.         compress(srcFile, destPath);   
  44.     }   
  45.   
  46.     /**  
  47.      * 壓縮  
  48.      *   
  49.      * @param srcFile  
  50.      *            源路徑  
  51.      * @param destPath  
  52.      *            目標(biāo)路徑  
  53.      * @throws Exception  
  54.      */  
  55.     public static void compress(File srcFile, File destFile) throws Exception {   
  56.   
  57.         // 對(duì)輸出文件做CRC32校驗(yàn)   
  58.         CheckedOutputStream cos = new CheckedOutputStream(new FileOutputStream(   
  59.                 destFile), new CRC32());   
  60.   
  61.         ZipOutputStream zos = new ZipOutputStream(cos);   
  62.   
  63.         compress(srcFile, zos, BASE_DIR);   
  64.   
  65.         zos.flush();   
  66.         zos.close();   
  67.     }   
  68.   
  69.     /**  
  70.      * 壓縮文件  
  71.      *   
  72.      * @param srcFile  
  73.      * @param destPath  
  74.      * @throws Exception  
  75.      */  
  76.     public static void compress(File srcFile, String destPath) throws Exception {   
  77.         compress(srcFile, new File(destPath));   
  78.     }   
  79.   
  80.     /**  
  81.      * 壓縮  
  82.      *   
  83.      * @param srcFile  
  84.      *            源路徑  
  85.      * @param zos  
  86.      *            ZipOutputStream  
  87.      * @param basePath  
  88.      *            壓縮包內(nèi)相對(duì)路徑  
  89.      * @throws Exception  
  90.      */  
  91.     private static void compress(File srcFile, ZipOutputStream zos,   
  92.             String basePath) throws Exception {   
  93.         if (srcFile.isDirectory()) {   
  94.             compressDir(srcFile, zos, basePath);   
  95.         } else {   
  96.             compressFile(srcFile, zos, basePath);   
  97.         }   
  98.     }   
  99.   
  100.     /**  
  101.      * 壓縮  
  102.      *   
  103.      * @param srcPath  
  104.      * @throws Exception  
  105.      */  
  106.     public static void compress(String srcPath) throws Exception {   
  107.         File srcFile = new File(srcPath);   
  108.   
  109.         compress(srcFile);   
  110.     }   
  111.   
  112.     /**  
  113.      * 文件壓縮  
  114.      *   
  115.      * @param srcPath  
  116.      *            源文件路徑  
  117.      * @param destPath  
  118.      *            目標(biāo)文件路徑  
  119.      *   
  120.      */  
  121.     public static void compress(String srcPath, String destPath)   
  122.             throws Exception {   
  123.         File srcFile = new File(srcPath);   
  124.   
  125.         compress(srcFile, destPath);   
  126.     }   
  127.   
  128.     /**  
  129.      * 壓縮目錄  
  130.      *   
  131.      * @param dir  
  132.      * @param zos  
  133.      * @param basePath  
  134.      * @throws Exception  
  135.      */  
  136.     private static void compressDir(File dir, ZipOutputStream zos,   
  137.             String basePath) throws Exception {   
  138.   
  139.         File[] files = dir.listFiles();   
  140.   
  141.         // 構(gòu)建空目錄   
  142.         if (files.length < 1) {   
  143.             ZipEntry entry = new ZipEntry(basePath   dir.getName()   PATH);   
  144.   
  145.             zos.putNextEntry(entry);   
  146.             zos.closeEntry();   
  147.         }   
  148.   
  149.         for (File file : files) {   
  150.   
  151.             // 遞歸壓縮   
  152.             compress(file, zos, basePath   dir.getName()   PATH);   
  153.   
  154.         }   
  155.     }   
  156.   
  157.     /**  
  158.      * 文件壓縮  
  159.      *   
  160.      * @param file  
  161.      *            待壓縮文件  
  162.      * @param zos  
  163.      *            ZipOutputStream  
  164.      * @param dir  
  165.      *            壓縮文件中的當(dāng)前路徑  
  166.      * @throws Exception  
  167.      */  
  168.     private static void compressFile(File file, ZipOutputStream zos, String dir)   
  169.             throws Exception {   
  170.   
  171.         /**  
  172.          * 壓縮包內(nèi)文件名定義  
  173.          *   
  174.          * <pre>  
  175.          * 如果有多級(jí)目錄,那么這里就需要給出包含目錄的文件名  
  176.          * 如果用WinRAR打開壓縮包,中文名將顯示為亂碼  
  177.          * </pre>  
  178.          */  
  179.         ZipEntry entry = new ZipEntry(dir   file.getName());   
  180.   
  181.         zos.putNextEntry(entry);   
  182.   
  183.         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(   
  184.                 file));   
  185.   
  186.         int count;   
  187.         byte data[] = new byte[BUFFER];   
  188.         while ((count = bis.read(data, 0, BUFFER)) != -1) {   
  189.             zos.write(data, 0, count);   
  190.         }   
  191.         bis.close();   
  192.   
  193.         zos.closeEntry();   
  194.     }   
  195.   
  196. }  

來做個(gè)簡(jiǎn)單的測(cè)試:
Java代碼
 
  1. import static org.junit.Assert.*;   
  2.   
  3. import org.junit.Test;   
  4.   
  5. /**  
  6.  *   
  7.  * @author 梁棟  
  8.  * @version 1.0  
  9.  * @since 1.0  
  10.  */  
  11. public class ZipUtilsTest {   
  12.   
  13.     /**  
  14.      *    
  15.      */  
  16.     @Test  
  17.     public void test() throws Exception {   
  18.         // 壓縮文件   
  19.         ZipUtils.compress("d:\\f.txt");   
  20.         // 壓縮目錄   
  21.         ZipUtils.compress("d:\\fd");   
  22.     }   
  23. }  


現(xiàn)在用WinRAR打開看看,是不是效果幾乎一致?


當(dāng)然,上述代碼有所不足之處主要是中文名稱亂碼問題。用java原生ZIP實(shí)現(xiàn)壓縮后得到的壓縮包,與系統(tǒng)的字符集不同,文件/目錄名將出現(xiàn)亂碼。這是所有歸檔壓縮都會(huì)遇到的問題。對(duì)于這種問題,Commons Copress提供了解決方案!


對(duì)于解壓縮,請(qǐng)關(guān)注后續(xù)內(nèi)容!


相關(guān)鏈接:
Java壓縮技術(shù)(一) ZLib
Java壓縮技術(shù)(二) ZIP壓縮——Java原生實(shí)現(xiàn)
Java壓縮技術(shù)(三) ZIP解壓縮——Java原生實(shí)現(xiàn)
Java壓縮技術(shù)(四) GZIP——Java原生實(shí)現(xiàn)
Java壓縮技術(shù)(五) GZIP相關(guān)——瀏覽器解析
Java壓縮技術(shù)(六) BZIP2——Commons實(shí)現(xiàn)
Java壓縮技術(shù)(七) TAR——Commons實(shí)現(xiàn)
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
java實(shí)現(xiàn)zip壓縮文件
java亂碼總結(jié)
Java實(shí)現(xiàn)將文件或者文件夾壓縮成zip
java壓縮和解壓縮Zip、Jar、Gzip文件
代碼解壓和壓縮文件
Java實(shí)現(xiàn)zip壓縮多個(gè)文件下載
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服