首页 > 开发 > Java > 正文

实例展示使用Java压缩和解压缩7z文件的方法

2024-07-13 09:56:01
字体:
来源:转载
供稿:网友

这篇文章主要介绍了实例展示使用Java压缩和解压缩7z文件的方法,用到了7-zip的开源项目7-zip-JBinding,需要的朋友可以参考下

压缩为7z文件

首先网络上对7z的压缩内容很少。

尤其是java调用进行压缩的是更少了。

一下是自己完成的一个压缩。

本人进行了测试是成功的。

将压缩的流写如磁盘一个压缩文件中。

然后使用7z的压缩软件进行打开解压。

7-zip的开源项目7-zip-JBinding项目地址(sourceforge)

不多说,调用7z源码进行压缩的方法如下。

 

 
  1. public byte[] lzmaZip(String xml) throws IOException{  
  2. BufferedInputStream inStream = new BufferedInputStream(new ByteArrayInputStream(xml.getBytes()));  
  3. ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  4.  
  5. boolean eos = true;  
  6. Encoder encoder = new Encoder();  
  7. encoder.SetEndMarkerMode(eos);  
  8. encoder.WriteCoderProperties(bos);  
  9. long fileSize = xml.length();  
  10. if (eos)  
  11. fileSize = -1;  
  12. for (int i = 0; i < 8; i++)  
  13. bos.write((int)(fileSize >>> (8 * i)) & 0xFF);  
  14. encoder.Code(inStream, bos, -1, -1, null);  
  15. return bos.toByteArray() ;  
  16. }  

解压缩7z文件

利用7-zip的开源项目7-zip-JBinding来解压缩多种压缩文件,而不是调用外部命令(比如win下调用winrar)。

java自带的解压模块可解压缩的压缩类型有限。

代码示例

 

 
  1. package core; 
  2.  
  3. import java.io.File; 
  4. import java.io.FileNotFoundException; 
  5. import java.io.FileOutputStream; 
  6. import java.io.IOException; 
  7. import java.io.RandomAccessFile; 
  8. import java.util.Arrays; 
  9.  
  10. import net.sf.sevenzipjbinding.ExtractOperationResult; 
  11. import net.sf.sevenzipjbinding.ISequentialOutStream; 
  12. import net.sf.sevenzipjbinding.ISevenZipInArchive; 
  13. import net.sf.sevenzipjbinding.SevenZip; 
  14. import net.sf.sevenzipjbinding.SevenZipException; 
  15. import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; 
  16. import net.sf.sevenzipjbinding.simple.ISimpleInArchive; 
  17. import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem; 
  18. /**利用7zbinding*/ 
  19. public class UnZip { 
  20.  
  21.  
  22. void extractile(String filepath){ 
  23. RandomAccessFile randomAccessFile = null
  24. ISevenZipInArchive inArchive = null
  25.  
  26. try { 
  27. randomAccessFile = new RandomAccessFile(filepath, "r"); 
  28. inArchive = SevenZip.openInArchive(null// autodetect archive type 
  29. new RandomAccessFileInStream(randomAccessFile)); 
  30.  
  31. // Getting simple interface of the archive inArchive 
  32. ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface(); 
  33.  
  34. System.out.println(" Hash | Size | Filename"); 
  35. System.out.println("----------+------------+---------"); 
  36.  
  37. for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { 
  38. final int[] hash = new int[] { 0 }; 
  39. if (!item.isFolder()) { 
  40. ExtractOperationResult result; 
  41.  
  42. final long[] sizeArray = new long[1]; 
  43. result = item.extractSlow(new ISequentialOutStream() { 
  44. public int write(byte[] data) throws SevenZipException { 
  45.  
  46. //Write to file 
  47. FileOutputStream fos; 
  48. try { 
  49. File file = new File(item.getPath()); 
  50. //error occours below 
  51. // file.getParentFile().mkdirs(); 
  52. fos = new FileOutputStream(file); 
  53. fos.write(data); 
  54. fos.close(); 
  55.  
  56. catch (FileNotFoundException e) { 
  57. // TODO Auto-generated catch block 
  58. e.printStackTrace(); 
  59. catch (IOException e) { 
  60. // TODO Auto-generated catch block 
  61. e.printStackTrace(); 
  62.  
  63. hash[0] ^= Arrays.hashCode(data); // Consume data 
  64. sizeArray[0] += data.length; 
  65. return data.length; // Return amount of consumed data 
  66. }); 
  67. if (result == ExtractOperationResult.OK) { 
  68. System.out.println(String.format("%9X | %10s | %s"//  
  69. hash[0], sizeArray[0], item.getPath())); 
  70. else { 
  71. System.err.println("Error extracting item: " + result); 
  72. catch (Exception e) { 
  73. System.err.println("Error occurs: " + e); 
  74. e.printStackTrace(); 
  75. System.exit(1); 
  76. finally { 
  77. if (inArchive != null) { 
  78. try { 
  79. inArchive.close(); 
  80. catch (SevenZipException e) { 
  81. System.err.println("Error closing archive: " + e); 
  82. if (randomAccessFile != null) { 
  83. try { 
  84. randomAccessFile.close(); 
  85. catch (IOException e) { 
  86. System.err.println("Error closing file: " + e); 

调用的时候:

 

 
  1. unzip=new UnZip(); 
  2. unzip.extractile("a.7z"); 

会自动解压缩压缩包里的文件到当前目录下,当然可以更改设置,到特定的目录。代码简单明确。有问题可以到上面的sourceforge项目地址下的discuss搜索。


注:相关教程知识阅读请移步到JAVA教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表