Code working for me :
pom.xml to add :
<!-- Unzip 7z -->
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
<!-- Unzip 7z dependancy with commons-compress for 7z-->
<!-- https://mvnrepository.com/artifact/org.tukaani/xz -->
<dependency>
<groupId>org.tukaani</groupId>
<artifactId>xz</artifactId>
<version>1.8</version>
</dependency>
Add to class
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();
}
UPDATE :
Without store :
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);
}
}
With ls method :
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;
}
And with getContent method :
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;
}