Skip to content

Commit cb584e7

Browse files
committed
manually read zip comment
1 parent cb866a2 commit cb584e7

File tree

1 file changed

+50
-2
lines changed

1 file changed

+50
-2
lines changed

src/main/java/dev/zarr/zarrjava/store/BufferedZipStore.java

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class BufferedZipStore implements Store, Store.ListableStore {
2323

2424
private final StoreHandle underlyingStore;
2525
private final Store.ListableStore bufferStore;
26-
private final String archiveComment;
26+
private String archiveComment;
2727

2828
private void writeBuffer() throws IOException{
2929
// create zip file bytes from buffer store and write to underlying store
@@ -113,14 +113,62 @@ private void writeBuffer() throws IOException{
113113
underlyingStore.set(ByteBuffer.wrap(zipBytes));
114114
}
115115

116+
// Source - https://stackoverflow.com/a/9918966
117+
// Retrieved 2025-12-12, License - CC BY-SA 3.0
118+
private static String getZipCommentFromBuffer (byte[] buffer, int len) {
119+
byte[] magicDirEnd = {0x50, 0x4b, 0x05, 0x06};
120+
int buffLen = Math.min(buffer.length, len);
121+
122+
// Check the buffer from the end
123+
for (int i = buffLen - magicDirEnd.length - 22; i >= 0; i--) {
124+
boolean isMagicStart = true;
125+
126+
for (int k = 0; k < magicDirEnd.length; k++) {
127+
if (buffer[i + k] != magicDirEnd[k]) {
128+
isMagicStart = false;
129+
break;
130+
}
131+
}
132+
133+
if (isMagicStart) {
134+
// Magic Start found!
135+
int commentLen = buffer[i + 20] + buffer[i + 21] * 256;
136+
int realLen = buffLen - i - 22;
137+
System.out.println ("ZIP comment found at buffer position "
138+
+ (i + 22) + " with len = " + commentLen + ", good!");
139+
140+
if (commentLen != realLen) {
141+
System.out.println ("WARNING! ZIP comment size mismatch: "
142+
+ "directory says len is " + commentLen
143+
+ ", but file ends after " + realLen + " bytes!");
144+
}
145+
146+
String comment = new String (buffer, i + 22, Math.min(commentLen, realLen));
147+
return comment;
148+
}
149+
}
150+
151+
System.out.println ("ERROR! ZIP comment NOT found!");
152+
return null;
153+
}
154+
116155
private void loadBuffer() throws IOException{
117156
// read zip file bytes from underlying store and populate buffer store
118157
ByteBuffer buffer = underlyingStore.read();
119158
if (buffer == null) {
120159
return;
121160
}
161+
162+
// read archive comment
163+
byte[] bufArray;
164+
if (buffer.hasArray()) {
165+
bufArray = buffer.array();
166+
} else {
167+
bufArray = new byte[buffer.remaining()];
168+
buffer.duplicate().get(bufArray);
169+
}
170+
this.archiveComment = getZipCommentFromBuffer(bufArray, bufArray.length);
122171
try (ZipArchiveInputStream zis = new ZipArchiveInputStream(new ByteBufferBackedInputStream(buffer))) {
123-
// this.archiveComment = zis.getComment();
124172
ArchiveEntry aentry;
125173
while ((aentry = zis.getNextEntry()) != null) {
126174
ZipArchiveEntry entry = (ZipArchiveEntry) aentry;

0 commit comments

Comments
 (0)