java--SevenZIP解压
2016-12-21 21:19
274 查看
最近用java解压7zip压缩文件,程序在Windows运行良好,在Linux服务器上解压却出现问题,将代码中SevenZip.openInArchive(null,t)的null替换为ArchiveFormat.ZIP,问题得到解决。
首先讲一下该位置参数的作用,该参数为指定所解压文件的类型,如果传入的参数为null则通过调用native方法nativeOpenArchive(null, inStream, archiveOpenCallback)返回一个IInArchive,这在Windows平台下没有问题,但是在Linux中由于默认的类型不是zip格式,则解压失败,所以这里需要指定一下索要解压文件的类型,其中ArchiveFormat为枚举类。下面已在Linux和Windows成功运行:
第一步、maven中引入的jar包如下:
首先讲一下该位置参数的作用,该参数为指定所解压文件的类型,如果传入的参数为null则通过调用native方法nativeOpenArchive(null, inStream, archiveOpenCallback)返回一个IInArchive,这在Windows平台下没有问题,但是在Linux中由于默认的类型不是zip格式,则解压失败,所以这里需要指定一下索要解压文件的类型,其中ArchiveFormat为枚举类。下面已在Linux和Windows成功运行:
第一步、maven中引入的jar包如下:
<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>第二步、解压代码如下:
public static boolean unzip(String zipFilePath, String zipFileName, String targetFileDir){ boolean flag = false; //1.判断压缩文件是否存在,以及里面的内容是否为空 File file = null; //压缩文件(带路径) ZipFile zipFile = null; file = new File(zipFilePath + "/" + zipFileName); System.out.println(">>>>>>解压文件【" + zipFilePath + "/" + zipFileName + "】到【" + targetFileDir + "】目录下<<<<<<"); if(false == file.exists()) { System.out.println(">>>>>>压缩文件【" + zipFilePath + "/" + zipFileName + "】不存在<<<<<<"); return false; } else if(0 == file.length()) { System.out.println(">>>>>>压缩文件【" + zipFilePath + "/" + zipFileName + "】大小为0不需要解压<<<<<<"); return false; } else { //2.开始解压ZIP压缩文件的处理 byte[] buf = new byte[FILE_BUFFER_SIZE]; int readSize = -1; ZipInputStream zis = null; FileOutputStream fos = null; //解压7zip文件 RandomAccessFile randomAccessFile = null; IInArchive inArchive =null; try{ // 判断目标目录是否存在,不存在则创建 File newdir = new File(targetFileDir); if (false == newdir.exists()) { newdir.mkdirs(); newdir = null; } randomAccessFile = new RandomAccessFile(zipFilePath + "/" + zipFileName, "r"); RandomAccessFileInStream t=new RandomAccessFileInStream(randomAccessFile); inArchive =SevenZip.openInArchive( ArchiveFormat.ZIP,t); ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface(); System.out.println("-----Hash-----+------Size------+-----FileName----"); for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { final int[] hash = new int[] { 0 }; System.out.println("item.isFolder()=="+item.isFolder()); if (!item.isFolder()) { ExtractOperationResult result; final long[] sizeArray = new long[1]; result = item.extractSlow(new ISequentialOutStream() { public int write(byte[] data) throws SevenZipException { //写入指定文件 FileOutputStream fos; try { if (item.getPath().indexOf(File.separator) > 0) { String path = targetFileDir+ File.separator+ item.getPath().substring(0,item.getPath().lastIndexOf(File.separator)); File folderExisting = new File(path); if (!folderExisting.exists()) new File(path).mkdirs(); } fos = new FileOutputStream(targetFileDir + File.separator + item.getPath(),true); System.out.println(">>>>>>保存文件至:"+targetFileDir + File.separator + item.getPath()); 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); } } } flag =true; }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); } } } } return flag; }
相关文章推荐
- java的优点再谈!
- JAVA WEB快速入门之从编写一个JSP WEB网站了解JSP WEB网站的基本结构、调试、部署
- HTML、Swing还是XML,Java客户端工具选择?(三)
- sun 称 java 存在漏洞!
- 实 现JAVA 的 动 态 类 载 入 机 制
- 从C++到Java --理解面向对象是关键所在
- Borland Eyeing the Chasm Between Java and .NET
- Java.NET --一个基于Java的Microsoft.NET框架的实现
- 批判性地看待一种可行的表示技术:JavaServerPages servlet技术(下)
- 批判性地看待一种可行的表示技术:JavaServerPages servlet技术(上)
- PHP & Java(4)
- PHP & Java(3)
- PHP & Java(2)
- PHP & Java(1)
- Java 101之线程基础
- java动画中消除闪烁的两个绝招!
- [导入]Java线程的深入探讨
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- 关于java 的 Classpath全解
- Java代码编写的一般性指导