admin管理员组文章数量:1516870
在我的前面博客中总结了一个压缩,解压缩的工具类, 。但是针对.7z格式的压缩包,我们用的这两个开源包:
<dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding</artifactId><version>9.20-2.00beta</version></dependency><dependency><groupId>net.sf.sevenzipjbinding</groupId><artifactId>sevenzipjbinding-all-platforms</artifactId><version>9.20-2.00beta</version></dependency>
在我自己的本机window上是可以成功解压.7z格式的,但是放在我们的linux服务器报错了,而且更严重是这种错误直接就把我们服务器上的tomcat给关了。。。很是郁闷,在网上找了一下,也没找到怎么处理这个。
先把报错贴一下:
所以不得已找其他的7z的解压方法,在apache的common-compress提供了解压缩7z的方法。
请参考:
。
在maven项目中引入下面两个jar包,否则自己下载下面这两个jar包放到自己的java项目中即可。
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-compress</artifactId><version>1.9</version></dependency><dependency><groupId>org.tukaani</groupId><artifactId>xz</artifactId><version>1.5</version></dependency>代码:
/**
* @author kxl
* @param orgPath 源压缩文件地址
* @param tarpath 解压后存放的目录地址
*/publicstaticvoidapache7ZDecomp(String orgPath, String tarpath) {
try {
SevenZFile sevenZFile = new SevenZFile(new File(orgPath));
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry != null) {
// System.out.println(entry.getName());if (entry.isDirectory()) {
new File(tarpath + entry.getName()).mkdirs();
entry = sevenZFile.getNextEntry();
continue;
}
FileOutputStream out = new FileOutputStream(tarpath
+ File.separator + entry.getName());
byte[] content = newbyte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
out.close();
entry = sevenZFile.getNextEntry();
}
sevenZFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}调用apache7ZDecomp(“C:\Users\Administrator\Desktop\图片\图片.7z”,”C:\Users\Administrator\Desktop\图片\”);
版权声明:本文标题:轻松用Java解锁7zip压缩包的神秘——SevenZFile指南。 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1770943733a3260280.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论