實(shí)例展示使用Java壓縮和解壓縮7z文件的方法
壓縮為7z文件
首先網(wǎng)絡(luò)上對(duì)7z的壓縮內(nèi)容很少。
尤其是java調(diào)用進(jìn)行壓縮的是更少了。
一下是自己完成的一個(gè)壓縮。
本人進(jìn)行了測(cè)試是成功的。
將壓縮的流寫如磁盤一個(gè)壓縮文件中。
然后使用7z的壓縮軟件進(jìn)行打開(kāi)解壓。
7-zip的開(kāi)源項(xiàng)目7-zip-JBinding項(xiàng)目地址(sourceforge)
不多說(shuō),調(diào)用7z源碼進(jìn)行壓縮的方法如下。
public byte[] lzmaZip(String xml) throws IOException{ BufferedInputStream inStream = new BufferedInputStream(new ByteArrayInputStream(xml.getBytes())); ByteArrayOutputStream bos = new ByteArrayOutputStream(); boolean eos = true; Encoder encoder = new Encoder(); encoder.SetEndMarkerMode(eos); encoder.WriteCoderProperties(bos); long fileSize = xml.length(); if (eos) fileSize = -1; for (int i = 0; i < 8; i++) bos.write((int)(fileSize >>> (8 * i)) & 0xFF); encoder.Code(inStream, bos, -1, -1, null); return bos.toByteArray() ; }
解壓縮7z文件
利用7-zip的開(kāi)源項(xiàng)目7-zip-JBinding來(lái)解壓縮多種壓縮文件,而不是調(diào)用外部命令(比如win下調(diào)用winrar)。
java自帶的解壓模塊可解壓縮的壓縮類型有限。
代碼示例
package core; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.Arrays; import net.sf.sevenzipjbinding.ExtractOperationResult; import net.sf.sevenzipjbinding.ISequentialOutStream; import net.sf.sevenzipjbinding.ISevenZipInArchive; import net.sf.sevenzipjbinding.SevenZip; import net.sf.sevenzipjbinding.SevenZipException; import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; import net.sf.sevenzipjbinding.simple.ISimpleInArchive; import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem; /**利用7zbinding*/ public class UnZip { void extractile(String filepath){ RandomAccessFile randomAccessFile = null; ISevenZipInArchive inArchive = null; try { randomAccessFile = new RandomAccessFile(filepath, "r"); inArchive = SevenZip.openInArchive(null, // autodetect archive type new RandomAccessFileInStream(randomAccessFile)); // Getting simple interface of the archive inArchive ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface(); System.out.println(" Hash | Size | Filename"); System.out.println("----------+------------+---------"); for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { final int[] hash = new int[] { 0 }; if (!item.isFolder()) { ExtractOperationResult result; final long[] sizeArray = new long[1]; result = item.extractSlow(new ISequentialOutStream() { public int write(byte[] data) throws SevenZipException { //Write to file FileOutputStream fos; try { File file = new File(item.getPath()); //error occours below // file.getParentFile().mkdirs(); fos = new FileOutputStream(file); fos.write(data); fos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } hash[0] ^= Arrays.hashCode(data); // Consume data sizeArray[0] += data.length; return data.length; // Return amount of consumed data } }); if (result == ExtractOperationResult.OK) { System.out.println(String.format("%9X | %10s | %s", // hash[0], sizeArray[0], item.getPath())); } else { System.err.println("Error extracting item: " + result); } } } } catch (Exception e) { System.err.println("Error occurs: " + e); e.printStackTrace(); System.exit(1); } finally { if (inArchive != null) { try { inArchive.close(); } catch (SevenZipException e) { System.err.println("Error closing archive: " + e); } } if (randomAccessFile != null) { try { randomAccessFile.close(); } catch (IOException e) { System.err.println("Error closing file: " + e); } } } } }
調(diào)用的時(shí)候:
unzip=new UnZip(); unzip.extractile("a.7z");
會(huì)自動(dòng)解壓縮壓縮包里的文件到當(dāng)前目錄下,當(dāng)然可以更改設(shè)置,到特定的目錄。代碼簡(jiǎn)單明確。有問(wèn)題可以到上面的sourceforge項(xiàng)目地址下的discuss搜索。
相關(guān)文章
Java Socket設(shè)置timeout的幾種常用方式說(shuō)明
這篇文章主要介紹了Java Socket設(shè)置timeout的幾種常用方式說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11推薦一款I(lǐng)ntelliJ IDEA提示快捷鍵的Key Promoter X插件
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA提示快捷鍵的Key Promoter X插件,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)
這篇文章主要介紹了Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02intellij idea查看方法被哪些類引用過(guò)(推薦)
這篇文章主要介紹了intellij idea查看方法被哪些類引用過(guò),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08Spring Security驗(yàn)證流程剖析及自定義驗(yàn)證方法
Spring Security是一個(gè)能夠?yàn)榛赟pring的企業(yè)應(yīng)用系統(tǒng)提供聲明式的安全訪問(wèn)控制解決方案的安全框架。這篇文章主要介紹了Spring Security驗(yàn)證流程剖析及自定義驗(yàn)證方法,需要的朋友可以參考下2018-03-03Java 語(yǔ)言實(shí)現(xiàn)清除帶 html 標(biāo)簽的內(nèi)容方法
下面小編就為大家?guī)?lái)一篇Java 語(yǔ)言實(shí)現(xiàn)清除帶 html 標(biāo)簽的內(nèi)容方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02MyBatis_Generator插件的安裝以及簡(jiǎn)單使用方法(圖解)
下面小編就為大家?guī)?lái)一篇MyBatis_Generator插件的安裝以及簡(jiǎn)單使用方法(圖解)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05