我从 7zip 网站下载了 LZMA SDK,但令我失望的是它只支持压缩和解压缩,不支持 AES 加密。有谁知道在 JAVA 中是否有完全使用 AES 加密的 7zip 实现?谢谢。
问候,卡尔。
来自 apache common-compress 文档:
请注意,Commons Compress 目前仅支持用于 7z 存档的压缩和加密算法的子集。对于仅写入未压缩条目,支持 LZMA、LZMA2、BZIP2 和 Deflate - 除了那些读取支持 AES-256/SHA-256 和 DEFLATE64。
如果您使用 common-compress,您可能不会遇到代码可移植性的问题,因为您不必嵌入任何本机库。
下面的代码显示了如何遍历 7zip 存档中的文件并将其内容打印到标准输出。您可以根据 AES 要求对其进行调整:
public static void showContent(String archiveFilename) throws IOException {
if (archiveFilename == null) {
return;
}
try (SevenZFile sevenZFile = new SevenZFile(new File(archiveFilename))) {
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry != null) {
final byte[] contents = new byte[(int) entry.getSize()];
int off = 0;
while ((off < contents.length)) {
final int bytesRead = sevenZFile.read(contents, off, contents.length - off);
off += bytesRead;
}
System.out.println(new String(contents, "UTF-8"));
entry = sevenZFile.getNextEntry();
}
}
}
使用的进口:
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;
使用的 Maven 依赖项:
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.6</version>
</dependency>
请注意:仅 7zip 需要 org.tukaani:xz。对于其他支持的压缩格式,common-compress 依赖项不需要它。
根据 7Zip 团队:
LZMA SDK 不支持加密方法。请改用 7-Zip 源代码。
源代码在汇编程序、C 和 C++ 中可用,您可以从 Java 调用它们。