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

Task05_Lyschev #122

Open
wants to merge 1 commit into
base: Lischev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Task_05/Controller.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import com.sun.jdi.ArrayReference

class Controller(){
fun makeRequest(commandsInput: Array<String>){
when{
((commandsInput[0] =="?")||(commandsInput[0]=="help"))&&(commandsInput.size == 1) -> {
print("Commands:\n open \n folderInfo \n fileInfo \n")

}
(commandsInput[1]=="open")&&(commandsInput.size == 2)-> {
val folder = Model(commandsInput[0])
val view = View()
view.viewZip(folder.zipFile)

}
(commandsInput[1]=="folderInfo")&&(commandsInput.size == 3) -> {
val folder = Model(commandsInput[0]).findInFile(commandsInput[2])
val view = View()
view.printFolderInfo(folder)

}
(commandsInput[1]=="fileInfo")&&(commandsInput.size == 3) ->{
val folder = Model(commandsInput[0]).findInFile(commandsInput[2])
val view = View()
view.printFileInfo(folder)
}
else -> {
println("Wrong input, enter '?' for help")
}

}

}

}
6 changes: 6 additions & 0 deletions Task_05/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fun main(args: Array<String>){

val myController = Controller()
myController.makeRequest(args)

}
61 changes: 61 additions & 0 deletions Task_05/Model.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import java.io.IOException
import java.util.zip.*
import kotlin.system.exitProcess


class Model (fileName : String){


val zipFile =try {
ZipFile(fileName)
}
catch (e: ZipException){
println("there is no zip file")
exitProcess(1)
}
catch (e : IOException){
println("can't open zip file")
exitProcess(2)
}


fun findInFile(entryName: String): MutableList<ZipEntry> {
val filesWithSameName: MutableList<ZipEntry> = mutableListOf()

var searchedEntry = zipFile.getEntry(entryName)
if (searchedEntry != null) {
if (searchedEntry.isDirectory)
searchedEntry.size = getFileSize(searchedEntry)

filesWithSameName.add(searchedEntry)
}

for (entry in zipFile.entries())
if (entry.isDirectory) {
val directoryName = entry.name
searchedEntry = zipFile.getEntry(directoryName + entryName)

if (searchedEntry != null) {
if (searchedEntry.isDirectory)
searchedEntry.size = getFileSize( searchedEntry)

filesWithSameName.add(searchedEntry)
}
}

return filesWithSameName
}

private fun getFileSize(dir: ZipEntry): Long {
var size = 0L

for (entry in zipFile.entries()) {
if (entry.name.contains(dir.name))
size += entry.size
}

return size
}
}


69 changes: 69 additions & 0 deletions Task_05/View.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.zip.*

class View() {



fun viewZip(data_:ZipFile) {
val data = data_
val zipFiles = data.entries().toList().sortedBy { it.name }
for (i in zipFiles) {
var path = i.name.split('/')
for (i in 0 until path.size) {
if (path[i] == "")
path = path.dropLast(1)
}
if (path.size > 1) {

for (i in 1 until path.size - 1)
print(" ")
print("|--->")


}
println(path[path.size - 1])


}
}
fun printFolderInfo(folders: MutableList<ZipEntry>) {
if (folders.isEmpty()) {
println("can't find folder!")
return
}


for (folder in folders) {
val folderSize = folder.size
println(folder.name + " found")
println("size is: $folderSize bytes")
println()
}
}

fun printFileInfo(filesWithSameName: MutableList<ZipEntry>) {
if (filesWithSameName.isEmpty()) {
println("can't find file!")
return
}
for (file in filesWithSameName) {


println(file.name + " found")
val creationTime :String = try {
"creation time is: "+file.creationTime.toString()
}
catch (e :java.lang.NullPointerException){
"can't read creation time"

}
println("$creationTime")
println()
}
}

}