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 Bytes()));
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);
ByteArray() ;
}
解压缩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 = SimpleInterface(); System.out.println(" Hash | Size | Filename");
System.out.println("----------+------------+---------");
for (final ISimpleInArchiveItem item : ArchiveItems()) { final int[] hash = new int[] { 0 };
if (!item.isFolder()) {
ExtractOperationResult result;
final long[] sizeArray = new long[1];
result = actSlow(new ISequentialOutStream() {
public int write(byte[] data) throws SevenZipException {
//Write to file
FileOutputStream fos;
try {
File file = new Path());
//error occours below
// ParentFile().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], Path()));
} else {
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inArchive != null) {
try {
inArchive.close();
} catch (SevenZipException e) {
}
}
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
}
}
}
}
}
调⽤的时候:
jfinal项目实例
unzip=new UnZip();
会⾃动解压缩压缩包⾥的⽂件到当前⽬录下,当然可以更改设置,到特定的⽬录。代码简单明确。有问题可以到上⾯的sourceforge项⽬地址下的discuss搜索。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。