From 9651ad1cd9a7052a0dc99fe5c05fd51b89911494 Mon Sep 17 00:00:00 2001 From: Anton Belyi Date: Mon, 24 Apr 2023 15:32:29 +0300 Subject: [PATCH] SNRK-60: slightly changed local driver and added unzip function --- .../snark/storage/local/localDriver.kt | 10 ++++---- .../kscience/snark/storage/unzip/Unzip.kt | 24 +++++++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/localDriver.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/localDriver.kt index 554faf0..04c78f2 100644 --- a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/localDriver.kt +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/local/localDriver.kt @@ -15,19 +15,19 @@ public class LocalFile(private val path: String) : FileReader, FileWriter { public class LocalDirectory(private val path: String) : Directory { override fun close() {} - override suspend fun get(filename: String): FileReader = LocalFile("${this.path}/$filename") + override suspend fun get(filename: String): FileReader = LocalFile("${this.path}${File.separator}$filename") override suspend fun create(filename: String, ignoreIfExists: Boolean) { - if (!File(filename).createNewFile() && !ignoreIfExists) { + if (!File("${this.path}${File.separator}$filename").createNewFile() && !ignoreIfExists) { throw UnsupportedOperationException("File already exists") } } - override suspend fun put(filename: String): FileWriter = LocalFile("${this.path}/$filename") + override suspend fun put(filename: String): FileWriter = LocalFile("${this.path}${File.separator}$filename") - override suspend fun getSubdir(dirpath: Path): Directory = LocalDirectory("${this.path}/$dirpath") + override suspend fun getSubdir(dirpath: Path): Directory = LocalDirectory("${this.path}${File.separator}$dirpath") override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): Directory { - if (!File(dirname).mkdir() && !ignoreIfExists) { + if (!File("${this.path}${File.separator}$dirname").mkdir() && !ignoreIfExists) { throw UnsupportedOperationException("File already exists") } return this.getSubdir(File(dirname).toPath()) diff --git a/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt new file mode 100644 index 0000000..e7aff9c --- /dev/null +++ b/snark-storage-driver/src/main/kotlin/space/kscience/snark/storage/unzip/Unzip.kt @@ -0,0 +1,24 @@ +package space.kscience.snark.storage.unzip + +import space.kscience.snark.storage.Directory +import java.io.FileInputStream +import java.util.zip.ZipInputStream + +public suspend fun unzip(source_path: String, target: Directory) { + val zis = ZipInputStream(FileInputStream(source_path)) + var zipEntry = zis.nextEntry + while (zipEntry != null) { + if (!zipEntry.isDirectory) { + val filename = zipEntry.name + target.create(filename, true) + val fos = target.put(filename) + val buffer = ByteArray(zipEntry.size.toInt()) + zis.read(buffer) + fos.write(buffer) + fos.close() + } + zipEntry = zis.nextEntry + } + zis.closeEntry() + zis.close() +} \ No newline at end of file