@@ -32,6 +32,12 @@ public class ZipArchive {
32
32
33
33
public class Stream : NSObject , DataOutputStream {
34
34
35
+ enum StreamState : Int {
36
+ case open
37
+ case closing
38
+ case closed
39
+ }
40
+
35
41
private let archive : Archive
36
42
private let compressionMethod : CompressionMethod
37
43
@@ -40,7 +46,7 @@ public class ZipArchive {
40
46
41
47
private let chunks = Locked < [ Data ] > ( [ ] )
42
48
private let error = Locked < Error ? > ( nil )
43
- private let finished = Locked < Bool > ( false )
49
+ private let state = Locked < StreamState > ( . open )
44
50
45
51
fileprivate init ( archive: Archive , path: String , compressionMethod: CompressionMethod ) {
46
52
self . archive = archive
@@ -58,13 +64,18 @@ public class ZipArchive {
58
64
processingQueue. async {
59
65
do {
60
66
try self . archive. addEntry ( with: path, type: . file, compressionMethod: self . compressionMethod. zfMethod) { position, size in
67
+
68
+ if self . state. value == . closed {
69
+ return Data ( )
70
+ }
61
71
self . semaphore. wait ( )
62
72
var chunk : Data !
63
73
self . chunks. mutate { ( value) in
64
74
if value. count > 0 {
65
75
chunk = value. removeFirst ( )
66
- } else if self . finished . value {
76
+ } else if self . state . value == . closing {
67
77
chunk = Data ( )
78
+ self . state. value = . closed
68
79
}
69
80
}
70
81
return chunk
@@ -82,7 +93,7 @@ public class ZipArchive {
82
93
if let error = error. value {
83
94
throw error
84
95
}
85
- if finished . value {
96
+ if self . state . value != . open {
86
97
throw ZipArchiveError . streamFinished
87
98
}
88
99
chunks. mutate { value in
@@ -94,7 +105,7 @@ public class ZipArchive {
94
105
public func finish( sync: Bool ) throws {
95
106
// An empty Data() is the sigil for the ZipFoundation read callback
96
107
// to detect end of stream.
97
- finished . value = true
108
+ state . value = . closing
98
109
semaphore. signal ( )
99
110
if sync {
100
111
// Block until processingQueue is finished, and then check error state
0 commit comments