java 解压7z_实例展示使用Java压缩和解压缩7z文件的方法
压缩为7z文件首先网络上对7z的压缩内容很少。
尤其是java调用进行压缩的是更少了。
一下是自己完成的一个压缩。
本人进行了测试是成功的。
将压缩的流写如磁盘一个压缩文件中。
然后使用7z的压缩软件进行打开解压。
7-zip的开源项目7-zip-JBinding项目地址(sourceforge)
不多说,调用7z源码进行压缩的方法如下。
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的开源项目7-zip-JBinding来解压缩多种压缩文件,而不是调用外部命令(比如win下调用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);
}
}
}
}
}
调用的时候:
unzip=new UnZip();
unzip.extractile("a.7z");
会自动解压缩压缩包里的文件到当前目录下,当然可以更改设置,到特定的目录。代码简单明确。有问题可以到上面的sourceforge项目地址下的discuss搜索。
java 解压7z_实例展示使用Java压缩和解压缩7z文件的方法相关推荐
- java解压出来损坏_在Java中解压缩错误
嗨,我是zip格式的新手,我使用Java的util实现来解压缩文件,但是每当我尝试打开文件时它都会抛出一个ZipException . 我检查文件是否已损坏,但不是因为我可以使用winRar打开它 . ...
- java解压多个zip_使用Java解压缩多部分zip文件卷
尝试将所有文件连接到一个文件中,然后提取单个文件.就像是: File dir = new File("D:/arc"); FileOutputStream fos = new ...
- java ant解压缩_java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解
java ant包中的org.apache.tools.zip实现压缩和解压缩实例详解 发布于 2020-4-7| 复制链接 摘记: java ant包中的org.apache.tools.zip实现 ...
- linux环境JAVA解压zip_Linux文件解压缩详解
tar命令 我们知道在Windows下最常见的压缩文件就只有两种,一是,zip,另一个是.rar.可是Linux就不同了,它有.gz..tar.gz.tgz.bz2..Z..tar等众多的压缩文件名, ...
- java 解压与压缩代码_Java实现多文件压缩和解压缩代码详解
Java实现多文件压缩和解压缩代码 import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStre ...
- gz解压java,java 解压gz
场景: java解压gz文件,这个是在linux下实验过的 下面是网上的代码 http://www.iteye.com/topic/894879 import java.io.FileInputStr ...
- java zip malformed_关于Java解压文件的一些坑及经验分享(MALFORMED异常)
关于Java解压文件的一些坑及经验分享 就在本周, 测试人员找到我说现上的需求文档(zip格式的)无法预览了, 让我帮忙看看怎么回事. 这个功能也并不是我做的, 于是我便先看看线上日志有没有什么错误, ...
- java解压rar5 兼容rar4(改bug)
本篇是为修改原作者bug,原作者:java解压rar5 兼容rar4_So straw?-CSDN博客 在我测试时发现bug,此方法 public int write(byte[] data) thr ...
- Java解压Jar文件
今天写点java解压jar文件的东西,以前项目中用到过,很简单... java中有专门的文件类型对应jar文件,那就是JarFile,用于从任何可以使用java.io.RandomAccessFile ...
- java解压报错java.io.IOException: failed to skip current tar entry
#java解压出现java.io.IOException: failed to skip current tar entry 当使用如下函数解压: AntBuilder antBuilder = ne ...
最新文章
- 服务器端会话技术Session|| Session的原理||Session的细节||session的钝化session的活化||session的特点||session与Cookie的区别
- java线程等待都完成_Java等待线程完成
- Spring Boot 2.x(十五):Dubbo + Zookeeper + Dubbo Admin
- 把VOC数据集转化成txt文件python
- 大一微积分笔记整理_大一高数笔记.doc
- SAP License:作业费用分割均分常见原因
- PHP上传的文件权限不足,上传文件的PHP脚本不工作的问题(目录权限问题)php-fpm+nginx...
- 永久且免费的CRM系统排行
- 教授专栏14 | 陈泰元: 提升公司治理之路--高管薪酬追回条款
- 2020 android平板推荐,2020年2000元左右的平板哪一款好?2000元左右的平板推荐
- 程序员的开发工具:Java语言开发人员常用软件
- Freda的访客 【找规律+快速幂】
- Python如何读取STL文件,生成STL文件预览图(缩略图)
- 块矩阵(Block Matrix)、舒尔补(Schur complement)
- windows的dmp文件使用
- C语言int类型和float浮点型数据在内存中的存储方式
- 多层神经元感知器模型_使用多层感知器模型对星系进行分类
- Linux内核分析2:一个简单的时间片轮转多道程序内核代码分析
- 分析PostLateUpdate.FinishFrameRendering()。每帧渲染时间截然不同
- 《C++新经典》第1章 C/C++语言
热门文章
- Windows定时关机小程序
- 中山纪中集训Day7+8.7模拟赛题解
- 【Linux基础编程】tr命令
- python关于类、self、_init_的应用
- 思维模型 波士顿矩阵
- [translate]Multimodal Self-Paced Learning for Multi-Omics Feature Selection and Data Integration
- 英语3500词(14/20)dynasty主题 (2022.1.26)
- springboot教学工作量管理毕业设计-附源码221541
- 爬楼梯java(leetcode70)
- 人脸对齐:Wing Loss人脸关键点检测算法2018