11package dev .zarr .zarrjava .store ;
22
3+ import org .apache .commons .compress .archivers .zip .Zip64Mode ;
4+ import org .apache .commons .compress .archivers .zip .ZipArchiveEntry ;
5+ import org .apache .commons .compress .archivers .zip .ZipArchiveInputStream ;
6+ import org .apache .commons .compress .archivers .zip .ZipArchiveOutputStream ;
7+
38import javax .annotation .Nonnull ;
49import javax .annotation .Nullable ;
510import java .io .ByteArrayOutputStream ;
1015import java .nio .file .Paths ;
1116import java .util .Comparator ;
1217import java .util .stream .Stream ;
13-
14- import org .apache .commons .compress .archivers .zip .*;
15-
1618import java .util .zip .CRC32 ;
17- import java .util .zip .ZipEntry ; // for STORED constant
19+ import java .util .zip .ZipEntry ;
1820
1921
20- /** A Store implementation that buffers reads and writes and flushes them to an underlying Store as a zip file.
22+ /**
23+ * A Store implementation that buffers reads and writes and flushes them to an underlying Store as a zip file.
2124 */
2225public class BufferedZipStore extends ZipStore {
2326
2427 private final Store .ListableStore bufferStore ;
25- private String archiveComment ;
2628 private final boolean flushOnWrite ;
27-
2829 private final Comparator <String []> zipEntryComparator = (a , b ) -> {
2930 boolean aIsZarr = a .length > 0 && a [a .length - 1 ].equals ("zarr.json" );
3031 boolean bIsZarr = b .length > 0 && b [b .length - 1 ].equals ("zarr.json" );
@@ -45,6 +46,79 @@ public class BufferedZipStore extends ZipStore {
4546 return String .join ("/" , a ).compareTo (String .join ("/" , b ));
4647 }
4748 };
49+ private String archiveComment ;
50+
51+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore , @ Nullable String archiveComment , boolean flushOnWrite ) {
52+ super (underlyingStore );
53+ this .bufferStore = bufferStore ;
54+ this .archiveComment = archiveComment ;
55+ this .flushOnWrite = flushOnWrite ;
56+ try {
57+ loadBuffer ();
58+ } catch (IOException e ) {
59+ throw new RuntimeException ("Failed to load buffer from underlying store" , e );
60+ }
61+ }
62+
63+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore , @ Nullable String archiveComment ) {
64+ this (underlyingStore , bufferStore , archiveComment , false );
65+ }
66+
67+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore ) {
68+ this (underlyingStore , bufferStore , null );
69+ }
70+
71+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , String archiveComment ) {
72+ this (underlyingStore , new MemoryStore (), archiveComment );
73+ }
74+
75+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore ) {
76+ this (underlyingStore , (String ) null );
77+ }
78+
79+ public BufferedZipStore (@ Nonnull Path underlyingStore , String archiveComment ) {
80+ this (new FilesystemStore (underlyingStore .getParent ()).resolve (underlyingStore .getFileName ().toString ()), archiveComment );
81+ }
82+
83+ public BufferedZipStore (@ Nonnull Path underlyingStore ) {
84+ this (underlyingStore , null );
85+ }
86+
87+ public BufferedZipStore (@ Nonnull String underlyingStorePath , String archiveComment ) {
88+ this (Paths .get (underlyingStorePath ), archiveComment );
89+ }
90+
91+ public BufferedZipStore (@ Nonnull String underlyingStorePath ) {
92+ this (underlyingStorePath , null );
93+ }
94+
95+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore , boolean flushOnWrite ) {
96+ this (underlyingStore , bufferStore , null , flushOnWrite );
97+ }
98+
99+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , String archiveComment , boolean flushOnWrite ) {
100+ this (underlyingStore , new MemoryStore (), archiveComment , flushOnWrite );
101+ }
102+
103+ public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , boolean flushOnWrite ) {
104+ this (underlyingStore , (String ) null , flushOnWrite );
105+ }
106+
107+ public BufferedZipStore (@ Nonnull Path underlyingStore , String archiveComment , boolean flushOnWrite ) {
108+ this (new FilesystemStore (underlyingStore .getParent ()).resolve (underlyingStore .getFileName ().toString ()), archiveComment , flushOnWrite );
109+ }
110+
111+ public BufferedZipStore (@ Nonnull Path underlyingStore , boolean flushOnWrite ) {
112+ this (underlyingStore , null , flushOnWrite );
113+ }
114+
115+ public BufferedZipStore (@ Nonnull String underlyingStorePath , String archiveComment , boolean flushOnWrite ) {
116+ this (Paths .get (underlyingStorePath ), archiveComment , flushOnWrite );
117+ }
118+
119+ public BufferedZipStore (@ Nonnull String underlyingStorePath , boolean flushOnWrite ) {
120+ this (underlyingStorePath , null , flushOnWrite );
121+ }
48122
49123 private void writeBuffer () throws IOException {
50124 // create zip file bytes from buffer store and write to underlying store
@@ -111,13 +185,6 @@ private void writeBuffer() throws IOException {
111185 underlyingStore .set (ByteBuffer .wrap (zipBytes ));
112186 }
113187
114- public void setArchiveComment (@ Nullable String archiveComment ) throws IOException {
115- this .archiveComment = archiveComment ;
116- if (flushOnWrite ) {
117- writeBuffer ();
118- }
119- }
120-
121188 public void deleteArchiveComment () throws IOException {
122189 this .setArchiveComment (null );
123190 }
@@ -154,81 +221,8 @@ private void loadBuffer() throws IOException {
154221 }
155222 }
156223
157- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore , @ Nullable String archiveComment , boolean flushOnWrite ) {
158- super (underlyingStore );
159- this .bufferStore = bufferStore ;
160- this .archiveComment = archiveComment ;
161- this .flushOnWrite = flushOnWrite ;
162- try {
163- loadBuffer ();
164- } catch (IOException e ) {
165- throw new RuntimeException ("Failed to load buffer from underlying store" , e );
166- }
167- }
168-
169- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore , @ Nullable String archiveComment ) {
170- this (underlyingStore , bufferStore , archiveComment , false );
171- }
172-
173- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore ) {
174- this (underlyingStore , bufferStore , null );
175- }
176-
177- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , String archiveComment ) {
178- this (underlyingStore , new MemoryStore (), archiveComment );
179- }
180-
181- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore ) {
182- this (underlyingStore , (String ) null );
183- }
184-
185- public BufferedZipStore (@ Nonnull Path underlyingStore , String archiveComment ) {
186- this (new FilesystemStore (underlyingStore .getParent ()).resolve (underlyingStore .getFileName ().toString ()), archiveComment );
187- }
188-
189- public BufferedZipStore (@ Nonnull Path underlyingStore ) {
190- this (underlyingStore , null );
191- }
192-
193- public BufferedZipStore (@ Nonnull String underlyingStorePath , String archiveComment ) {
194- this (Paths .get (underlyingStorePath ), archiveComment );
195- }
196-
197- public BufferedZipStore (@ Nonnull String underlyingStorePath ) {
198- this (underlyingStorePath , null );
199- }
200-
201- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , @ Nonnull Store .ListableStore bufferStore , boolean flushOnWrite ) {
202- this (underlyingStore , bufferStore , null , flushOnWrite );
203- }
204-
205- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , String archiveComment , boolean flushOnWrite ) {
206- this (underlyingStore , new MemoryStore (), archiveComment , flushOnWrite );
207- }
208-
209- public BufferedZipStore (@ Nonnull StoreHandle underlyingStore , boolean flushOnWrite ) {
210- this (underlyingStore , (String ) null , flushOnWrite );
211- }
212-
213- public BufferedZipStore (@ Nonnull Path underlyingStore , String archiveComment , boolean flushOnWrite ) {
214- this (new FilesystemStore (underlyingStore .getParent ()).resolve (underlyingStore .getFileName ().toString ()), archiveComment , flushOnWrite );
215- }
216-
217- public BufferedZipStore (@ Nonnull Path underlyingStore , boolean flushOnWrite ) {
218- this (underlyingStore , null , flushOnWrite );
219- }
220-
221- public BufferedZipStore (@ Nonnull String underlyingStorePath , String archiveComment , boolean flushOnWrite ) {
222- this (Paths .get (underlyingStorePath ), archiveComment , flushOnWrite );
223- }
224-
225- public BufferedZipStore (@ Nonnull String underlyingStorePath , boolean flushOnWrite ) {
226- this (underlyingStorePath , null , flushOnWrite );
227- }
228-
229-
230224 /**
231- * Flushes the buffer and archiveComment to the underlying store as a zip file.
225+ * Flushes the buffer and archiveComment to the underlying store as a zip file.
232226 */
233227 public void flush () throws IOException {
234228 writeBuffer ();
@@ -239,6 +233,13 @@ public String getArchiveComment() {
239233 return archiveComment ;
240234 }
241235
236+ public void setArchiveComment (@ Nullable String archiveComment ) throws IOException {
237+ this .archiveComment = archiveComment ;
238+ if (flushOnWrite ) {
239+ writeBuffer ();
240+ }
241+ }
242+
242243 @ Override
243244 public Stream <String []> list (String [] keys ) {
244245 return bufferStore .list (keys );
0 commit comments