@@ -113,42 +113,30 @@ 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-
116+ // adopted from https://stackoverflow.com/a/9918966
117+ @ Nullable
118+ private String getZipCommentFromBuffer (byte [] bufArray ) throws IOException {
119+ // End of Central Directory (EOCD) record magic number
120+ byte [] EOCD = { 0x50 , 0x4b , 0x05 , 0x06 } ;
121+ int buffLen = bufArray . length ;
122122 // 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 ;
123+ search :
124+ for (int i = buffLen - EOCD .length - 22 ; i >= 0 ; i --) {
125+ for (int k = 0 ; k < EOCD .length ; k ++) {
126+ if (bufArray [i + k ] != EOCD [k ]) {
127+ continue search ;
130128 }
131129 }
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 ;
130+ // End of Central Directory found!
131+ int commentLen = bufArray [i + 20 ] + bufArray [i + 21 ] * 256 ;
132+ int realLen = buffLen - i - 22 ;
133+ if (commentLen != realLen ) {
134+ throw new IOException ("ZIP comment size mismatch: "
135+ + "directory says len is " + commentLen
136+ + ", but file ends after " + realLen + " bytes!" );
148137 }
138+ return new String (bufArray , i + 22 , commentLen );
149139 }
150-
151- System .out .println ("ERROR! ZIP comment NOT found!" );
152140 return null ;
153141 }
154142
@@ -158,20 +146,17 @@ private void loadBuffer() throws IOException{
158146 if (buffer == null ) {
159147 return ;
160148 }
161-
162- // read archive comment
163149 byte [] bufArray ;
164150 if (buffer .hasArray ()) {
165151 bufArray = buffer .array ();
166152 } else {
167153 bufArray = new byte [buffer .remaining ()];
168154 buffer .duplicate ().get (bufArray );
169155 }
170- this .archiveComment = getZipCommentFromBuffer (bufArray , bufArray . length );
156+ this .archiveComment = getZipCommentFromBuffer (bufArray );
171157 try (ZipArchiveInputStream zis = new ZipArchiveInputStream (new ByteBufferBackedInputStream (buffer ))) {
172- ArchiveEntry aentry ;
173- while ((aentry = zis .getNextEntry ()) != null ) {
174- ZipArchiveEntry entry = (ZipArchiveEntry ) aentry ;
158+ ZipArchiveEntry entry ;
159+ while ((entry = zis .getNextEntry ()) != null ) {
175160 if (entry .isDirectory ()) {
176161 continue ;
177162 }
@@ -185,7 +170,6 @@ private void loadBuffer() throws IOException{
185170 bufferStore .set (new String []{entry .getName ()}, ByteBuffer .wrap (bytes ));
186171 }
187172 }
188-
189173 }
190174
191175 public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore , @ Nullable String archiveComment ) {
0 commit comments