实例展示使用Java压缩和解压缩7z文件的方法
压缩为7z文件
首先网络上对7z的压缩内容很少。
尤其是java调用进行压缩的是更少了。
一下是自己完成的一个压缩。
本人进行了测试是成功的。
将压缩的流写如磁盘一个压缩文件中。
然后使用7z的压缩软件进行打开解压。
7-zip的开源项目7-zip-JBinding项目地址(sourceforge)
不多说,调用7z源码进行压缩的方法如下。
publicbyte[]lzmaZip(Stringxml)throwsIOException{ BufferedInputStreaminStream=newBufferedInputStream(newByteArrayInputStream(xml.getBytes())); ByteArrayOutputStreambos=newByteArrayOutputStream(); booleaneos=true; Encoderencoder=newEncoder(); encoder.SetEndMarkerMode(eos); encoder.WriteCoderProperties(bos); longfileSize=xml.length(); if(eos) fileSize=-1; for(inti=0;i<8;i++) bos.write((int)(fileSize>>>(8*i))&0xFF); encoder.Code(inStream,bos,-1,-1,null); returnbos.toByteArray(); }
解压缩7z文件
利用7-zip的开源项目7-zip-JBinding来解压缩多种压缩文件,而不是调用外部命令(比如win下调用winrar)。
java自带的解压模块可解压缩的压缩类型有限。
代码示例
packagecore; importjava.io.File; importjava.io.FileNotFoundException; importjava.io.FileOutputStream; importjava.io.IOException; importjava.io.RandomAccessFile; importjava.util.Arrays; importnet.sf.sevenzipjbinding.ExtractOperationResult; importnet.sf.sevenzipjbinding.ISequentialOutStream; importnet.sf.sevenzipjbinding.ISevenZipInArchive; importnet.sf.sevenzipjbinding.SevenZip; importnet.sf.sevenzipjbinding.SevenZipException; importnet.sf.sevenzipjbinding.impl.RandomAccessFileInStream; importnet.sf.sevenzipjbinding.simple.ISimpleInArchive; importnet.sf.sevenzipjbinding.simple.ISimpleInArchiveItem; /**利用7zbinding*/ publicclassUnZip{ voidextractile(Stringfilepath){ RandomAccessFilerandomAccessFile=null; ISevenZipInArchiveinArchive=null; try{ randomAccessFile=newRandomAccessFile(filepath,"r"); inArchive=SevenZip.openInArchive(null,//autodetectarchivetype newRandomAccessFileInStream(randomAccessFile)); //GettingsimpleinterfaceofthearchiveinArchive ISimpleInArchivesimpleInArchive=inArchive.getSimpleInterface(); System.out.println("Hash|Size|Filename"); System.out.println("----------+------------+---------"); for(finalISimpleInArchiveItemitem:simpleInArchive.getArchiveItems()){ finalint[]hash=newint[]{0}; if(!item.isFolder()){ ExtractOperationResultresult; finallong[]sizeArray=newlong[1]; result=item.extractSlow(newISequentialOutStream(){ publicintwrite(byte[]data)throwsSevenZipException{ //Writetofile FileOutputStreamfos; try{ Filefile=newFile(item.getPath()); //erroroccoursbelow //file.getParentFile().mkdirs(); fos=newFileOutputStream(file); fos.write(data); fos.close(); }catch(FileNotFoundExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); }catch(IOExceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } hash[0]^=Arrays.hashCode(data);//Consumedata sizeArray[0]+=data.length; returndata.length;//Returnamountofconsumeddata } }); if(result==ExtractOperationResult.OK){ System.out.println(String.format("%9X|%10s|%s",// hash[0],sizeArray[0],item.getPath())); }else{ System.err.println("Errorextractingitem:"+result); } } } }catch(Exceptione){ System.err.println("Erroroccurs:"+e); e.printStackTrace(); System.exit(1); }finally{ if(inArchive!=null){ try{ inArchive.close(); }catch(SevenZipExceptione){ System.err.println("Errorclosingarchive:"+e); } } if(randomAccessFile!=null){ try{ randomAccessFile.close(); }catch(IOExceptione){ System.err.println("Errorclosingfile:"+e); } } } } }
调用的时候:
unzip=newUnZip(); unzip.extractile("a.7z");
会自动解压缩压缩包里的文件到当前目录下,当然可以更改设置,到特定的目录。代码简单明确。有问题可以到上面的sourceforge项目地址下的discuss搜索。