您的位置:首页 > 编程语言 > Java开发

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包如下:

<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: