我的代码可以正常运行:
需要添加的pom.xml文件:
<!
<!
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
<!
<!
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.8</version>
</dependency>
添加到类中
public static String unzip(final String directory, final String fileName) {
final StringBuilder sb = new StringBuilder();
final File fDirectory = new File(directory);
final File input7z = new File(fDirectory, fileName);
try (final SevenZFile sevenZFile = new SevenZFile(input7z)) {
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry != null) {
LOGGER.info("Treatment of entry : {}", entry.getName());
try (final FileOutputStream out = new FileOutputStream(new File(fDirectory, entry.getName()))) {
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out.write(content);
} catch (final IOException ioe) {
final String error = "Error when writing entry " + entry.getName();
LOGGER.error(error);
sb.append(error).append("\n");
}
entry = sevenZFile.getNextEntry();
}
} catch (final IOException ioe) {
final String error = "Error when reading entry " + fileName;
LOGGER.error(error);
sb.append(error).append("\n");
}
return sb.length() == 0 ? null : sb.toString();
}
更新:
无存储:
public static String getContentOneFile(final String directory, final String fileName) throws IOException {
final List<String> subFilenames = ls(directory, fileName);
if (subFilenames.size() == 1) {
return getContent(directory, fileName, subFilenames.get(0));
} else if (subFilenames.size() == 0) {
throw new IOException("No file found in 7zip : " + directory + ":" + fileName);
} else {
throw new IOException("Can not extract data from many document. Please specify subFilename in " + subFilenames);
}
}
使用 ls 方法:
public static List<String> ls(final String directory, final String fileName) throws IOException {
final List<String> out = new ArrayList<>();
final File fDirectory = new File(directory);
final File input7z = new File(fDirectory, fileName);
try (final SevenZFile sevenZFile = new SevenZFile(input7z)) {
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry != null) {
out.add(entry.getName());
entry = sevenZFile.getNextEntry();
}
} catch (final IOException ioe) {
final String error = "Error when reading entry " + fileName;
LOGGER.error(error);
throw ioe;
}
return out;
}
而且使用getContent方法:
public static String getContent(final String directory, final String fileName, final String subFileName) throws IOException {
String out = null;
final File fDirectory = new File(directory);
final File input7z = new File(fDirectory, fileName);
try (final SevenZFile sevenZFile = new SevenZFile(input7z)) {
SevenZArchiveEntry entry = sevenZFile.getNextEntry();
while (entry != null) {
LOGGER.info("Treatment of entry : {}", entry.getName());
if (subFileName.equals(entry.getName())) {
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content, 0, content.length);
out = new String(content);
}
entry = sevenZFile.getNextEntry();
}
} catch (final IOException ioe) {
final String error = "Error when reading entry " + fileName;
LOGGER.error(error);
throw ioe;
}
return out;
}