Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datasources - part 3: TarArchiveDataSource #3103

Merged
merged 12 commits into from
Aug 2, 2024
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