4
4
import software .coley .lljzip .format .model .EndOfCentralDirectory ;
5
5
import software .coley .lljzip .format .model .ZipArchive ;
6
6
import software .coley .lljzip .format .read .*;
7
- import software .coley .lljzip .util .BufferData ;
8
- import software .coley .lljzip .util .ByteData ;
9
- import software .coley .lljzip .util .FileMapUtil ;
10
7
11
8
import java .io .FileNotFoundException ;
12
9
import java .io .IOException ;
10
+ import java .lang .foreign .Arena ;
11
+ import java .lang .foreign .MemorySegment ;
12
+ import java .nio .channels .FileChannel ;
13
13
import java .nio .file .Files ;
14
14
import java .nio .file .Path ;
15
15
import java .util .zip .ZipFile ;
21
21
* <li>For regular ZIP files use {@link ForwardScanZipReader}.</li>
22
22
* <li>For ZIP files without {@link CentralDirectoryFileHeader} or {@link EndOfCentralDirectory} items, use {@link NaiveLocalFileZipReader}</li>
23
23
* </ul>
24
- * You can fully control zip parsing via {@link #read(ByteData , ZipReader)} by passing a customized reader implementation.
24
+ * You can fully control zip parsing via {@link #read(MemorySegment , ZipReader)} by passing a customized reader implementation.
25
25
*
26
26
* @author Matt Coley
27
27
*/
@@ -37,7 +37,7 @@ public class ZipIO {
37
37
* @throws IOException
38
38
* When the archive bytes cannot be read from, usually indicating a malformed zip.
39
39
*/
40
- public static ZipArchive readStandard (ByteData data ) throws IOException {
40
+ public static ZipArchive readStandard (MemorySegment data ) throws IOException {
41
41
return read (data , new ForwardScanZipReader ());
42
42
}
43
43
@@ -82,7 +82,7 @@ public static ZipArchive readStandard(Path data) throws IOException {
82
82
* @throws IOException
83
83
* When the archive bytes cannot be read from, usually indicating a malformed zip.
84
84
*/
85
- public static ZipArchive readNaive (ByteData data ) throws IOException {
85
+ public static ZipArchive readNaive (MemorySegment data ) throws IOException {
86
86
return read (data , new NaiveLocalFileZipReader ());
87
87
}
88
88
@@ -128,7 +128,7 @@ public static ZipArchive readNaive(Path data) throws IOException {
128
128
* @throws IOException
129
129
* When the archive bytes cannot be read from, usually indicating a malformed zip.
130
130
*/
131
- public static ZipArchive readJvm (ByteData data ) throws IOException {
131
+ public static ZipArchive readJvm (MemorySegment data ) throws IOException {
132
132
return read (data , new JvmZipReader ());
133
133
}
134
134
@@ -172,7 +172,7 @@ public static ZipArchive readJvm(Path path) throws IOException {
172
172
*
173
173
* @return Archive from path.
174
174
*
175
- * @throws IOException
175
+ * @throws IOException When the archive cannot be read.
176
176
*/
177
177
public static ZipArchive readAdaptingIO (Path path ) throws IOException {
178
178
ZipArchive archive = new ZipArchive ();
@@ -194,7 +194,7 @@ public static ZipArchive readAdaptingIO(Path path) throws IOException {
194
194
public static ZipArchive read (byte [] data , ZipReader strategy ) throws IOException {
195
195
if (data == null )
196
196
throw new IOException ("Data is null!" );
197
- return read (BufferData . wrap (data ), strategy );
197
+ return read (MemorySegment . ofArray (data ), strategy );
198
198
}
199
199
200
200
/**
@@ -213,7 +213,23 @@ public static ZipArchive read(Path path, ZipReader strategy) throws IOException
213
213
throw new IOException ("Data is null!" );
214
214
if (!Files .isRegularFile (path ))
215
215
throw new FileNotFoundException (path .toString ());
216
- return read (FileMapUtil .map (path ), strategy );
216
+ FileChannel fc = FileChannel .open (path );
217
+ try {
218
+ long size = fc .size ();
219
+ // The fixed size elements of a CDFH is 22 bytes (plus the variable size bits which can be 0)
220
+ // - Even if we only want to read local/central file entries, those are even larger at a minimum
221
+ if (size < 22 )
222
+ throw new IOException ("Not enough bytes to read Central-Directory-File-Header, minimum=22" );
223
+
224
+ ZipArchive zip = new ZipArchive (fc );
225
+ strategy .read (zip , fc .map (FileChannel .MapMode .READ_ONLY , 0L , size , Arena .ofAuto ()));
226
+ fc = null ;
227
+ return zip ;
228
+ } finally {
229
+ if (fc != null ) {
230
+ fc .close ();
231
+ }
232
+ }
217
233
}
218
234
219
235
/**
@@ -227,17 +243,17 @@ public static ZipArchive read(Path path, ZipReader strategy) throws IOException
227
243
* @throws IOException
228
244
* When the archive bytes cannot be read from, usually indicating a malformed zip.
229
245
*/
230
- public static ZipArchive read (ByteData data , ZipReader strategy ) throws IOException {
246
+ public static ZipArchive read (MemorySegment data , ZipReader strategy ) throws IOException {
231
247
if (data == null )
232
248
throw new IOException ("Data is null!" );
233
249
234
250
// The fixed size elements of a CDFH is 22 bytes (plus the variable size bits which can be 0)
235
251
// - Even if we only want to read local/central file entries, those are even larger at a minimum
236
- if (data .length () < 22 )
252
+ if (data .byteSize () < 22 )
237
253
throw new IOException ("Not enough bytes to read Central-Directory-File-Header, minimum=22" );
238
254
239
255
// Create instance
240
- ZipArchive zip = new ZipArchive (data );
256
+ ZipArchive zip = new ZipArchive ();
241
257
strategy .read (zip , data );
242
258
return zip ;
243
259
}
0 commit comments