生活随笔
收集整理的這篇文章主要介紹了
Java压缩技术(三) ZIP解压缩——Java原生实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載自? ?Java壓縮技術(三) ZIP解壓縮——Java原生實現
解壓縮與壓縮運作方式相反,原理大抵相同,由ZipInputStream通過read方法對數據解壓,同時需要通過CheckedInputStream設置冗余校驗碼,如:?
Java代碼??
CheckedInputStream?cis?=?new?CheckedInputStream(new?FileInputStream(??????????srcFile),?new?CRC32());????ZipInputStream?zis?=?new?ZipInputStream(cis);??
需要注意的是,在構建解壓文件時,需要考慮目錄的自動創建,這里通過遞歸方式逐層創建父目錄,如下所示:?
Java代碼??
??????????private?static?void?fileProber(File?dirFile)?{????????File?parentFile?=?dirFile.getParentFile();??????if?(!parentFile.exists())?{??????????????????????fileProber(parentFile);????????????parentFile.mkdir();??????}????}??
在壓縮的時候,我們是將一個一個文件作為壓縮添加項(ZipEntry)添加至壓縮包中,解壓縮就要將一個一個壓縮項從壓縮包中提取出來,如下所示:?
Java代碼??
??????????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();??????}??}??
最核心的解壓縮實現,其實與壓縮實現非常相似,代碼如下所示:?
Java代碼??
??????????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();??}??
來個完整的解壓縮實現,代碼如下:?
Java代碼??
????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;???????????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;?????????????????????public?static?void?decompress(String?srcPath)?throws?Exception?{??????????File?srcFile?=?new?File(srcPath);????????????decompress(srcFile);??????}???????????????????public?static?void?decompress(File?srcFile)?throws?Exception?{??????????String?basePath?=?srcFile.getParent();??????????decompress(srcFile,?basePath);??????}????????????????????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();????????}????????????????????public?static?void?decompress(File?srcFile,?String?destPath)??????????????throws?Exception?{??????????decompress(srcFile,?new?File(destPath));????????}??????????????????????public?static?void?decompress(String?srcPath,?String?destPath)??????????????throws?Exception?{????????????File?srcFile?=?new?File(srcPath);??????????decompress(srcFile,?destPath);??????}??????????????????????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();??????????}??????}??????????????????????private?static?void?fileProber(File?dirFile)?{????????????File?parentFile?=?dirFile.getParentFile();??????????if?(!parentFile.exists())?{??????????????????????????????fileProber(parentFile);????????????????parentFile.mkdir();??????????}????????}??????????????????????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代碼??
????package?org.zlex.commons.io;????import?static?org.junit.Assert.*;????import?org.junit.Test;???????????public?class?ZipUtilsTest?{????????????????@Test??????public?void?test()?throws?Exception?{????????????????????ZipUtils.decompress("d:\\f.txt.zip",?"d:\\ff");????????????????????ZipUtils.decompress("d:\\fd.zip");??????}????}??
完整代碼詳見附件!
java原生的ZIP實現雖然在壓縮時會因與系統字符集不符產生中文亂碼,但在解壓縮后,字符集即可恢復。?
除了java原生的ZIP實現外,commons和ant也提供了相應的ZIP算法實現,有機會我再一一介紹!
- ZipUtils.rar?(2.1 KB)
- 下載次數: 526
總結
以上是生活随笔為你收集整理的Java压缩技术(三) ZIP解压缩——Java原生实现的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。