Skip to content

Commit b6a4e0c

Browse files
authored
Merge pull request #20 from Col-E/refactoring
2 parents b7cbbb8 + 832d8e3 commit b6a4e0c

File tree

65 files changed

+1696
-612
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+1696
-612
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ implementation group: 'software.coley', name: 'lljzip', version: zipVersion
5050
implementation "software.coley:lljzip:${zipVersion}"
5151
```
5252

53-
For example usage see the [tests](src/test/java/software/coley/llzip).
53+
For example usage see the [tests](src/test/java/software/coley/lljzip).

pom.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.coley</groupId>
88
<artifactId>lljzip</artifactId>
9-
<version>1.6.2</version>
9+
<version>2.0.0</version>
1010

1111
<name>LL Java ZIP</name>
1212
<description>Lower level ZIP support for Java</description>
@@ -41,6 +41,13 @@
4141
<artifactId>slf4j-api</artifactId>
4242
<version>1.7.36</version>
4343
</dependency>
44+
<!-- https://mvnrepository.com/artifact/com.google.code.findbugs/jsr305 -->
45+
<dependency>
46+
<groupId>com.google.code.findbugs</groupId>
47+
<artifactId>jsr305</artifactId>
48+
<version>3.0.2</version>
49+
<scope>compile</scope>
50+
</dependency>
4451
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
4552
<dependency>
4653
<groupId>ch.qos.logback</groupId>
Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
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

1414
import java.io.FileNotFoundException;
1515
import java.io.IOException;
@@ -19,16 +19,17 @@
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
*/
2930
public 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
}

src/main/java/software/coley/llzip/format/ZipPatterns.java renamed to src/main/java/software/coley/lljzip/format/ZipPatterns.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
package software.coley.llzip.format;
1+
package software.coley.lljzip.format;
22

3-
import software.coley.llzip.format.model.CentralDirectoryFileHeader;
4-
import software.coley.llzip.format.model.EndOfCentralDirectory;
5-
import software.coley.llzip.format.model.LocalFileHeader;
6-
import software.coley.llzip.util.ByteDataUtil;
3+
import software.coley.lljzip.format.model.CentralDirectoryFileHeader;
4+
import software.coley.lljzip.format.model.EndOfCentralDirectory;
5+
import software.coley.lljzip.format.model.LocalFileHeader;
6+
import software.coley.lljzip.util.ByteDataUtil;
77

88
/**
99
* Patterns for usage in {@link ByteDataUtil} methods.
@@ -43,4 +43,12 @@ public interface ZipPatterns {
4343
* Header for {@link EndOfCentralDirectory}, as a {@code s4/quad/int}
4444
*/
4545
int END_OF_CENTRAL_DIRECTORY_QUAD = 0x06_05_4B_50;
46+
/**
47+
* Optional header for the data descriptor section
48+
*/
49+
int[] DATA_DESCRIPTOR = {0x08, 0x07, 0x4b, 0x50};
50+
/**
51+
* Optional header for the data descriptor section, as a {@code s4/quad/int}
52+
*/
53+
int DATA_DESCRIPTOR_QUAD = 0x08_07_4b_50;
4654
}

src/main/java/software/coley/llzip/format/compression/Decompressor.java renamed to src/main/java/software/coley/lljzip/format/compression/Decompressor.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
package software.coley.llzip.format.compression;
1+
package software.coley.lljzip.format.compression;
22

3-
import software.coley.llzip.format.model.LocalFileHeader;
4-
import software.coley.llzip.util.ByteData;
3+
import software.coley.lljzip.format.model.LocalFileHeader;
4+
import software.coley.lljzip.util.ByteData;
55

66
import java.io.IOException;
77

src/main/java/software/coley/llzip/format/compression/DeflateDecompressor.java renamed to src/main/java/software/coley/lljzip/format/compression/DeflateDecompressor.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package software.coley.llzip.format.compression;
1+
package software.coley.lljzip.format.compression;
22

3-
import software.coley.llzip.format.model.LocalFileHeader;
4-
import software.coley.llzip.util.ByteData;
5-
import software.coley.llzip.util.FastWrapOutputStream;
3+
import software.coley.lljzip.format.model.LocalFileHeader;
4+
import software.coley.lljzip.util.ByteData;
5+
import software.coley.lljzip.util.FastWrapOutputStream;
66

77
import java.io.IOException;
88
import java.util.zip.DataFormatException;
@@ -15,6 +15,12 @@
1515
* @author Matt Coley
1616
*/
1717
public class DeflateDecompressor implements Decompressor {
18+
public static final DeflateDecompressor INSTANCE = new DeflateDecompressor();
19+
20+
private DeflateDecompressor() {
21+
// deny construction
22+
}
23+
1824
@Override
1925
public ByteData decompress(LocalFileHeader header, ByteData data) throws IOException {
2026
if (header.getCompressionMethod() != ZipCompressions.DEFLATED)

src/main/java/software/coley/llzip/format/compression/DeflateEntry.java renamed to src/main/java/software/coley/lljzip/format/compression/DeflateEntry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package software.coley.llzip.format.compression;
1+
package software.coley.lljzip.format.compression;
22

3-
import software.coley.llzip.util.InflaterHackery;
3+
import software.coley.lljzip.util.InflaterHackery;
44

55
import java.util.zip.Inflater;
66

0 commit comments

Comments
 (0)