1- package software .coley .llzip ;
2-
3- import software .coley .llzip .format .model .CentralDirectoryFileHeader ;
4- import software .coley .llzip .format .model .EndOfCentralDirectory ;
5- import software .coley .llzip .format .model .ZipArchive ;
6- import software .coley .llzip .format .read .ForwardScanZipReaderStrategy ;
7- import software .coley .llzip .format .read .JvmZipReaderStrategy ;
8- import software .coley .llzip .format .read .NaiveLocalFileZipReaderStrategy ;
9- import software .coley .llzip .format .read .ZipReaderStrategy ;
10- import software .coley .llzip .util .BufferData ;
11- import software .coley .llzip .util .ByteData ;
12- import software .coley .llzip .util .FileMapUtil ;
1+ package software .coley .lljzip ;
2+
3+ import software .coley .lljzip .format .model .CentralDirectoryFileHeader ;
4+ import software .coley .lljzip .format .model .EndOfCentralDirectory ;
5+ import software .coley .lljzip .format .model .ZipArchive ;
6+ import software .coley .lljzip .format .read .ForwardScanZipReader ;
7+ import software .coley .lljzip .format .read .JvmZipReader ;
8+ import software .coley .lljzip .format .read .NaiveLocalFileZipReader ;
9+ import software .coley .lljzip .format .read .ZipReader ;
10+ import software .coley .lljzip .util .BufferData ;
11+ import software .coley .lljzip .util .ByteData ;
12+ import software .coley .lljzip .util .FileMapUtil ;
1313
1414import java .io .FileNotFoundException ;
1515import java .io .IOException ;
1919/**
2020 * IO wrappers for reading {@link ZipArchive} contents.
2121 * <ul>
22- * <li>For JAR files or anything intended to be read by the JVM use the JVM operations which use {@link JvmZipReaderStrategy }.</li>
23- * <li>For regular ZIP files use {@link ForwardScanZipReaderStrategy }.</li>
24- * <li>For ZIP files without {@link CentralDirectoryFileHeader} or {@link EndOfCentralDirectory} items, use {@link NaiveLocalFileZipReaderStrategy }</li>
22+ * <li>For JAR files or anything intended to be read by the JVM use the JVM operations which use {@link JvmZipReader }.</li>
23+ * <li>For regular ZIP files use {@link ForwardScanZipReader }.</li>
24+ * <li>For ZIP files without {@link CentralDirectoryFileHeader} or {@link EndOfCentralDirectory} items, use {@link NaiveLocalFileZipReader }</li>
2525 * </ul>
26+ * You can fully control zip parsing via {@link #read(ByteData, ZipReader)} by passing a customized reader implementation.
2627 *
2728 * @author Matt Coley
2829 */
2930public class ZipIO {
3031 /**
31- * Creates an archive using the {@link ForwardScanZipReaderStrategy }.
32+ * Creates an archive using the {@link ForwardScanZipReader }.
3233 *
3334 * @param data
3435 * Zip bytes.
@@ -39,11 +40,11 @@ public class ZipIO {
3940 * When the archive bytes cannot be read from, usually indicating a malformed zip.
4041 */
4142 public static ZipArchive readStandard (ByteData data ) throws IOException {
42- return read (data , new ForwardScanZipReaderStrategy ());
43+ return read (data , new ForwardScanZipReader ());
4344 }
4445
4546 /**
46- * Creates an archive using the {@link ForwardScanZipReaderStrategy }.
47+ * Creates an archive using the {@link ForwardScanZipReader }.
4748 *
4849 * @param data
4950 * Zip bytes.
@@ -54,11 +55,11 @@ public static ZipArchive readStandard(ByteData data) throws IOException {
5455 * When the archive bytes cannot be read from, usually indicating a malformed zip.
5556 */
5657 public static ZipArchive readStandard (byte [] data ) throws IOException {
57- return read (data , new ForwardScanZipReaderStrategy ());
58+ return read (data , new ForwardScanZipReader ());
5859 }
5960
6061 /**
61- * Creates an archive using the {@link ForwardScanZipReaderStrategy }.
62+ * Creates an archive using the {@link ForwardScanZipReader }.
6263 *
6364 * @param data
6465 * Zip path.
@@ -69,11 +70,11 @@ public static ZipArchive readStandard(byte[] data) throws IOException {
6970 * When the archive bytes cannot be read from, usually indicating a malformed zip.
7071 */
7172 public static ZipArchive readStandard (Path data ) throws IOException {
72- return read (data , new ForwardScanZipReaderStrategy ());
73+ return read (data , new ForwardScanZipReader ());
7374 }
7475
7576 /**
76- * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy }.
77+ * Creates an archive using the {@link NaiveLocalFileZipReader }.
7778 *
7879 * @param data
7980 * Zip bytes.
@@ -84,11 +85,11 @@ public static ZipArchive readStandard(Path data) throws IOException {
8485 * When the archive bytes cannot be read from, usually indicating a malformed zip.
8586 */
8687 public static ZipArchive readNaive (ByteData data ) throws IOException {
87- return read (data , new NaiveLocalFileZipReaderStrategy ());
88+ return read (data , new NaiveLocalFileZipReader ());
8889 }
8990
9091 /**
91- * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy }.
92+ * Creates an archive using the {@link NaiveLocalFileZipReader }.
9293 *
9394 * @param data
9495 * Zip bytes.
@@ -99,11 +100,11 @@ public static ZipArchive readNaive(ByteData data) throws IOException {
99100 * When the archive bytes cannot be read from, usually indicating a malformed zip.
100101 */
101102 public static ZipArchive readNaive (byte [] data ) throws IOException {
102- return read (data , new NaiveLocalFileZipReaderStrategy ());
103+ return read (data , new NaiveLocalFileZipReader ());
103104 }
104105
105106 /**
106- * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy }.
107+ * Creates an archive using the {@link NaiveLocalFileZipReader }.
107108 *
108109 * @param data
109110 * Zip path.
@@ -114,11 +115,11 @@ public static ZipArchive readNaive(byte[] data) throws IOException {
114115 * When the archive bytes cannot be read from, usually indicating a malformed zip.
115116 */
116117 public static ZipArchive readNaive (Path data ) throws IOException {
117- return read (data , new NaiveLocalFileZipReaderStrategy ());
118+ return read (data , new NaiveLocalFileZipReader ());
118119 }
119120
120121 /**
121- * Creates an archive using the {@link JvmZipReaderStrategy } which handles some edge cases not usually
122+ * Creates an archive using the {@link JvmZipReader } which handles some edge cases not usually
122123 * expected from zip files.
123124 *
124125 * @param data
@@ -130,11 +131,11 @@ public static ZipArchive readNaive(Path data) throws IOException {
130131 * When the archive bytes cannot be read from, usually indicating a malformed zip.
131132 */
132133 public static ZipArchive readJvm (ByteData data ) throws IOException {
133- return read (data , new JvmZipReaderStrategy ());
134+ return read (data , new JvmZipReader ());
134135 }
135136
136137 /**
137- * Creates an archive using the {@link JvmZipReaderStrategy } which handles some edge cases not usually
138+ * Creates an archive using the {@link JvmZipReader } which handles some edge cases not usually
138139 * expected from zip files.
139140 *
140141 * @param data
@@ -146,11 +147,11 @@ public static ZipArchive readJvm(ByteData data) throws IOException {
146147 * When the archive bytes cannot be read from, usually indicating a malformed zip.
147148 */
148149 public static ZipArchive readJvm (byte [] data ) throws IOException {
149- return read (data , new JvmZipReaderStrategy ());
150+ return read (data , new JvmZipReader ());
150151 }
151152
152153 /**
153- * Creates an archive using the {@link JvmZipReaderStrategy } which handles some edge cases not usually
154+ * Creates an archive using the {@link JvmZipReader } which handles some edge cases not usually
154155 * expected from zip files.
155156 *
156157 * @param path
@@ -162,7 +163,7 @@ public static ZipArchive readJvm(byte[] data) throws IOException {
162163 * When the archive bytes cannot be read from, usually indicating a malformed zip.
163164 */
164165 public static ZipArchive readJvm (Path path ) throws IOException {
165- return read (path , new JvmZipReaderStrategy ());
166+ return read (path , new JvmZipReader ());
166167 }
167168
168169 /**
@@ -176,22 +177,15 @@ public static ZipArchive readJvm(Path path) throws IOException {
176177 * @throws IOException
177178 * When the archive bytes cannot be read from, usually indicating a malformed zip.
178179 */
179- public static ZipArchive read (ByteData data , ZipReaderStrategy strategy ) throws IOException {
180+ public static ZipArchive read (byte [] data , ZipReader strategy ) throws IOException {
180181 if (data == null )
181182 throw new IOException ("Data is null!" );
182- // The fixed size elements of a CDFH is 22 bytes (plus the variable size bits which can be 0)
183- // - Even if we only want to read local/central file entries, those are even larger at a minimum
184- if (data .length () < 22 )
185- throw new IOException ("Not enough bytes to read Central-Directory-File-Header, minimum=22" );
186- // Create instance
187- ZipArchive zip = new ZipArchive (data );
188- strategy .read (zip , data );
189- return zip ;
183+ return read (BufferData .wrap (data ), strategy );
190184 }
191185
192186 /**
193- * @param data
194- * Zip bytes .
187+ * @param path
188+ * Zip path .
195189 * @param strategy
196190 * Zip reader implementation.
197191 *
@@ -200,15 +194,17 @@ public static ZipArchive read(ByteData data, ZipReaderStrategy strategy) throws
200194 * @throws IOException
201195 * When the archive bytes cannot be read from, usually indicating a malformed zip.
202196 */
203- public static ZipArchive read (byte [] data , ZipReaderStrategy strategy ) throws IOException {
204- if (data == null )
197+ public static ZipArchive read (Path path , ZipReader strategy ) throws IOException {
198+ if (path == null )
205199 throw new IOException ("Data is null!" );
206- return read (BufferData .wrap (data ), strategy );
200+ if (!Files .isRegularFile (path ))
201+ throw new FileNotFoundException (path .toString ());
202+ return read (FileMapUtil .map (path ), strategy );
207203 }
208204
209205 /**
210- * @param path
211- * Zip path .
206+ * @param data
207+ * Zip bytes .
212208 * @param strategy
213209 * Zip reader implementation.
214210 *
@@ -217,11 +213,18 @@ public static ZipArchive read(byte[] data, ZipReaderStrategy strategy) throws IO
217213 * @throws IOException
218214 * When the archive bytes cannot be read from, usually indicating a malformed zip.
219215 */
220- public static ZipArchive read (Path path , ZipReaderStrategy strategy ) throws IOException {
221- if (path == null )
216+ public static ZipArchive read (ByteData data , ZipReader strategy ) throws IOException {
217+ if (data == null )
222218 throw new IOException ("Data is null!" );
223- if (!Files .isRegularFile (path ))
224- throw new FileNotFoundException (path .toString ());
225- return read (FileMapUtil .map (path ), strategy );
219+
220+ // The fixed size elements of a CDFH is 22 bytes (plus the variable size bits which can be 0)
221+ // - Even if we only want to read local/central file entries, those are even larger at a minimum
222+ if (data .length () < 22 )
223+ throw new IOException ("Not enough bytes to read Central-Directory-File-Header, minimum=22" );
224+
225+ // Create instance
226+ ZipArchive zip = new ZipArchive (data );
227+ strategy .read (zip , data );
228+ return zip ;
226229 }
227230}
0 commit comments