@@ -7,14 +7,15 @@ import java.nio.file.Files
7
7
import java.nio.file.Path
8
8
import java.util.zip.ZipEntry
9
9
10
- internal class ZipFileSystemUtils (
11
- file : File
12
- ) : Closeable {
13
- private var zipFileSystem = FileSystems .newFileSystem(file.toPath(), mapOf (" noCompression" to true ))
10
+ internal class ZipFileSystemUtils (input : File ? , output : File ) : Closeable {
11
+ private val inFileSystem = if (input != null ) {
12
+ FileSystems .newFileSystem(input.toPath())
13
+ } else null
14
+ private val outFileSystem = FileSystems .newFileSystem(output.toPath(), mapOf (" noCompression" to true ))
14
15
15
16
private fun Path.deleteRecursively () {
16
17
if (! Files .exists(this )) {
17
- throw IllegalStateException (" File exists in real folder but not in zip file system " )
18
+ throw IllegalStateException (" File exists in input but not in output, cannot delete " )
18
19
}
19
20
20
21
if (Files .isDirectory(this )) {
@@ -26,21 +27,25 @@ internal class ZipFileSystemUtils(
26
27
Files .delete(this )
27
28
}
28
29
29
- internal fun writePathRecursively (path : Path ) {
30
- Files .list(path).let { fileStream ->
30
+ internal fun writeInput () {
31
+ if (inFileSystem == null ) {
32
+ throw IllegalArgumentException (" Input file not set" )
33
+ }
34
+ val root = inFileSystem.getPath(inFileSystem.separator)
35
+
36
+ Files .list(root).close()
37
+
38
+ Files .list(root).also { fileStream ->
31
39
fileStream.forEach { filePath ->
32
- val fileSystemPath = filePath.getRelativePath(path )
40
+ val fileSystemPath = filePath.getRelativePath(root )
33
41
fileSystemPath.deleteRecursively()
34
42
}
35
-
36
- fileStream
37
43
}.close()
38
44
39
- Files .walk(path).let { fileStream ->
40
- // don't include build directory
41
- // by skipping the root node.
45
+ Files .walk(root).also { fileStream ->
46
+ // don't include build directory by skipping the root node.
42
47
fileStream.skip(1 ).forEach { filePath ->
43
- val relativePath = filePath.getRelativePath(path )
48
+ val relativePath = filePath.getRelativePath(root )
44
49
45
50
if (Files .isDirectory(filePath)) {
46
51
Files .createDirectory(relativePath)
@@ -49,17 +54,18 @@ internal class ZipFileSystemUtils(
49
54
50
55
Files .copy(filePath, relativePath)
51
56
}
52
-
53
- fileStream
54
57
}.close()
55
58
}
56
59
57
- internal fun write (path : String , content : ByteArray ) = Files .write(zipFileSystem .getPath(path), content)
60
+ internal fun write (path : String , content : ByteArray ) = Files .write(outFileSystem .getPath(path), content)
58
61
59
- private fun Path.getRelativePath (path : Path ): Path = zipFileSystem .getPath(path.relativize(this ).toString())
62
+ private fun Path.getRelativePath (path : Path ): Path = outFileSystem .getPath(path.relativize(this ).toString())
60
63
61
64
internal fun uncompress (vararg paths : String ) =
62
- paths.forEach { Files .setAttribute(zipFileSystem .getPath(it), " zip:method" , ZipEntry .STORED ) }
65
+ paths.forEach { Files .setAttribute(outFileSystem .getPath(it), " zip:method" , ZipEntry .STORED ) }
63
66
64
- override fun close () = zipFileSystem.close()
67
+ override fun close () {
68
+ inFileSystem?.close()
69
+ outFileSystem.close()
70
+ }
65
71
}
0 commit comments