zip压缩包
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
public static void main(String[] args) {
try {
ZipFile file = new ZipFile(new File(""));
System.out.println(file.isEncrypted());
} catch (ZipException e) {
e.printStackTrace();
}
}
rar压缩包
使用sevenzip判断是否加密。
参考https://stackoverflow.com/questions/27543357/how-do-i-detect-if-the-7zip-file-is-password-protected
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding</artifactId>
<version>16.02-2.01</version>
</dependency>
<dependency>
<groupId>net.sf.sevenzipjbinding</groupId>
<artifactId>sevenzipjbinding-all-platforms</artifactId>
<version>16.02-2.01</version>
</dependency>
try(RandomAccessFile randomAccessFile = new RandomAccessFile(rarDir, "r");
IInArchive archive = SevenZip.openInArchive(null,new RandomAccessFileInStream(randomAccessFile))){
if (Boolean.TRUE.equals(archive.getArchiveProperty(PropID.ENCRYPTED))) {
System.out.println("加密");
return true;
}
for (int i=0;i<archive.getNumberOfItems();i++) {
if (Boolean.TRUE.equals(archive.getProperty(i,PropID.ENCRYPTED))) {
System.out.println("加密");
return true;
}
}
System.out.println("没加密");
int[] in = new int[archive.getNumberOfItems()];
for (int i = 0; i < in.length; i++) {
in[i] = i;
}
archive.extract(in, false, new ExtractCallback(archive,outDir + "/"));
}catch (Exception e){
result = false;
LogUtil.error(RarUtil.class,"rar文件解压缩失败,原因:{}",e);
}