Skip to content

Commit 4423325

Browse files
committed
Pass original ByteData to ZipArchive, and make it closable to support try(...)
1 parent 834db30 commit 4423325

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/software/coley/llzip/ZipIO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static ZipArchive read(ByteData data, ZipReaderStrategy strategy) throws
130130
if (data.length() < 22)
131131
throw new IOException("Not enough bytes to read Central-Directory-File-Header, minimum=22");
132132
// Create instance
133-
ZipArchive zip = new ZipArchive();
133+
ZipArchive zip = new ZipArchive(data);
134134
strategy.read(zip, data);
135135
return zip;
136136
}

src/main/java/software/coley/llzip/format/model/ZipArchive.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import software.coley.llzip.util.OffsetComparator;
44

5+
import java.io.Closeable;
56
import java.util.ArrayList;
67
import java.util.List;
78
import java.util.function.Predicate;
@@ -12,8 +13,17 @@
1213
*
1314
* @author Matt Coley
1415
*/
15-
public class ZipArchive {
16+
public class ZipArchive implements AutoCloseable {
1617
private final List<ZipPart> parts = new ArrayList<>();
18+
private final Closeable closableBackingResource;
19+
20+
/**
21+
* @param closableBackingResource
22+
* Closable resource backing the zip archive.
23+
*/
24+
public ZipArchive(Closeable closableBackingResource) {
25+
this.closableBackingResource = closableBackingResource;
26+
}
1727

1828
/**
1929
* @return All parts of the zip archive.
@@ -95,6 +105,13 @@ public EndOfCentralDirectory getEnd() {
95105
.findFirst().orElse(null);
96106
}
97107

108+
/**
109+
* @return Closable resource backing the zip archive.
110+
*/
111+
protected Closeable getClosableBackingResource() {
112+
return closableBackingResource;
113+
}
114+
98115
@Override
99116
public boolean equals(Object o) {
100117
if (this == o) return true;
@@ -109,4 +126,10 @@ public boolean equals(Object o) {
109126
public int hashCode() {
110127
return parts.hashCode();
111128
}
129+
130+
@Override
131+
public void close() throws Exception {
132+
if (closableBackingResource != null)
133+
closableBackingResource.close();
134+
}
112135
}

0 commit comments

Comments
 (0)