44import software .coley .lljzip .format .model .EndOfCentralDirectory ;
55import software .coley .lljzip .format .model .ZipArchive ;
66import 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 ;
107
118import java .io .FileNotFoundException ;
129import java .io .IOException ;
10+ import java .lang .foreign .Arena ;
11+ import java .lang .foreign .MemorySegment ;
12+ import java .nio .channels .FileChannel ;
1313import java .nio .file .Files ;
1414import java .nio .file .Path ;
1515import java .util .zip .ZipFile ;
2121 * <li>For regular ZIP files use {@link ForwardScanZipReader}.</li>
2222 * <li>For ZIP files without {@link CentralDirectoryFileHeader} or {@link EndOfCentralDirectory} items, use {@link NaiveLocalFileZipReader}</li>
2323 * </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.
2525 *
2626 * @author Matt Coley
2727 */
@@ -37,7 +37,7 @@ public class ZipIO {
3737 * @throws IOException
3838 * When the archive bytes cannot be read from, usually indicating a malformed zip.
3939 */
40- public static ZipArchive readStandard (ByteData data ) throws IOException {
40+ public static ZipArchive readStandard (MemorySegment data ) throws IOException {
4141 return read (data , new ForwardScanZipReader ());
4242 }
4343
@@ -82,7 +82,7 @@ public static ZipArchive readStandard(Path data) throws IOException {
8282 * @throws IOException
8383 * When the archive bytes cannot be read from, usually indicating a malformed zip.
8484 */
85- public static ZipArchive readNaive (ByteData data ) throws IOException {
85+ public static ZipArchive readNaive (MemorySegment data ) throws IOException {
8686 return read (data , new NaiveLocalFileZipReader ());
8787 }
8888
@@ -128,7 +128,7 @@ public static ZipArchive readNaive(Path data) throws IOException {
128128 * @throws IOException
129129 * When the archive bytes cannot be read from, usually indicating a malformed zip.
130130 */
131- public static ZipArchive readJvm (ByteData data ) throws IOException {
131+ public static ZipArchive readJvm (MemorySegment data ) throws IOException {
132132 return read (data , new JvmZipReader ());
133133 }
134134
@@ -194,7 +194,7 @@ public static ZipArchive readAdaptingIO(Path path) throws IOException {
194194 public static ZipArchive read (byte [] data , ZipReader strategy ) throws IOException {
195195 if (data == null )
196196 throw new IOException ("Data is null!" );
197- return read (BufferData . wrap (data ), strategy );
197+ return read (MemorySegment . ofArray (data ), strategy );
198198 }
199199
200200 /**
@@ -213,7 +213,23 @@ public static ZipArchive read(Path path, ZipReader strategy) throws IOException
213213 throw new IOException ("Data is null!" );
214214 if (!Files .isRegularFile (path ))
215215 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+ }
217233 }
218234
219235 /**
@@ -227,17 +243,17 @@ public static ZipArchive read(Path path, ZipReader strategy) throws IOException
227243 * @throws IOException
228244 * When the archive bytes cannot be read from, usually indicating a malformed zip.
229245 */
230- public static ZipArchive read (ByteData data , ZipReader strategy ) throws IOException {
246+ public static ZipArchive read (MemorySegment data , ZipReader strategy ) throws IOException {
231247 if (data == null )
232248 throw new IOException ("Data is null!" );
233249
234250 // The fixed size elements of a CDFH is 22 bytes (plus the variable size bits which can be 0)
235251 // - 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 )
237253 throw new IOException ("Not enough bytes to read Central-Directory-File-Header, minimum=22" );
238254
239255 // Create instance
240- ZipArchive zip = new ZipArchive (data );
256+ ZipArchive zip = new ZipArchive ();
241257 strategy .read (zip , data );
242258 return zip ;
243259 }
0 commit comments