fixed unzip tests and slightly modified LocalDriver

This commit is contained in:
Anton Belyi 2023-05-05 16:41:32 +03:00
parent e1c8be66db
commit 8e304377c6
3 changed files with 32 additions and 49 deletions

View File

@ -19,19 +19,19 @@ internal class LocalFile(private val path: Path) : FileReader, FileWriter {
override suspend fun write(bytes: ByteArray) = path.writeBytes(bytes)
}
private class LocalDirectory(private val root: Path, private val currentDir: Path) : Directory {
internal class LocalDirectory(private val root: Path, private val currentDir: Path) : Directory {
private fun child(child: String): Path = root / currentDir / child
private fun child(child: Path): Path = root / currentDir / child
override fun close() {}
override suspend fun get(filename: String): FileReader = LocalFile(child(filename))
override suspend fun get(filename: String): LocalFile = LocalFile(child(filename))
override suspend fun get(filename: Path): FileReader = LocalFile(child(filename))
override suspend fun get(filename: Path): LocalFile = LocalFile(child(filename))
override suspend fun create(filename: String, ignoreIfExists: Boolean) {
child(filename).parent.createDirectories()
val dir = child(filename)
dir.parent.createDirectories()
try {
child(filename).createFile()
} catch (ex: java.nio.file.FileAlreadyExistsException) {
@ -41,18 +41,14 @@ private class LocalDirectory(private val root: Path, private val currentDir: Pat
}
}
override suspend fun put(filename: String): FileWriter {
val tmp = child(filename)
//tmp.toFile().setWritable(true)
return LocalFile(tmp)
}
override suspend fun put(filename: String): LocalFile = get(filename)
override suspend fun put(filename: Path): LocalFile = get(filename)
override suspend fun put(filename: Path): FileWriter = LocalFile(child(filename))
override suspend fun getSubdir(path: Path): LocalDirectory = LocalDirectory(root, child(path))
override suspend fun getSubdir(path: Path): LocalDirectory = LocalDirectory(root, currentDir / path)
override suspend fun createSubdir(dirname: String, ignoreIfExists: Boolean): LocalDirectory {
val dir = child(dirname)
dir.parent.createDirectories()
try {
dir.createDirectory()
} catch (ex: java.nio.file.FileAlreadyExistsException) {
@ -60,7 +56,7 @@ private class LocalDirectory(private val root: Path, private val currentDir: Pat
throw ex
}
}
return LocalDirectory(root, dir)
return LocalDirectory(root, currentDir / dirname)
}
override val path: Path

View File

@ -17,7 +17,7 @@ internal class LocalDriverTests {
@BeforeTest
fun setUp() {
tempDir = createTempDirectory()
testSample = LocalDirectory(tempDir!!)
testSample = localStorage(tempDir!!)
}
@Test

View File

@ -4,6 +4,7 @@ import kotlinx.coroutines.runBlocking
import space.kscience.snark.storage.Directory
import space.kscience.snark.storage.local.LocalDirectory
import space.kscience.snark.storage.local.localStorage
import java.io.*
import java.nio.file.Files
import java.nio.file.Path
@ -24,55 +25,40 @@ internal class UnzipTests {
private suspend fun makeFile(dir: Directory, filename: String, content: ByteArray) {
dir.create(filename)
val writter = dir.put(filename)
if (!(tempDir!! / Path("source") / Path(filename)).isRegularFile()) {
println("new shit")
}
writter.write(content)
dir.put(filename).write(content)
}
private fun zipAll(directory: String, zipFile: String) {
val sourceFile = File(directory)
ZipOutputStream(BufferedOutputStream( FileOutputStream(zipFile))).use {
it.use {
zipFiles(it, sourceFile, "")
it.closeEntry()
it.close()
}
zipFiles(it, sourceFile, File.separator)
it.closeEntry()
it.close()
}
}
private fun zipFiles(zipOut: ZipOutputStream, sourceFile: File, parentDirPath: String) {
private fun zipFiles(zipOut: ZipOutputStream, sourceFile: File, parentDirname: String) {
val data = ByteArray(2048)
for (f in sourceFile.listFiles()) {
if (f.isDirectory) {
val entry = ZipEntry(f.name + File.separator)
entry.time = f.lastModified()
entry.isDirectory
entry.size = f.length()
zipOut.putNextEntry(entry)
zipFiles(zipOut, f, f.name)
zipFiles(zipOut, f, parentDirname + f.name + File.separator)
} else {
if (!f.name.contains(".zip")) { //If folder contains a file with extension ".zip", skip it
FileInputStream(f).use { fi ->
BufferedInputStream(fi).use { origin ->
val path = parentDirPath + File.separator + f.name
val entry = ZipEntry(path)
entry.time = f.lastModified()
entry.isDirectory
entry.size = f.length()
zipOut.putNextEntry(entry)
while (true) {
val readBytes = origin.read(data)
if (readBytes == -1) {
break
}
zipOut.write(data, 0, readBytes)
FileInputStream(f).use { fi ->
BufferedInputStream(fi).use { origin ->
var path = parentDirname + f.name
val entry = ZipEntry(path.drop(1))
entry.time = f.lastModified()
entry.size = f.length()
zipOut.putNextEntry(entry)
while (true) {
val readBytes = origin.read(data)
if (readBytes == -1) {
break
}
zipOut.write(data, 0, readBytes)
}
}
}
@ -83,7 +69,7 @@ internal class UnzipTests {
@Test
fun testUnzip() = runBlocking {
val dir: Directory = LocalDirectory(tempDir!!)
val dir: Directory = localStorage(tempDir!!)
val source = dir.createSubdir("source")
val target = dir.createSubdir("target")
val bytes1 = byteArrayOf(0, 1, 2, 3)
@ -101,6 +87,7 @@ internal class UnzipTests {
unzip(archive_path, target)
val targetPath = tempDir!! / Path("target")
println(targetPath)
val entries = targetPath.listDirectoryEntries()
assertEquals(3, entries.size)