Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Commit

Permalink
zip iterator improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
sokomishalov committed Dec 10, 2019
1 parent 72c8a63 commit 38deb0d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright 2019-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused")

package ru.sokomishalov.commons.core.collections


/**
* @author sokomishalov
*/
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@

package ru.sokomishalov.commons.core.io

import java.io.File
import java.io.InputStream
import java.io.RandomAccessFile
import java.io.*
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream
Expand All @@ -46,6 +44,31 @@ fun File.zipFiles(filesMap: Map<String, ByteArray>) {
}
}

fun File.unzipTo(folder: File) {
if (folder.exists().not()) folder.mkdir()

FileInputStream(this).use { fis ->
ZipInputStream(fis).use { zis ->
zis.toIterableEntries().forEach { ze ->
val newFile = File("${folder.path}/${ze.name}")
when {
ze.isDirectory -> newFile.mkdirs()
else -> {
newFile.parentFile.mkdir()
FileOutputStream(newFile).use { fos ->
zis.copyTo(fos)
}
}
}
}
}
}
}

fun File.unzipTo(path: String) {
unzipTo(File(path))
}

fun File.listFilesDeep(filter: (File) -> Boolean = { true }): List<File> {
return walkTopDown()
.maxDepth(MAX_VALUE)
Expand Down

0 comments on commit 38deb0d

Please sign in to comment.