实例展现运用Java紧缩宽和紧缩7z文件的办法

时间:2019-02-01 13:10 来源:英国365bet 作者:admin

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; 测试{ randomAccessFile = newRandomAccessFile(filepath,“r”); inArchive = SevenZip.openInArchive(null,// autodetectarchivetype newRandomAccessFileInStream(randomAccessFile)); //在文件中获取搜索的简单界面 ISimpleInArchivesimpleInArchive = inArchive.getSimpleInterface(); System.out.println(“Hash | Size | Filename”); System.out.println(“---------- + ------------ + ---------”); For(finalISimpleInArchiveItemitem:simpleInArchive.getArchiveItems()){ Finalint[]hash = newint[]{0}; 是(!Item.isFolder()){ ExtractOperationResultresult; Finallong[]sizeArray = newlong[1]; Result = item.extractSlow(newISequentialOutStream(){ Publicintwrite(byte[]data)throwsSevenZipException{ // Writetofile FileOutputStreamfos; 测试{ FileFile =新文件(item.getPath()); // errorocorre down //file.getParentFile()。mkdirs(); Fos = newFileOutputStream(file); Fos.write(数据); Fos.close(); }catch(FileNotFoundExceptione){ // TODOAuto-generatedcatchblock e.printStackTrace(); }capture(IOExceptione){ // TODOAuto-generatedcatchblock e.printStackTrace(); Hash[0]^ = Arrays.hashCode(data);// Consumedata sizeArray[0]+ = data.length; Returndata.length;// Returnamountofconsumeddata }); 是(result == ExtractOperationResult.OK){ System.out.println(String.format(“%9X |%10s |%s”,// Hash[0],sizeArray[0],item.getPath())); }else{ System.err.println(“Errorextractingitem:”+结果); }capture(Exceptione){ System.err.println(“发生错误:”+ e); e.printStackTrace(); System.exit(1); }finally{ 是(inArchive!= Null){ 测试{ inArchive.close(); }capture(SevenZipExceptione){ System.err.println(“Errorclosingarchive:”+ e); 是(randomAccessFile!= Null){ 测试{ randomAccessFile.close(); }capture(IOExceptione){ System.err.println(“Errorclosingfile:”+ e);
回到顶部