Skip to content

Commit

Permalink
Datasources - part 3: TarArchiveDataSource (#3103)
Browse files Browse the repository at this point in the history
* add TarArchiveDataSource
* add unit test
* make tar work like zip in listFiles
* change test to show that files in every subfolder is reached

Signed-off-by: Nicolas Rol <nicolas.rol@rte-france.com>
  • Loading branch information
rolnico authored Aug 2, 2024
1 parent 4b4f148 commit 4446bd9
Show file tree
Hide file tree
Showing 14 changed files with 670 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package com.powsybl.commons.datasource;

import java.nio.file.Path;
import java.util.Objects;

/**
* @author Nicolas Rol {@literal <nicolas.rol at rte-france.com>}
Expand All @@ -26,4 +27,17 @@ public abstract class AbstractArchiveDataSource extends AbstractFileSystemDataSo
protected Path getArchiveFilePath() {
return directory.resolve(archiveFileName);
}

protected ArchiveFormat getArchiveFormat() {
return archiveFormat;
}

protected abstract boolean entryExists(Path archiveFilePath, String fileName);

@Override
public boolean exists(String fileName) {
Objects.requireNonNull(fileName);
Path archiveFilePath = getArchiveFilePath();
return entryExists(archiveFilePath, fileName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* @author Nicolas Rol {@literal <nicolas.rol at rte-france.com>}
*/
public enum ArchiveFormat {
ZIP("zip");
ZIP("zip"),
TAR("tar");

ArchiveFormat(String extension) {
this.extension = Objects.requireNonNull(extension);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ DataSource build() {
// Create the datasource
if (compressionFormat == CompressionFormat.ZIP || archiveFormat == ArchiveFormat.ZIP) {
return buildZip();
} else if (archiveFormat == ArchiveFormat.TAR) {
return archiveFileName == null ?
new TarArchiveDataSource(directory, baseName, dataExtension, compressionFormat, observer) :
new TarArchiveDataSource(directory, archiveFileName, baseName, dataExtension, compressionFormat, observer);
} else if (compressionFormat == null) {
return new DirectoryDataSource(directory, baseName, dataExtension, observer);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,21 @@ private void computeInformation(String fileName, boolean dataSourceInitializatio
// File name without the compression extension
String fileNameWithoutCompressionExtension = compressionFormat == null ? fileName : fileName.substring(0, currentDotIndex);

// Last dot index
currentDotIndex = fileNameWithoutCompressionExtension.lastIndexOf('.');

// Archive extension
String fileNameWithoutCompressionNorArchive;
archiveFormat = compressionFormat == CompressionFormat.ZIP ? ArchiveFormat.ZIP : null;
fileNameWithoutCompressionNorArchive = fileNameWithoutCompressionExtension;
if (compressionFormat == CompressionFormat.ZIP) {
archiveFormat = ArchiveFormat.ZIP;
fileNameWithoutCompressionNorArchive = fileNameWithoutCompressionExtension;
} else if (ArchiveFormat.TAR.getExtension().equals(fileNameWithoutCompressionExtension.substring(currentDotIndex + 1))) {
archiveFormat = ArchiveFormat.TAR;
fileNameWithoutCompressionNorArchive = fileNameWithoutCompressionExtension.substring(0, currentDotIndex);
} else {
archiveFormat = null;
fileNameWithoutCompressionNorArchive = fileNameWithoutCompressionExtension;
}

// Last dot index
currentDotIndex = fileNameWithoutCompressionNorArchive.lastIndexOf('.');
Expand Down
Loading

0 comments on commit 4446bd9

Please sign in to comment.