SNRK-60: slightly changed local driver and added unzip function

This commit is contained in:
Anton Belyi 2023-04-24 15:32:29 +03:00
parent 0dad4cd923
commit 9651ad1cd9
2 changed files with 29 additions and 5 deletions

View File

@ -15,19 +15,19 @@ public class LocalFile(private val path: String) : FileReader, FileWriter {
public class LocalDirectory(private val path: String) : Directory { public class LocalDirectory(private val path: String) : Directory {
override fun close() {} 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) { 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") 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 { 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") throw UnsupportedOperationException("File already exists")
} }
return this.getSubdir(File(dirname).toPath()) return this.getSubdir(File(dirname).toPath())

View File

@ -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()
}