gpt4 book ai didi

java - JAVA中7Zip的实现

转载 作者:行者123 更新时间:2023-11-30 06:37:29 25 4
gpt4 key购买 nike

我已经从 7zip 网站下载了 LZMA SDK,但令我失望的是它只支持压缩和解压,不支持 AES 加密。有谁知道在 JAVA 中是否有完全使用 AES 加密的 7zip 实现?谢谢。

问候,卡尔。

最佳答案

来自 apache common-compress 文档:

Note that Commons Compress currently only supports a subset of compression and encryption algorithms used for 7z archives. For writing only uncompressed entries, LZMA, LZMA2, BZIP2 and Deflate are supported - in addition to those reading supports AES-256/SHA-256 and DEFLATE64.

如果您使用 common-compress,您可能不会遇到代码的可移植性问题,因为您不必嵌入任何 native 库。

下面的代码展示了如何遍历 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 依赖项不需要它。

关于java - JAVA中7Zip的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3474605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com