日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java压缩技术(三) ZIP解压缩——Java原生实现

發(fā)布時間:2023/12/3 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java压缩技术(三) ZIP解压缩——Java原生实现 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

轉(zhuǎn)載自? ?Java壓縮技術(shù)(三) ZIP解壓縮——Java原生實現(xiàn)

解壓縮與壓縮運作方式相反,原理大抵相同,由ZipInputStream通過read方法對數(shù)據(jù)解壓,同時需要通過CheckedInputStream設(shè)置冗余校驗碼,如:?

Java代碼??
  • CheckedInputStream?cis?=?new?CheckedInputStream(new?FileInputStream(??
  • ????????srcFile),?new?CRC32());??
  • ??
  • ZipInputStream?zis?=?new?ZipInputStream(cis);??

  • 需要注意的是,在構(gòu)建解壓文件時,需要考慮目錄的自動創(chuàng)建,這里通過遞歸方式逐層創(chuàng)建父目錄,如下所示:?
    Java代碼??
  • /**?
  • ?*?文件探針?
  • ?*??
  • ?*??
  • ?*?當(dāng)父目錄不存在時,創(chuàng)建目錄!?
  • ?*??
  • ?*??
  • ?*?@param?dirFile?
  • ?*/??
  • private?static?void?fileProber(File?dirFile)?{??
  • ??
  • ????File?parentFile?=?dirFile.getParentFile();??
  • ????if?(!parentFile.exists())?{??
  • ??
  • ????????//?遞歸尋找上級目錄??
  • ????????fileProber(parentFile);??
  • ??
  • ????????parentFile.mkdir();??
  • ????}??
  • ??
  • }??

  • 在壓縮的時候,我們是將一個一個文件作為壓縮添加項(ZipEntry)添加至壓縮包中,解壓縮就要將一個一個壓縮項從壓縮包中提取出來,如下所示:?
    Java代碼??
  • /**?
  • ?*?文件?解壓縮?
  • ?*??
  • ?*?@param?destFile?
  • ?*????????????目標(biāo)文件?
  • ?*?@param?zis?
  • ?*????????????ZipInputStream?
  • ?*?@throws?Exception?
  • ?*/??
  • private?static?void?decompress(File?destFile,?ZipInputStream?zis)??
  • ????????throws?Exception?{??
  • ??
  • ????ZipEntry?entry?=?null;??
  • ????while?((entry?=?zis.getNextEntry())?!=?null)?{??
  • ??
  • ????????//?文件??
  • ????????String?dir?=?destFile.getPath()?+?File.separator?+?entry.getName();??
  • ??
  • ????????File?dirFile?=?new?File(dir);??
  • ??
  • ????????//?文件檢查??
  • ????????fileProber(dirFile);??
  • ??
  • ????????????if?(entry.isDirectory()){??
  • ????????????????dirFile.mkdirs();??
  • ????????????}?else?{??
  • ????????????decompressFile(dirFile,?zis);??
  • ????????????}??
  • ??
  • ????????????zis.closeEntry();??
  • ????}??
  • }??

  • 最核心的解壓縮實現(xiàn),其實與壓縮實現(xiàn)非常相似,代碼如下所示:?
    Java代碼??
  • /**?
  • ?*?文件解壓縮?
  • ?*??
  • ?*?@param?destFile?
  • ?*????????????目標(biāo)文件?
  • ?*?@param?zis?
  • ?*????????????ZipInputStream?
  • ?*?@throws?Exception?
  • ?*/??
  • private?static?void?decompressFile(File?destFile,?ZipInputStream?zis)??
  • ????????throws?Exception?{??
  • ??
  • ????BufferedOutputStream?bos?=?new?BufferedOutputStream(??
  • ????????????new?FileOutputStream(destFile));??
  • ??
  • ????int?count;??
  • ????byte?data[]?=?new?byte[BUFFER];??
  • ????while?((count?=?zis.read(data,?0,?BUFFER))?!=?-1)?{??
  • ????????bos.write(data,?0,?count);??
  • ????}??
  • ??
  • ????bos.close();??
  • }??

  • 來個完整的解壓縮實現(xiàn),代碼如下:?
    Java代碼??
  • /**?
  • ?*?2010-4-12?
  • ?*/??
  • package?org.zlex.commons.io;??
  • ??
  • import?java.io.BufferedInputStream;??
  • import?java.io.BufferedOutputStream;??
  • import?java.io.File;??
  • import?java.io.FileInputStream;??
  • import?java.io.FileOutputStream;??
  • import?java.util.zip.CRC32;??
  • import?java.util.zip.CheckedInputStream;??
  • import?java.util.zip.CheckedOutputStream;??
  • import?java.util.zip.ZipEntry;??
  • import?java.util.zip.ZipInputStream;??
  • import?java.util.zip.ZipOutputStream;??
  • ??
  • /**?
  • ?*?ZIP壓縮工具?
  • ?*??
  • ?*?@author?梁棟????
  • ?*?@since?1.0?
  • ?*/??
  • public?class?ZipUtils?{??
  • ??
  • ????public?static?final?String?EXT?=?".zip";??
  • ????private?static?final?String?BASE_DIR?=?"";??
  • ????private?static?final?String?PATH?=?File.separator;??
  • ????private?static?final?int?BUFFER?=?1024;??
  • ??
  • ????/**?
  • ?????*?文件?解壓縮?
  • ?????*??
  • ?????*?@param?srcPath?
  • ?????*????????????源文件路徑?
  • ?????*??
  • ?????*?@throws?Exception?
  • ?????*/??
  • ????public?static?void?decompress(String?srcPath)?throws?Exception?{??
  • ????????File?srcFile?=?new?File(srcPath);??
  • ??
  • ????????decompress(srcFile);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?解壓縮?
  • ?????*??
  • ?????*?@param?srcFile?
  • ?????*?@throws?Exception?
  • ?????*/??
  • ????public?static?void?decompress(File?srcFile)?throws?Exception?{??
  • ????????String?basePath?=?srcFile.getParent();??
  • ????????decompress(srcFile,?basePath);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?解壓縮?
  • ?????*??
  • ?????*?@param?srcFile?
  • ?????*?@param?destFile?
  • ?????*?@throws?Exception?
  • ?????*/??
  • ????public?static?void?decompress(File?srcFile,?File?destFile)?throws?Exception?{??
  • ??
  • ????????CheckedInputStream?cis?=?new?CheckedInputStream(new?FileInputStream(??
  • ????????????????srcFile),?new?CRC32());??
  • ??
  • ????????ZipInputStream?zis?=?new?ZipInputStream(cis);??
  • ??
  • ????????decompress(destFile,?zis);??
  • ??
  • ????????zis.close();??
  • ??
  • ????}??
  • ??
  • ????/**?
  • ?????*?解壓縮?
  • ?????*??
  • ?????*?@param?srcFile?
  • ?????*?@param?destPath?
  • ?????*?@throws?Exception?
  • ?????*/??
  • ????public?static?void?decompress(File?srcFile,?String?destPath)??
  • ????????????throws?Exception?{??
  • ????????decompress(srcFile,?new?File(destPath));??
  • ??
  • ????}??
  • ??
  • ????/**?
  • ?????*?文件?解壓縮?
  • ?????*??
  • ?????*?@param?srcPath?
  • ?????*????????????源文件路徑?
  • ?????*?@param?destPath?
  • ?????*????????????目標(biāo)文件路徑?
  • ?????*?@throws?Exception?
  • ?????*/??
  • ????public?static?void?decompress(String?srcPath,?String?destPath)??
  • ????????????throws?Exception?{??
  • ??
  • ????????File?srcFile?=?new?File(srcPath);??
  • ????????decompress(srcFile,?destPath);??
  • ????}??
  • ??
  • ????/**?
  • ?????*?文件?解壓縮?
  • ?????*??
  • ?????*?@param?destFile?
  • ?????*????????????目標(biāo)文件?
  • ?????*?@param?zis?
  • ?????*????????????ZipInputStream?
  • ?????*?@throws?Exception?
  • ?????*/??
  • ????private?static?void?decompress(File?destFile,?ZipInputStream?zis)??
  • ????????????throws?Exception?{??
  • ??
  • ????????ZipEntry?entry?=?null;??
  • ????????while?((entry?=?zis.getNextEntry())?!=?null)?{??
  • ??
  • ????????????//?文件??
  • ????????????String?dir?=?destFile.getPath()?+?File.separator?+?entry.getName();??
  • ??
  • ????????????File?dirFile?=?new?File(dir);??
  • ??
  • ????????????//?文件檢查??
  • ????????????fileProber(dirFile);??
  • ??
  • ????????????if?(entry.isDirectory())?{??
  • ????????????????dirFile.mkdirs();??
  • ????????????}?else?{??
  • ????????????????decompressFile(dirFile,?zis);??
  • ????????????}??
  • ??
  • ????????????zis.closeEntry();??
  • ????????}??
  • ????}??
  • ??
  • ????/**?
  • ?????*?文件探針?
  • ?????*??
  • ?????*??
  • ?????*?當(dāng)父目錄不存在時,創(chuàng)建目錄!?
  • ?????*??
  • ?????*??
  • ?????*?@param?dirFile?
  • ?????*/??
  • ????private?static?void?fileProber(File?dirFile)?{??
  • ??
  • ????????File?parentFile?=?dirFile.getParentFile();??
  • ????????if?(!parentFile.exists())?{??
  • ??
  • ????????????//?遞歸尋找上級目錄??
  • ????????????fileProber(parentFile);??
  • ??
  • ????????????parentFile.mkdir();??
  • ????????}??
  • ??
  • ????}??
  • ??
  • ????/**?
  • ?????*?文件解壓縮?
  • ?????*??
  • ?????*?@param?destFile?
  • ?????*????????????目標(biāo)文件?
  • ?????*?@param?zis?
  • ?????*????????????ZipInputStream?
  • ?????*?@throws?Exception?
  • ?????*/??
  • ????private?static?void?decompressFile(File?destFile,?ZipInputStream?zis)??
  • ????????????throws?Exception?{??
  • ??
  • ????????BufferedOutputStream?bos?=?new?BufferedOutputStream(??
  • ????????????????new?FileOutputStream(destFile));??
  • ??
  • ????????int?count;??
  • ????????byte?data[]?=?new?byte[BUFFER];??
  • ????????while?((count?=?zis.read(data,?0,?BUFFER))?!=?-1)?{??
  • ????????????bos.write(data,?0,?count);??
  • ????????}??
  • ??
  • ????????bos.close();??
  • ????}??
  • ??
  • }??
  • 其實,理解了ZIP的工作原理,這些代碼看起來很好懂! ?
    把剛才做的壓縮文件再用上述代碼解開看看,測試用例如下:?
    Java代碼??
  • /**?
  • ?*?2010-4-12?
  • ?*/??
  • package?org.zlex.commons.io;??
  • ??
  • import?static?org.junit.Assert.*;??
  • ??
  • import?org.junit.Test;??
  • ??
  • /**?
  • ?*??
  • ?*?@author?梁棟?
  • ?*?@version?1.0?
  • ?*?@since?1.0?
  • ?*/??
  • public?class?ZipUtilsTest?{??
  • ??
  • ????/**?
  • ?????*???
  • ?????*/??
  • ????@Test??
  • ????public?void?test()?throws?Exception?{??
  • ????????//?解壓到指定目錄??
  • ????????ZipUtils.decompress("d:\\f.txt.zip",?"d:\\ff");??
  • ????????//?解壓到當(dāng)前目錄??
  • ????????ZipUtils.decompress("d:\\fd.zip");??
  • ????}??
  • ??
  • }??
  • 完整代碼詳見附件!
    java原生的ZIP實現(xiàn)雖然在壓縮時會因與系統(tǒng)字符集不符產(chǎn)生中文亂碼,但在解壓縮后,字符集即可恢復(fù)。?

    除了java原生的ZIP實現(xiàn)外,commons和ant也提供了相應(yīng)的ZIP算法實現(xiàn),有機會我再一一介紹!


    • ZipUtils.rar?(2.1 KB)
    • 下載次數(shù): 526


    總結(jié)

    以上是生活随笔為你收集整理的Java压缩技术(三) ZIP解压缩——Java原生实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。