|
| 1 | +package io.github.typesafegithub.workflows.mavenbinding |
| 2 | + |
| 3 | +import com.intellij.util.io.PagedFileStorage |
| 4 | +import java.io.BufferedInputStream |
| 5 | +import java.io.File |
| 6 | +import java.io.FileInputStream |
| 7 | +import java.io.FileNotFoundException |
| 8 | +import java.io.IOException |
| 9 | +import java.io.OutputStream |
| 10 | +import java.nio.file.Path |
| 11 | +import java.util.zip.ZipEntry |
| 12 | +import java.util.zip.ZipOutputStream |
| 13 | +import kotlin.io.path.isDirectory |
| 14 | +import kotlin.io.path.listDirectoryEntries |
| 15 | +import kotlin.io.path.name |
| 16 | + |
| 17 | +internal fun OutputStream.createZipFile(contents: Path) = |
| 18 | + ZipOutputStream(this).use { zipOutputStream -> |
| 19 | + contents.listDirectoryEntries().forEach { file -> |
| 20 | + if (file.isDirectory()) { |
| 21 | + zipDirectory(file.toFile(), file.name, zipOutputStream) |
| 22 | + } else { |
| 23 | + zipFile(file.toFile(), zipOutputStream) |
| 24 | + } |
| 25 | + } |
| 26 | + zipOutputStream.flush() |
| 27 | + } |
| 28 | + |
| 29 | +/** |
| 30 | + * Adds a directory to the current zip output stream |
| 31 | + * @param folder the directory to be added |
| 32 | + * @param parentFolder the path of parent directory |
| 33 | + * @param zos the current zip output stream |
| 34 | + * @throws FileNotFoundException |
| 35 | + * @throws IOException |
| 36 | + */ |
| 37 | +@Throws(FileNotFoundException::class, IOException::class) |
| 38 | +private fun zipDirectory( |
| 39 | + folder: File, |
| 40 | + parentFolder: String, |
| 41 | + zos: ZipOutputStream, |
| 42 | +) { |
| 43 | + for (file in folder.listFiles()) { |
| 44 | + if (file.isDirectory()) { |
| 45 | + zipDirectory(file, parentFolder + "/" + file.getName(), zos) |
| 46 | + continue |
| 47 | + } |
| 48 | + zos.putNextEntry(ZipEntry(parentFolder + "/" + file.getName())) |
| 49 | + val bis = |
| 50 | + BufferedInputStream( |
| 51 | + FileInputStream(file), |
| 52 | + ) |
| 53 | + var bytesRead: Long = 0 |
| 54 | + val bytesIn = ByteArray(PagedFileStorage.BUFFER_SIZE) |
| 55 | + var read: Int |
| 56 | + while ((bis.read(bytesIn).also { read = it }) != -1) { |
| 57 | + zos.write(bytesIn, 0, read) |
| 58 | + bytesRead += read.toLong() |
| 59 | + } |
| 60 | + zos.closeEntry() |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Adds a file to the current zip output stream |
| 66 | + * @param file the file to be added |
| 67 | + * @param zos the current zip output stream |
| 68 | + * @throws FileNotFoundException |
| 69 | + * @throws IOException |
| 70 | + */ |
| 71 | +@Throws(FileNotFoundException::class, IOException::class) |
| 72 | +private fun zipFile( |
| 73 | + file: File, |
| 74 | + zos: ZipOutputStream, |
| 75 | +) { |
| 76 | + zos.putNextEntry(ZipEntry(file.getName())) |
| 77 | + val bis = |
| 78 | + BufferedInputStream( |
| 79 | + FileInputStream( |
| 80 | + file, |
| 81 | + ), |
| 82 | + ) |
| 83 | + var bytesRead: Long = 0 |
| 84 | + val bytesIn = ByteArray(PagedFileStorage.BUFFER_SIZE) |
| 85 | + var read: Int |
| 86 | + while ((bis.read(bytesIn).also { read = it }) != -1) { |
| 87 | + zos.write(bytesIn, 0, read) |
| 88 | + bytesRead += read.toLong() |
| 89 | + } |
| 90 | + zos.closeEntry() |
| 91 | +} |
0 commit comments