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 ;
13
13
14
14
import java .io .FileNotFoundException ;
15
15
import java .io .IOException ;
19
19
/**
20
20
* IO wrappers for reading {@link ZipArchive} contents.
21
21
* <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>
25
25
* </ul>
26
+ * You can fully control zip parsing via {@link #read(ByteData, ZipReader)} by passing a customized reader implementation.
26
27
*
27
28
* @author Matt Coley
28
29
*/
29
30
public class ZipIO {
30
31
/**
31
- * Creates an archive using the {@link ForwardScanZipReaderStrategy }.
32
+ * Creates an archive using the {@link ForwardScanZipReader }.
32
33
*
33
34
* @param data
34
35
* Zip bytes.
@@ -39,11 +40,11 @@ public class ZipIO {
39
40
* When the archive bytes cannot be read from, usually indicating a malformed zip.
40
41
*/
41
42
public static ZipArchive readStandard (ByteData data ) throws IOException {
42
- return read (data , new ForwardScanZipReaderStrategy ());
43
+ return read (data , new ForwardScanZipReader ());
43
44
}
44
45
45
46
/**
46
- * Creates an archive using the {@link ForwardScanZipReaderStrategy }.
47
+ * Creates an archive using the {@link ForwardScanZipReader }.
47
48
*
48
49
* @param data
49
50
* Zip bytes.
@@ -54,11 +55,11 @@ public static ZipArchive readStandard(ByteData data) throws IOException {
54
55
* When the archive bytes cannot be read from, usually indicating a malformed zip.
55
56
*/
56
57
public static ZipArchive readStandard (byte [] data ) throws IOException {
57
- return read (data , new ForwardScanZipReaderStrategy ());
58
+ return read (data , new ForwardScanZipReader ());
58
59
}
59
60
60
61
/**
61
- * Creates an archive using the {@link ForwardScanZipReaderStrategy }.
62
+ * Creates an archive using the {@link ForwardScanZipReader }.
62
63
*
63
64
* @param data
64
65
* Zip path.
@@ -69,11 +70,11 @@ public static ZipArchive readStandard(byte[] data) throws IOException {
69
70
* When the archive bytes cannot be read from, usually indicating a malformed zip.
70
71
*/
71
72
public static ZipArchive readStandard (Path data ) throws IOException {
72
- return read (data , new ForwardScanZipReaderStrategy ());
73
+ return read (data , new ForwardScanZipReader ());
73
74
}
74
75
75
76
/**
76
- * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy }.
77
+ * Creates an archive using the {@link NaiveLocalFileZipReader }.
77
78
*
78
79
* @param data
79
80
* Zip bytes.
@@ -84,11 +85,11 @@ public static ZipArchive readStandard(Path data) throws IOException {
84
85
* When the archive bytes cannot be read from, usually indicating a malformed zip.
85
86
*/
86
87
public static ZipArchive readNaive (ByteData data ) throws IOException {
87
- return read (data , new NaiveLocalFileZipReaderStrategy ());
88
+ return read (data , new NaiveLocalFileZipReader ());
88
89
}
89
90
90
91
/**
91
- * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy }.
92
+ * Creates an archive using the {@link NaiveLocalFileZipReader }.
92
93
*
93
94
* @param data
94
95
* Zip bytes.
@@ -99,11 +100,11 @@ public static ZipArchive readNaive(ByteData data) throws IOException {
99
100
* When the archive bytes cannot be read from, usually indicating a malformed zip.
100
101
*/
101
102
public static ZipArchive readNaive (byte [] data ) throws IOException {
102
- return read (data , new NaiveLocalFileZipReaderStrategy ());
103
+ return read (data , new NaiveLocalFileZipReader ());
103
104
}
104
105
105
106
/**
106
- * Creates an archive using the {@link NaiveLocalFileZipReaderStrategy }.
107
+ * Creates an archive using the {@link NaiveLocalFileZipReader }.
107
108
*
108
109
* @param data
109
110
* Zip path.
@@ -114,11 +115,11 @@ public static ZipArchive readNaive(byte[] data) throws IOException {
114
115
* When the archive bytes cannot be read from, usually indicating a malformed zip.
115
116
*/
116
117
public static ZipArchive readNaive (Path data ) throws IOException {
117
- return read (data , new NaiveLocalFileZipReaderStrategy ());
118
+ return read (data , new NaiveLocalFileZipReader ());
118
119
}
119
120
120
121
/**
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
122
123
* expected from zip files.
123
124
*
124
125
* @param data
@@ -130,11 +131,11 @@ public static ZipArchive readNaive(Path data) throws IOException {
130
131
* When the archive bytes cannot be read from, usually indicating a malformed zip.
131
132
*/
132
133
public static ZipArchive readJvm (ByteData data ) throws IOException {
133
- return read (data , new JvmZipReaderStrategy ());
134
+ return read (data , new JvmZipReader ());
134
135
}
135
136
136
137
/**
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
138
139
* expected from zip files.
139
140
*
140
141
* @param data
@@ -146,11 +147,11 @@ public static ZipArchive readJvm(ByteData data) throws IOException {
146
147
* When the archive bytes cannot be read from, usually indicating a malformed zip.
147
148
*/
148
149
public static ZipArchive readJvm (byte [] data ) throws IOException {
149
- return read (data , new JvmZipReaderStrategy ());
150
+ return read (data , new JvmZipReader ());
150
151
}
151
152
152
153
/**
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
154
155
* expected from zip files.
155
156
*
156
157
* @param path
@@ -162,7 +163,7 @@ public static ZipArchive readJvm(byte[] data) throws IOException {
162
163
* When the archive bytes cannot be read from, usually indicating a malformed zip.
163
164
*/
164
165
public static ZipArchive readJvm (Path path ) throws IOException {
165
- return read (path , new JvmZipReaderStrategy ());
166
+ return read (path , new JvmZipReader ());
166
167
}
167
168
168
169
/**
@@ -176,22 +177,15 @@ public static ZipArchive readJvm(Path path) throws IOException {
176
177
* @throws IOException
177
178
* When the archive bytes cannot be read from, usually indicating a malformed zip.
178
179
*/
179
- public static ZipArchive read (ByteData data , ZipReaderStrategy strategy ) throws IOException {
180
+ public static ZipArchive read (byte [] data , ZipReader strategy ) throws IOException {
180
181
if (data == null )
181
182
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 );
190
184
}
191
185
192
186
/**
193
- * @param data
194
- * Zip bytes .
187
+ * @param path
188
+ * Zip path .
195
189
* @param strategy
196
190
* Zip reader implementation.
197
191
*
@@ -200,15 +194,17 @@ public static ZipArchive read(ByteData data, ZipReaderStrategy strategy) throws
200
194
* @throws IOException
201
195
* When the archive bytes cannot be read from, usually indicating a malformed zip.
202
196
*/
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 )
205
199
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 );
207
203
}
208
204
209
205
/**
210
- * @param path
211
- * Zip path .
206
+ * @param data
207
+ * Zip bytes .
212
208
* @param strategy
213
209
* Zip reader implementation.
214
210
*
@@ -217,11 +213,18 @@ public static ZipArchive read(byte[] data, ZipReaderStrategy strategy) throws IO
217
213
* @throws IOException
218
214
* When the archive bytes cannot be read from, usually indicating a malformed zip.
219
215
*/
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 )
222
218
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 ;
226
229
}
227
230
}
0 commit comments